less css using last-child selector

2020-08-13 07:32发布

问题:

I am using less and trying to get the last input to have a margin-bottom of 10px. Here is what I have, but it is not working, and not applying the margin-bottom on the last input. Any ideas why?

input {
  margin-bottom: 0px;

  &:last-child {
    margin-bottom: 10px;
  }
}

And the HTML

<form id="auth-form">
  <input id="ember367" class="ember-view ember-text-field" placeholder="Email" type="email" name="email">
  <input id="ember368" class="ember-view ember-text-field" placeholder="Password" type="password" name="password">
  <div class="clear"></div>

  <a class="pull-left forgot-password">Forgot password?</a>
  <button class="pull-right" data-ember-action="1" type="button">Sign In</button>
</form>

回答1:

Your last input is not the last child — that would be your button.

Use :last-of-type instead to select the last input:

input {
  margin-bottom: 0px;

  &:last-of-type {
    margin-bottom: 10px;
  }
}