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 1 year has passed since last update.

ddにfirst-childが効かない

Last updated at Posted at 2022-12-20

やりたいこと

dl(説明リストタグ)の一番上のddタグに上のボーダーつけたい。

<div class="news">
<h2>News</h2>
<dl>
  <dt>2020.XX.XX</dt>
  <dd>デザイン雑誌「XXXXXX Vol.11』に掲載していただきました。</dd> ←このddに上のボーターをつけたい
    ~~ 中略 〜〜
</dl>
</div>

スクリーンショット 2022-12-20 10.55.22.png

失敗したコード

dt:first-child,
dd:first-child { ← これが効いていない
  border-top: 1px solid #c8c8c8;
}

原因

first-childは全体のタグを含めた中の要素からfirst(1番目)という指定になるみたいです。

<div class="news">
<h2>News</h2>
<dl>
  <dt>2020.XX.XX</dt> ← これが1番目!
  <dd>デザイン雑誌「XXXXXX Vol.11』に掲載していただきました。</dd> ←1番目じゃないのでfirst-childが適用されない
    ~~ 中略 〜〜
</dl>
</div>

対処法

dt:first-child,
dt:first-child + dd {
  border-top: 1px solid #c8c8c8;
}

「+」は、指定した要素に隣接する要素にスタイルを適用します。
これでdt:first-childから隣接するddにスタイルを適用できます。

スクリーンショット 2022-12-20 10.53.35.png

0
0
1

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?