给具有特定类的表格的第一行添加边框

huangapple go评论53阅读模式
英文:

Add border to first row in a table with a specific class

问题

我想在具有类“relegated”的行上方放置一个红色边框。

我知道我需要在子级的<td>标签上应用边框,而不是在<tr>上应用边框。

我已经尝试过:

.relegated:nth-child(1) td {
    border-top: 1px dashed red;
}

但我什么都没得到。执行以下代码:

tr:nth-child(7) td {
    border-top: 1px dashed red;
}

确实有效。但我想根据类来选择目标(因为这是一个联赛积分榜,底部分区不能降级)。

英文:

I have the following table

    &lt;table&gt;
        &lt;tr&gt;
            &lt;td&gt;1st&lt;/td&gt;
            &lt;td&gt;Anne Wright &amp;amp; Steve Wright&lt;/td&gt;
            &lt;td&gt;510 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2nd&lt;/td&gt;
            &lt;td&gt;Ian Bruce &amp;amp; Lyn Hilton&lt;/td&gt;
            &lt;td&gt;472 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3rd&lt;/td&gt;
            &lt;td&gt;Anne Morris &amp;amp; Lucy Pathan&lt;/td&gt;
            &lt;td&gt;469 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;4th&lt;/td&gt;
            &lt;td&gt;Mike Ayers &amp;amp; Mike Gould&lt;/td&gt;
            &lt;td&gt;426 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;5th&lt;/td&gt;
            &lt;td&gt;Alison Nichols &amp;amp; Simon Stokes&lt;/td&gt;
            &lt;td&gt;409 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;6th&lt;/td&gt;
            &lt;td&gt;Brian Stockdale &amp;amp; Sheila Stockdale&lt;/td&gt;
            &lt;td&gt;406 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;relegated&quot;&gt;
            &lt;td&gt;7th&lt;/td&gt;
            &lt;td&gt;Pauline Dignan &amp;amp; Val Rees&lt;/td&gt;
            &lt;td&gt;349 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;relegated&quot;&gt;
            &lt;td&gt;8th&lt;/td&gt;
            &lt;td&gt;Alan Lord &amp;amp; John Thompson&lt;/td&gt;
            &lt;td&gt;319 VPs&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;

I want to put a red border above the row that has the class "relegated".

I know that I need to apply the border on the child &lt;td&gt; tags and not the &lt;tr&gt;

I have tried

        .relegated:nth-child(1) td {
            border-top: 1px dashed red;
        }

but I get nothing. Doing

        tr:nth-child(7) td {
            border-top: 1px dashed red;
        }

does work. But I want to target it based on the class (because this is a league table and you can't get relegated from the bottom division).

答案1

得分: 1

以下是您要翻译的内容:

tr:nth-child(1 of .relegated) td{
  border-top: 1px dashed red;
}
<table id="table">
  <tbody>
      <tr>
            <td>1st</td>
            <td>Anne Wright &amp; Steve Wright</td>
            <td>510 VPs</td>
        </tr>
        <tr>
            <td>2nd</td>
            <td>Ian Bruce &amp; Lyn Hilton</td>
            <td>472 VPs</td>
        </tr>
        <tr>
            <td>3rd</td>
            <td>Anne Morris &amp; Lucy Pathan</td>
            <td>469 VPs</td>
        </tr>
        <tr>
            <td>4th</td>
            <td>Mike Ayers &amp; Mike Gould</td>
            <td>426 VPs</td>
        </tr>
        <tr>
            <td>5th</td>
            <td>Alison Nichols &amp; Simon Stokes</td>
            <td>409 VPs</td>
        </tr>
        <tr>
            <td>6th</td>
            <td>Brian Stockdale &amp; Sheila Stockdale</td>
            <td>406 VPs</td>
        </tr>
        <tr class="relegated">
            <td>7th</td>
            <td>Pauline Dignan &amp; Val Rees</td>
            <td>349 VPs</td>
        </tr>
        <tr class="relegated">
            <td>8th</td>
            <td>Alan Lord &amp; John Thompson</td>
            <td>319 VPs</td>
        </tr>     
  </tbody>      
</table>
英文:

use that.
:nth-child(1 of className)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

tr:nth-child(1 of .relegated) td{
  border-top: 1px dashed red;
}

<!-- language: lang-html -->

&lt;table id=&quot;table&quot;&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
            &lt;td&gt;1st&lt;/td&gt;
            &lt;td&gt;Anne Wright &amp;amp; Steve Wright&lt;/td&gt;
            &lt;td&gt;510 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2nd&lt;/td&gt;
            &lt;td&gt;Ian Bruce &amp;amp; Lyn Hilton&lt;/td&gt;
            &lt;td&gt;472 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3rd&lt;/td&gt;
            &lt;td&gt;Anne Morris &amp;amp; Lucy Pathan&lt;/td&gt;
            &lt;td&gt;469 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;4th&lt;/td&gt;
            &lt;td&gt;Mike Ayers &amp;amp; Mike Gould&lt;/td&gt;
            &lt;td&gt;426 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;5th&lt;/td&gt;
            &lt;td&gt;Alison Nichols &amp;amp; Simon Stokes&lt;/td&gt;
            &lt;td&gt;409 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;6th&lt;/td&gt;
            &lt;td&gt;Brian Stockdale &amp;amp; Sheila Stockdale&lt;/td&gt;
            &lt;td&gt;406 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;relegated&quot;&gt;
            &lt;td&gt;7th&lt;/td&gt;
            &lt;td&gt;Pauline Dignan &amp;amp; Val Rees&lt;/td&gt;
            &lt;td&gt;349 VPs&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;relegated&quot;&gt;
            &lt;td&gt;8th&lt;/td&gt;
            &lt;td&gt;Alan Lord &amp;amp; John Thompson&lt;/td&gt;
            &lt;td&gt;319 VPs&lt;/td&gt;
        &lt;/tr&gt;     
  &lt;/tbody&gt;      
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 06:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定