CSS疑似要素
疑似要素とは何ですか?
CSS疑似要素は、要素の指定された部分のスタイルを設定するために使用されます。
たとえば、次の目的で使用できます。
- 要素の最初の文字または行のスタイルを設定する
- 要素のコンテンツの前または後にコンテンツを挿入する
構文
疑似要素の構文:
selector::pseudo-element {
property: value;
}
::最初の行の疑似要素
::first-line
疑似要素は、テキストの最初の行に特別なスタイルを追加するために使用されます。
次の例では、すべての<p>要素のテキストの最初の行をフォーマットします。
例
p::first-line
{
color: #ff0000;
font-variant: small-caps;
}
注:::first-line
疑似要素は、ブロックレベルの要素にのみ適用できます。
次のプロパティが::first-line
疑似要素に適用されます。
- フォントのプロパティ
- 色のプロパティ
- 背景のプロパティ
- 単語間隔
- 文字間隔
- テキスト装飾
- 垂直整列
- テキスト変換
- 行の高さ
- クリア
二重コロン表記に注意してください- ::first-line
対
:first-line
二重コロンは、CSS3の疑似要素の単一コロン表記を置き換えました。これは、疑似クラス
と疑似要素を区別するためのW3Cからの試みでした。
シングルコロン構文は、CSS2とCSS1の疑似クラスと疑似要素の両方に使用されました。
下位互換性のために、CSS2およびCSS1疑似要素にはシングルコロン構文を使用できます。
::最初の文字の疑似要素
::first-letter
疑似要素は、テキストの最初の文字に特別なスタイルを追加するために使用されます。
次の例では、すべての<p>要素のテキストの最初の文字をフォーマットします。
例
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
注:::first-letter
疑似要素は、ブロックレベルの要素にのみ適用できます。
次のプロパティは、:: first-letter疑似要素に適用されます。
- フォントのプロパティ
- 色のプロパティ
- 背景のプロパティ
- マージンプロパティ
- パディングプロパティ
- ボーダープロパティ
- テキスト装飾
- vertical-align(「float」が「none」の場合のみ)
- テキスト変換
- 行の高さ
- 浮く
- クリア
疑似要素とHTMLクラス
疑似要素は、HTMLクラスと組み合わせることができます。
例
p.intro::first-letter {
color: #ff0000;
font-size: 200%;
}
上記の例では、class = "intro"の段落の最初の文字が、赤でより大きなサイズで表示されます。
複数の疑似要素
複数の疑似要素を組み合わせることもできます。
次の例では、段落の最初の文字がxxの大きなフォントサイズの赤になります。最初の行の残りの部分は青色で、スモールキャップスになっています。段落の残りの部分は、デフォルトのフォントサイズと色になります。
例
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
p::first-line
{
color: #0000ff;
font-variant: small-caps;
}
CSS-:: before疑似要素
::before
疑似要素を使用して、要素のコンテンツの前にコンテンツを挿入できます。
次の例では、各<h1>要素のコンテンツの前に画像を挿入します。
例
h1::before
{
content: url(smiley.gif);
}
CSS-:: after疑似要素
::after
疑似要素を使用して、要素のコンテンツの後にコンテンツを挿入できます。
次の例では、各<h1>要素のコンテンツの後に画像を挿入します。
例
h1::after
{
content: url(smiley.gif);
}
CSS-:: marker疑似要素
::marker
疑似要素は、リストアイテムのマーカーを選択します。
次の例では、リストアイテムのマーカーのスタイルを設定します。
例
::marker {
color: red;
font-size: 23px;
}
CSS-:: selection疑似要素
::selection
疑似要素は、ユーザーが選択した要素の部分と一致します。
次のCSSプロパティは、、、、およびに
適用でき::selection
ます
。color
background
cursor
outline
次の例では、選択したテキストを黄色の背景に赤にします。
例
::selection {
color: red;
background: yellow;
}
すべてのCSS疑似要素
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert something after the content of each <p> element |
::before | p::before | Insert something before the content of each <p> element |
::first-letter | p::first-letter | Selects the first letter of each <p> element |
::first-line | p::first-line | Selects the first line of each <p> element |
::marker | ::marker | Selects the markers of list items |
::selection | p::selection | Selects the portion of an element that is selected by a user |
すべてのCSS疑似クラス
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
:first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects <input> elements with a value within a specified range |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
:last-child | p:last-child | Selects every <p> elements that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects <input> elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
:read-only | input:read-only | Selects <input> elements with a "readonly" attribute specified |
:read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
:required | input:required | Selects <input> elements with a "required" attribute specified |
:root | root | Selects the document's root element |
:target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |