::after (:after)
        
        
          
                Baseline
                
                  Widely available
                
                
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
CSS において ::after は、選択した要素の最後の子要素として擬似要素を生成します。よく content プロパティを使用して、要素に装飾的な内容を追加するために用いられます。この要素は既定でインラインです。
/* リンクの後に矢印を追加 */
a::after {
  content: "→";
}
試してみましょう
a::after {
  content: " (" attr(href) ")";
}
.dead-link {
  text-decoration: line-through;
}
.dead-link::after {
  content: url("/shared-assets/images/examples/warning.svg");
  display: inline-block;
  width: 12px;
  height: 12px;
}
<p>
  The sailfish is named for its sail-like dorsal fin and is widely considered
  the fastest fish in the ocean.
  <a href="https://en.wikipedia.org/wiki/Sailfish"
    >You can read more about it here</a
  >.
</p>
<p>
  The red lionfish is a predatory scorpionfish that lives on coral reefs of the
  Indo-Pacific Ocean and more recently in the western Atlantic.
  <a href="" class="dead-link">You can read more about it here</a>.
</p>
メモ:
::before および ::after によって作成される擬似要素は要素の整形ボックスに含まれるため、 <img> や <br> のような置換要素には適用されません。
構文
Error: could not find syntax for this item例
>シンプルな使い方
2 つのクラスを作成しましょう。 1 つはつまらない段落で 1 つは楽しい段落です。これらのクラスを使用して、段落の最後に擬似要素を追加することができます。
HTML
<p class="boring-text">古くつまらないテキストです。</p>
<p>つまらなくも楽しくもないふつうのテキストです。</p>
<p class="exciting-text">MDN への協力は簡単で楽しいものです。</p>
CSS
.exciting-text::after {
  content: " <- 楽しい!";
  color: green;
}
.boring-text::after {
  content: " <- ツマラナイ!";
  color: red;
}
結果
装飾の例
content プロパティ内の文字列や画像は、大体思う通りに整形することができます。
HTML
<span class="ribbon">このテキストの後のオレンジのボックスを見てください。</span>
CSS
.ribbon {
  background-color: #5bc8f7;
}
.ribbon::after {
  content: "かわいいオレンジのボックスです。";
  background-color: #ffba10;
  border-color: black;
  border-style: dotted;
}
結果
ツールチップ
この例は、 ::after を CSS の attr() 関数と data-descr カスタムデータ属性との組み合わせで使用し、ツールチップを作成しています。 JavaScript は必要ありません。
また、このテクニックを使ってキーボードユーザーに対応することもできます。 tabindex に 0 を追加して、それぞれの span をキーボードフォーカス可能にし、CSS の :focus セレクターを使用することでキーボードフォーカスを可能にします。これは ::before と ::after がいかに柔軟であるかを示しています。しかし、最もアクセスしやすいようにするには、他の方法(例えば details および summary 要素で作成した詳細折りたたみウィジェットがより適していると思われます。
HTML
<p>
  Here we have some
  <span tabindex="0" data-descr="collection of words and punctuation"
    >text</span
  >
  with a few
  <span tabindex="0" data-descr="small popups that appear when hovering"
    >tooltips</span
  >.
</p>
CSS
span[data-descr] {
  position: relative;
  text-decoration: underline;
  color: #00f;
  cursor: help;
}
span[data-descr]:hover::after,
span[data-descr]:focus::after {
  content: attr(data-descr);
  position: absolute;
  left: 0;
  top: 24px;
  min-width: 200px;
  border: 1px #aaaaaa solid;
  border-radius: 10px;
  background-color: #ffffcc;
  padding: 12px;
  color: #000000;
  font-size: 14px;
  z-index: 1;
}
結果
アクセシビリティの考慮
::after 擬似要素を使用してコンテンツを追加することは、スクリーンリーダーからアクセスできなくなる可能性があるため推奨されません。
仕様書
| Specification | 
|---|
| CSS Pseudo-Elements Module Level 4> # generated-content>  | 
            
ブラウザーの互換性
Loading…