18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSS 擬似クラス targetについて

Posted at

:targetはリンク先となる要素に対してスタイルを適用できます。

sample

<ul>
  <li><a href="#content1">content1の背景色と文字色を変えます</a></li>
  <li><a href="#content2">content2の文字を大きくします</a></li>
  <li><a href="#box">boxを丸めます</a></li>
</ul>
<div id="content1">content1</div>
<div id="content2">content2</div>
<div id="box">box</div>


#content2{
  font-size: 16px;
  transition: 1s;
}

#box{
  width: 200px;
  height: 200px;
  background: skyblue;
  transition: 2s;
}

/* リンク先となる要素に対してスタイル適用 */
#content1:target{
  background: blue;
  color: #fff;
}

#content2:target{
  font-size: 32px;
}

#box:target{
  border-radius: 100%;
}

transitionを使えばアニメーションもできます。

動作確認

対応ブラウザ

caniuse
http://caniuse.com/#search=%3Atarget

18
18
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
18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?