CSSチュートリアル

CSSホーム CSSの紹介 CSS構文 CSSセレクター CSSハウツー CSSコメント CSSの色 CSSの背景 CSSボーダー CSSマージン CSSパディング CSSの高さ/幅 CSSボックスモデル CSSの概要 CSSテキスト CSSフォント CSSアイコン CSSリンク CSSリスト CSSテーブル CSSディスプレイ CSSの最大幅 CSSの位置 CSSZ-index CSSオーバーフロー CSSフロート CSSインラインブロック CSS整列 CSSコンビネータ CSS疑似クラス CSS疑似要素 CSSの不透明度 CSSナビゲーションバー CSSドロップダウン CSS画像ギャラリー CSS画像スプライト CSS AttrSelectors CSSフォーム CSSカウンター CSSWebサイトのレイアウト CSSユニット CSSの特異性 CSS!重要 CSS数学関数

CSS Advanced

CSSの丸みを帯びたコーナー CSSボーダー画像 CSSの背景 CSSの色 CSSカラーキーワード CSSグラデーション CSSシャドウ CSSテキスト効果 CSSWebフォント CSS2D変換 CSS3D変換 CSSトランジション CSSアニメーション CSSツールチップ CSSスタイルの画像 CSS画像反射 CSSオブジェクト適合 CSSオブジェクトの位置 CSSマスキング CSSボタン CSSページ付け CSSの複数の列 CSSユーザーインターフェイス CSS変数 CSSボックスのサイズ設定 CSSメディアクエリ CSSMQの例 CSSFlexbox

CSSレスポンシブ

RWDイントロ RWDビューポート RWDグリッドビュー RWDメディアクエリ RWD画像 RWDビデオ RWDフレームワーク RWDテンプレート

CSSグリッド

グリッドイントロ グリッドコンテナ グリッドアイテム

CSS SASS

SASSチュートリアル

CSSの

CSSテンプレート CSSの例 cssクイズ CSS演習 CSS証明書

CSSリファレンス

CSSリファレンス CSSセレクター CSS関数 CSSリファレンスオーラル CSSWebセーフフォント CSSアニメーション可能 CSSユニット CSSPX-EMコンバーター CSSの色 CSSの色の値 CSSのデフォルト値 CSSブラウザのサポート

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ます colorbackgroundcursoroutline

次の例では、選択したテキストを黄色の背景に赤にします。

::selection {
  color: red;
  background: yellow;
}

エクササイズで自分をテストする

エクササイズ:

段落の最初の行の背景色を赤に設定します。

<style>
 {
  background-color: red;
}
</style>

<body>

<p class="intro">
In my younger and more vulnerable years
my father gave me some advice that I've
been turning over in my mind ever since.
'Whenever you feel like criticizing anyone,' he told me,
'just remember that all the people in this world
haven't had the advantages that you've had.'
</p>

</body>


すべての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