0
0

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 3 years have passed since last update.

【備忘録】:nth-of-type(n)の使用例

Posted at

はじめに

Sassを使った実装の時によく使われていたので、備忘録として残します。

 目次

  • :nth-of-type(n)とは
  • 使用例
  • 参考サイト

 E:nth-of-type(n)とは

ターゲット要素(E)と同じタイプ(タグ)で兄弟関係にある要素に対してスタイルを適用します。

使用例

①html

<!DOCTYPE html>
<!-- ここからがhtmlタグ/ルート要素 -->
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div>
        <h1>果物</h1>
        <p>
            りんご
        </p>
        <p>
            バナナ
        </p>
        <div>
            トマト?
        </div>
        <p>
            キウイ
        </p>
        <p>
            パイナップル
        </p>
        <h1>fruits</h1>
        <p>
            オレンジ
        </p>
        <p>
            みかん
        </p>
        <p>
            ぶどう
        </p>
        <p>
            かき
        </p>
    </div>

</body>

</html>

②CSS

p:nth-of-type(odd) {
    color: white;
    background-color: black;
}

p:nth-of-type(n + 8) {
    /* p要素で兄弟関係にある8番目の要素からスタイルを適用。 */
    color: blue;
    background-color: yellowgreen;
}

h1:nth-of-type(2) {
    color: red;
    font-weight: 500;
}

div:nth-of-type(2) {
    /* div要素で兄弟関係にある2番目の要素がないため、スタイルはなし。 */
    color: pink;
    font-size: large;
}

③結果

スクリーンショット 2021-02-11 15.54.57.png

参考サイト

http://www.htmq.com/selector/nth-of-type.shtml
https://developer.mozilla.org/ja/docs/Web/CSS/:nth-of-type

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?