疑似クラスは、:hover、:active、:last-child などのセレクターのステータスを表します。コロン (:) で始まります。
CSS 疑似クラスの構文は次のとおりです。 -
:pseudo-class{ attribute: /*value*/ }
同様に、疑似要素は、::after などの仮想要素を選択するために使用されます。 、::before、::first -line など。
これらは二重コロン (::) で始まります。
CSS 疑似要素の構文は次のとおりです。 -
::pseudo-element{ attribute: /*value*/ }
次の例は、CSS 疑似クラスと疑似要素のプロパティを示しています。
ライブ デモンストレーション
<!DOCTYPE html> <html> <head> <style> a:hover{ padding: 3%; font-size:1.4em; color: tomato; background: bisque; } </style> </head> <body> <p>You're somebody else</p> <a href=#>Dummy link 1</a> <a href=#>Dummy link 2</a> </body> </html>
これにより、次の結果が生成されます。
ライブデモ
<!DOCTYPE html> <html> <head> <style> p::after { content: " BOOM!"; background: hotpink; } p:last-child { font-size: 1.4em; color: red; } </style> </head> <body> <p>Anymore Snare?</p> <p>Donec in semper diam. Morbi sollicitudin sed eros nec elementum. Praesent eget nisl vitaeneque consectetur tincidunt. Ut molestie vulputate sem, nec convallis odio molestie nec.</p> <p>Hit</p> <p>Pop</p> </body> </html>
これにより、次の結果が生成されます -
以上がCSS の疑似クラスと疑似要素の違いの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。