【问题标题】:CSS selector for following tags [duplicate]以下标签的CSS选择器[重复]
【发布时间】:2019-10-05 16:39:53
【问题描述】:

我有一些这样的html代码:

<table>
  <tr>
    <td>first line </td>
    <td>second line </td>
    <td>third line </td>
  </tr>
</table>

我只希望“第一行”具有红色。 * 我不能为标签使用内联样式或设置类。我只能使用外部 CSS 文件。

【问题讨论】:

    标签: css css-selectors


    【解决方案1】:

    您可以使用:first-child 选择器。来自文档:

    :first-child CSS 伪类表示一组兄弟元素中的第一个元素。

    演示:

    table tr td:first-child {
      color: red;
    }
    <table>
      <tr>
        <td>first line </td>
        <td>second line </td>
        <td>third line </td>
      </tr>
    </table>

    【讨论】:

      【解决方案2】:

      您可以使用first-of-type

      table td:first-of-type {
        color: green;
      }
      <table>
        <tr>
          <td>first line </td>
          <td>second line </td>
          <td>third line </td>
        </tr>
      </table>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-07
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        相关资源
        最近更新 更多