:notを使って「親/先祖が〜〜でない」要素を示すCSSを書く
:not(.parent) > .target :notを親/先祖に対して使う タイトルの通り、「親/先祖が〜〜でない」要素を示すCSSを:notを使用して書く。
まず以下のようなHTML/CSSがあるとする。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> :not(.parent) > .target { background-color: aquamarine; } </style> </head> <body> <div class="parent">親 <div class="target">親の中にいる</div> </div> <div class="target">親の中にいない</div> </body> </html> 親の中にいるdivの背景は塗られず、親の中にいないdivの背景だけが塗られることになる