CSS3の否定疑似クラス「:not」の使い方

CSS3の否定疑似クラス「:not」の使い方を覚えれば、
コーディングをよりシンプルに書くことが出来ます。
:not とは
css3の否定疑似クラスで、
○○以外に対してスタイルを適用する場合に使います。
:not は以下のように使います。
|
1 2 3 |
:not(セレクタ or クラス) { /* 指定したセレクタorクラス以外に適用するCSS */ } |
:notの使用例
・使用例1:最後のリストだけボーダーを入れたくない場合

これまでの書き方
|
1 2 |
ul li {border-bottom: 1px dotted #000;} ul li:last-child {border-bottom: none;} |
:not を使った書き方
|
1 |
ul li:not(:last-child) {border-bottom: 1px dotted #000;} |
・使用例2:横並びの3の倍数の要素にだけ余白を入れたくない場合

これまでの書き方
|
1 2 3 4 5 6 |
ul li { margin-right: 2%; } ul li:nth-child(3n) { margin-right: 0; } |
:not を使った書き方
|
1 2 3 |
ul li:not(:nth-child(3n)) { margin-right: 2%; } |
![MARKLEAPS[マークリープス]](https://markleaps.com/blog/wp-content/themes/mkl/images/00_logo.png)


![[CSS] shape-outside プロパティで画像にテキストを回り込ませる方法](https://markleaps.com/blog/wp-content/uploads/2025/09/css-shape-outside-500x254.jpg)
![[HTML・CSS] 説明リストdlのdtとddを横並びに表示する方法(コピペで使えるコード付き)](https://markleaps.com/blog/wp-content/uploads/2025/09/html-css-dl-flex-grid-500x254.jpg)
![[CSS] 疑似クラス is() とwhere() の使い方と違い](https://markleaps.com/blog/wp-content/uploads/2023/11/css-is-where-500x254.jpg)