LoginSignup
20
22

More than 5 years have passed since last update.

HTMLで指定したdata属性をCSSから出力する

Last updated at Posted at 2016-05-26

HTML5のdata属性について

HTML5からタグにdata属性を指定できるようになった。

<div class="hoge" data-hoge="ほげほげ">ディブの中身</div>

CSSからdata属性の中身を出力する

これをCSS側から出力するテクニックがある。

.hoge:before {
  content: attr(data-hoge);
}

こんな感じ。

デモ

まとめ

content: attr(data-xxx);

という記法を使うと、CSSからdata属性の内容を出力することができる。

20
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
22