質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
CSS3

CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Q&A

解決済

1回答

314閲覧

順番指定の要素が思うようにいきません。

niconic73027793

総合スコア215

CSS3

CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

0グッド

0クリップ

投稿2022/04/26 09:16

table に指定した要素を反映させたいのですが、うまくできません。

指定したtableの中の2番目以降の要素の初めのクラスにそれぞれCSSを反映させるために
下記のコードを指定しました。

/順番指定 2番目以降tbody の中の初めの time class だけ paddingを上に23px 付けたい/

tbody:not(:first-child) .time:first-of-type{
padding-top:23px;
background-color:pink;
}
/順番指定 2番目以降tbody の中の初めの price class だけ paddingを上に23px 付けたい/
tbody:not(:first-child) .price:first-of-type{
padding-top:23px;
background-color:pink;
}
すると、not(:first-child) の指定だけ効いていると思うのですが、

2番目以降の初めの time  class だけなく 2番目以降の全ての time class に css が反映されてしまいます。

また、
tbody:not(:first-child) .price:first-of-type
こちらの指定は効いていないように見えます。

どのように直せばよいのでしょうか?

下記がコードにになります。

<table class="menu-list-facial"> <tbody> <tr> <td class="menu" rowspan="4"> <h3 class="parts-title">小顔フェイシャル</h3> </td> </tr> <tr> <td class="time">60分</td><td class="price">&yen;3,000</td> </tr> <tr> <td class="time">90分</td><td class="price">&yen;5,000</td> </tr> <tr> <td class="time">120分</td><td class="price">&yen;7,000</td> </tr> </tbody> <tbody> <tr> <td class="menu" rowspan="3"> <h3 class="parts-title">小顔矯正</h3> </td> </tr> <tr> <td class="time">60分</td><td class="price">&yen;9,000</td> </tr> <tr> <td class="time">90分</td><td class="price">&yen;12,000</td> </tr> </tbody> <tbody> <tr> <td class="menu" rowspan="3"> <h3 class="parts-title">プラズマシャワー</h3> </td> </tr> <tr> <td class="time">30分</td><td class="price">&yen;6,000</td> </tr> <tr> <td class="time">60分</td><td class="price">&yen;10,000</td> </tr> </tbody> </table>
/*------------------------------------------- menu -------------------------------------------*/ tbody{ border-bottom:1px dotted #000; } #menu .parts .reserve-box{ padding:40px 0; } /*facial*/ .menu-list-facial .menu{ width:80%; padding-bottom:13px; margin-top:20px; vertical-align: top; } .menu-list-facial .time{ width:10%; padding-bottom:13px; margin-top:20px; text-align:right; } .menu-list-facial .price{ width:10%; padding-bottom:13px; margin-top:20px; text-align:right; } /*順番指定 2番目以降tbody の中の初めの time class だけ paddingを上に23px 付けたい*/ tbody:not(:first-child) .time:first-of-type{ padding-top:23px; background-color:pink; } /*順番指定 2番目以降tbody の中の初めの price class だけ paddingを上に23px 付けたい*/ tbody:not(:first-child) .price:first-of-type{ padding-top:23px; background-color:pink; }

試した事、 nth-of-type から nth-child に変更

色をつけて css の反映範囲確認

nthを使わずに 検証ツールから直書き

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

2番目以降の初めの time  class だけなく 2番目以降の全ての time class に css が反映されてしまいます。

:first-of-typetype というのはタグ名のことです。「timeクラスの1番目」という意味ではありません。time クラスは <tr>の最初の子<td> すべてに付いているので、すべてにマッチします。

css

1tbody:not(:first-child) tr:nth-of-type(2) .time {

でしょうか。


tbody:not(:first-child) .price:first-of-type
こちらの指定は効いていないように見えます。

こちらも同様で、priceクラスは2番目の子<td>についているので、マッチすることはありません。
同様に、tr:nth-of-type(2) を使いましょう。

投稿2022/04/26 09:28

編集2022/04/26 12:22
int32_t

総合スコア20832

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

niconic73027793

2022/04/26 09:51

回答ありがとうございます。 tbody:not(:first-child) tr:first-of-type .time こちらに変更したのですが , 今度はcssが反映されなくなってしまいました。 tbody:not(:first-child) tr:first-of-type .time { padding-top:23px; background-color:pink; }
int32_t

2022/04/26 12:21

おっと、失礼しました。2番めの <tr> なんですね。 tr:nth-of-type(2) ですね。
niconic73027793

2022/04/27 01:30 編集

#menu tbody:not(:first-child) tr:first-of-type .menu, #menu tbody:not(:first-child) tr:nth-of-type(2) .time, #menu tbody:not(:first-child) tr:nth-of-type(2) .price { padding-top:23px; } このように書いたらできました。 1番目のtbody 以外の  tbody:not(:first-child) は良いのですが、 2番目以降の 初めの time クラスと 初めの priceに cssを適用させたいので tr:first-of-type .time tr:first-of-type .price でよいかとおもいましたが、なぜ tbody:not(:first-child) tr:nth-of-type(2) .time, tbody:not(:first-child) tr:nth-of-type(2) .price これで初めの timeと priceに適用されているか謎です。
int32_t

2022/04/27 02:11 編集

最初のtimeとpriceが2番めの子<tr>に含まれているからです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問