7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Sassで:last-childなどの擬似クラスを使用するときの注意点

Last updated at Posted at 2019-02-26

概要

たとえば、要素を繰り返し表示していて最後の要素のみmarginを抜きたいなど、:last-childを使用することがあるが、ネストして書く場合は必ず&をつけなければならない。

付けなかった場合、その配下の要素にlast-childが付いてしまい上手くスタイリングできない。

正しいコード

 .data {
   &__item {
     margin-bottom: 15px;

     &:last-child {
       margin-bottom: 0;
     }
   }
 }

間違っているコード

 .data {
   &__item {
     margin-bottom: 15px;

     :last-child {
       margin-bottom: 0;
     }
   }
 }
7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?