概要
セレクタとは、
要素やその他を選択して
CSSプロパティ値を適用する
HTML要素を伝えるものです。
セレクタと宣言ブロックとを組んだ物を、規則セットと呼びます。
言い方を変えると、セレクタとは宣言ブロックと要素とを紐づける物です。
選択札,選択紐と訳せるのかな。
例示
セレクタの一部を提示しています。
- /* === 基本セレクタ === */
- * { text-decoration: line-through;} /* --- 全称セレクタ --- */
- span { text-decoration: line-through;} /* --- 要素型セレクタ --- */
- .csnClass {text-decoration: line-through;} /* --- クラスセレクタ --- */
- #isnId { text-decoration: line-through;} /* --- IDセレクタ --- */
- [class] { text-decoration: line-through;} /* --- 属性セレクタ --- */
- /* === グループ化セレクタ === */
- a, span {text-decoration: line-through;} /* --- セレクタリスト --- */
- /* === 結合子 === */
- div span { text-decoration: line-through;} /* --- 子孫結合子 --- */
- div > span { text-decoration: line-through;} /* --- 子結合子 --- */
- span ~ span {text-decoration: line-through;} /* --- 一般兄弟結合子 --- */
- div + p { text-decoration: line-through;} /* --- 隣接兄弟結合子 --- */
- /* === 疑似要素 === */
- span::before { content: "≪";}
- span::after { content: "≫";}
- p::first-letter {font-size: 2.0em;}
- *::first-line { text-decoration: line-through;}
- /* === 擬似クラス === */
- a:active { text-decoration: underline;}
- a:hover { text-decoration: underline;}
- a:focus { text-decoration: underline;}
- div > *:first-child { text-decoration: line-through;}
- div > *:last-child { text-decoration: line-through;}
- div > *:nth-child(2) { text-decoration: line-through;}
- div > *:nth-last-child(2) { text-decoration: line-through;}
- div > p:first-of-type { text-decoration: line-through;}
- div > p:last-of-type { text-decoration: line-through;}
- div > p:nth-of-type(2) { text-decoration: line-through;}
- div > p:nth-last-of-type(2) {text-decoration: line-through;}
- div > p:only-child { text-decoration: line-through;}
- div > p:only-of-type { text-decoration: line-through;}
- div > p:not([class]) { text-decoration: line-through;}
- p:empty {background: #ff0000;}
- a:link { text-decoration: underline;}
- p:target {background: #ff0000;}
- /* === 論理積 (AND) === */
- p[lang] {text-decoration: line-through;}
基本セレクタの説明
全称セレクタ
- <span>
テキスト </span><br> - <a href="#APHNEXT">
リンク </a>
- * {text-decoration: line-through;}
*(アスタリスク) : すべての種類の要素を選択します。
要素型セレクタ
- <span>
テキスト </span><br> - <a href="#APHNEXT">
リンク </a>
- span {text-decoration: line-through;}
要素名で要素を選択します。
クラスセレクタ
- <span class="csnClass">
テキスト1 </a> - <span>
テキスト2 </span><br>
- .csnClass {text-decoration: line-through;}
class属性の値で要素を選択します。
class属性には複数の値が設定できます。なので属性セレクタで表すと [class~=class名] と同等となります。
テキスト2
IDセレクタ
- <span id="isnId">
テキスト1 </a> - <span>
テキスト2 </span><br>
- #isnId {text-decoration: line-through;}
id属性の値で要素を選択します。
id属性には単一の値だけが設定できます。なので属性セレクタで表すと [id=ID名] と同等となります。
テキスト2
属性セレクタ
- <span class=
"csnSample350"> テキスト1 </a> - <span>
テキスト2 </span><br>
- [class] {text-decoration: line-through;}
指定された属性の存在や値で要素を選択します。
テキスト2
- [lang] : lang属性の存在
- [lang="en"] : lang属性の値に "en" だけ存在
- [lang|="en"] : lang属性の値に "en" と一致するか、直後にハイフンが続く値(例えば"en-US")が存在
- [lang^="en"] : lang属性の値に "en" で始まる値が存在
- [lang$="en"] : lang属性の値に "en" で終わる値が存在
- [lang*="en"] : lang属性の値に "en" を文字列中に含む値が存在
- [lang~="en"] : lang属性の値に "en" と一致する値が存在
グループ化セレクタの説明
セレクタリスト
- <span>
テキスト </span><br> - <a href="#APHNEXT">
リンク </a>
- a, span {text-decoration: line-through;}
, (カンマ) はグループ化の手段であり、一致するすべてを選択します。
結合子の説明
子孫結合子
- <div>
<span> テキスト</span> <br> - <a href="#APHNEXT">
リンク<span>1</span> </a></div>
- div span {text-decoration: line-through;}
(空白) は、第一のセレクタの子孫にあたる第二のセレクタを選択します。
セレクタというかCSS の文は複数行に渡ってひとつの記述ができる為に、グループ化の意図の行末の, (カンマ)の書き忘れが子孫結合子となりうるので注意が必要です。
子結合子
- <div><span>
テキスト </span><br> - <a href="#APHNEXT">
リンク<span>1</span> </a></div>
- div > span {text-decoration: line-through;}
> (大なり) は、第一のセレクタの子にあたる第二のセレクタを選択します。
一般兄弟結合子
- <span>
テキスト </span><br> - <a href="#APHNEXT">
リンク<span>1</span> </a><br> - <span>
テキスト </span>
- span ~ span {text-decoration: line-through;}
~ (チルダ) は、第一のセレクタと同じ親要素の子同士であって、第一のセレクタの後にある第二のセレクタを選択します。
隣接兄弟結合子
- <div>
テキスト<p>子供</p> </div> - <p>
兄弟 </p> - <p>
兄弟 </p>
- div + p {text-decoration: line-through;}
+ (プラス) は、第一のセレクタと同じ親要素の子同士であって、第一のセレクタの直後にある第二のセレクタを選択します。
直後であって直近ではないので、間に別の要素があった場合は該当しない。
子供
兄弟
兄弟
疑似要素の説明
疑似要素の一部を提示しています。
::before
- <span>
テキスト </span>
- span::before {content:"≪";}
::before は、選択した要素の最初の子要素として擬似要素を作成します。
::after
- <span>
テキスト </span>
- span::after {content:"≫";}
::after は、選択した要素の最後の子要素として擬似要素を作成します。
::first-letter
- <p>
テキスト </p>
- p::first-letter {font-size: 2.0em;}
::first-letter は、ブロックレベル要素の最初の行の最初の文字に装飾を適用します。
テキスト
::first-line
- <p>
テキストをすこしながめに </p> - <span>
テキストをすこしながめに </span>
- *::first-line { text-decoration: line-through;}
::first-line は、ブロックレベル要素の最初の行に装飾を適用します。
テキストをすこしながめに
テキストをすこしながめに擬似クラスの説明
擬似クラスの一部を提示しています。
- a:active { text-decoration: underline;}
- a:hover { text-decoration: underline;}
- a:focus { text-decoration: underline;}
- div > *:first-child { text-decoration: line-through;}
- div > *:last-child { text-decoration: line-through;}
- div > *:nth-child(2) { text-decoration: line-through;}
- div > *:nth-last-child(2) { text-decoration: line-through;}
- div > p:first-of-type { text-decoration: line-through;}
- div > p:last-of-type { text-decoration: line-through;}
- div > p:nth-of-type(2) { text-decoration: line-through;}
- div > p:nth-last-of-type(2) {text-decoration: line-through;}
- div > p:only-child { text-decoration: line-through;}
- div > p:only-of-type { text-decoration: line-through;}
- div > p:not([class]) { text-decoration: line-through;}
- p:empty {background: #ff0000;}
- a:link { text-decoration: underline;}
- p:target {background: #ff0000;}
論理積 (AND)
「Aであり、かつBである」としたい場合、セレクタを空白なく続けてABと書きます。
要素同士のAND条件はそもそも発生しないので、AとBの区分けは大丈夫と思われます。
「Aであり、かつAである」(AA)という記述も出来るので、詳細度を調整するのに使えます。
- <p>テキスト1</p>
- <div lang="ja" >テキスト2</div>
- <p lang="ja" >テキスト3</p>
- p[lang] {text-decoration: line-through;}
「p要素であり、かつlang属性がある」要素を選択します。
テキスト1
テキスト3