【问题标题】:How can I target <td> and <th> except for the first and last <tr>?除了第一个和最后一个 <tr> 之外,我如何定位 <td> 和 <th>?
【发布时间】:2013-02-20 00:11:19
【问题描述】:

我如何定位某些子元素除了,其中父元素是父元素的第一个或最后一个子元素?

请参阅我的 CSS 中的 cmets 以更好地理解我的意思。

CSS:

/* Always apply */
tr th, tr td
{
    text-align: left;
    vertical-align: top;
}

/* Apply to all except first <tr> */
tr th, tr td
{
    padding-top: 5px;
}

/* Apply to all except last <tr> */
tr th, tr td
{
    border-bottom: 1px solid #c4c4c4;
    padding-bottom: 5px;
}

HTML:

<table>
    <tr>
        <th>Born</th>
        <td><time datetime="1986-11-05">5<sup>th</sup> November 1986</time></td>
    </tr>
    <tr>
        <th>Gender</th>
        <td>Male</td>
    </tr>
    <tr>
        <th>Sports</th>
        <td>Football<br />Tennis</td>
    </tr>
    <tr>
        <th>Teams</th>
        <td>Liverpool FC<br />Spain FC</td>
    </tr>
</table>

【问题讨论】:

  • 为什么这个问题有 3 票反对?是不是因为所有者的修改大大改变了问题的含义?

标签: css css-selectors


【解决方案1】:

嗯,您对所链接的问题有一个解决方案。使用:not() 伪类:

/* Apply to all except first <tr> */
tr:not(:first-child) th, tr:not(:first-child) td{
    padding-top: 5px;
}

/* Apply to all except last <tr> */
tr:not(:last-child) th, tr:not(:last-child) td{
    border-bottom: 1px solid #c4c4c4;
    padding-bottom: 5px;
}

fiddle

(但这在 IE 8 中不起作用)


IE 7+ 的替代方案,如果边框样式可以更改:
/* Apply to all */
tr th, tr td{
    border-top: 1px solid #c4c4c4;
    padding-top: 5px;
}

/* Remove styles from first */
tr:first-child th, tr:first-child td{
    border-top: 0;
    padding-top: 0;
}

/* Apply to all but first */
tr + tr th, tr + tr td{
    border-top: 1px solid #c4c4c4;
    padding-top: 5px;
}

(但不确定 IE 7 是否支持 +

【讨论】:

  • 我想使用兄弟组合子。
  • 只有在您知道表中TRs 的数量时才能这样做。你呢?
  • tr + tr th, tr + tr td{ padding-top: 5px; } 效果很好!我将把它用于“除了第一个”场景。正如您所指出的,反向操作是不切实际的。我将使用:not 来处理“除最后一个”场景。非常感谢您的回答!
  • 你知道你可以添加border-top,然后你只需要从在IE 7中工作的first-child中删除它? Here's is what I'm talking about
  • 接受!最后一段代码很完美!仅供参考,+ 是我所指的兄弟组合子,并且 与 IE7 兼容。
【解决方案2】:
tr th,
tr td
    {} /* always apply */

tr:first-child th,
tr:first-child td
    {} /* apply only to td and th of the first tr */

tr:last-child th,
tr:last-child td
    {} /* apply only to td and th of the last tr */

【讨论】:

  • 这与我想要实现的目标相反。我正在尝试设置所有 except 第一个和最后一个孩子的样式。不只是第一个和最后一个孩子。
  • 不是,先定义基本的css,然后用first-child和last-child覆盖
  • 这也适用于较旧但仍在使用的浏览器(如 ie7 和 ie8)
  • ie7 和 ie8 不支持 :not()
  • 我知道你现在得到了什么,只是因为不包含样式而有点不清楚。不幸的是,当我尝试从这个答案中删除我的反对票时,我收到了一条错误消息。聊天中的另一位用户添加了赞成票,以平衡我无法删除的反对票。
【解决方案3】:
tr:first-child ~ tr:not(:last-child) td,
tr:first-child ~ tr:not(:last-child) th
    {background:red;}

这是你可以用兄弟组合器归档的最好的方法,但它不适用于旧浏览器,因为 :not 并且可能也是 ~

【讨论】:

  • 这应该针对什么?所有除了第一个还是所有除了最后一个?答案的另一半是什么?
  • 我不想要两者,我需要将它们分开。
  • 哈哈,这太不真实了。相信我,你甚至不知道你想要什么。你已经有了两个答案.. 离开这里并标记为版主注意
  • 我想是你不知道我想要什么。我在原始 CSS 源代码中已经非常清楚地说明了我需要分别定位 'all except first' 和 'all except last'!
  • 在什么样的世界中,您尝试使用一些不起作用的代码而不是简单的英文单词来解释您正在尝试做什么?这是您第一次清楚地询问您要做什么,并且您从@OneTrickPony 得到了答案
【解决方案4】:

你可以这样做

tr:not(:first-child) th, tr:not(:first-child) td{
 padding-top:5px;
}
tr:not(:last-child) th, tr:not(:last-child) td{
 border-bottom: 1px solid #c4c4c4;
 padding-bottom: 5px;
}

您还可以为每个类的第一个和最后一个添加一个类,可能类似于firstthlastth。这肯定是最简单的解决方案。所有浏览器都支持它

此外,正如我们所说,您可以定义法线,然后为第一个和最后一个应用不同的样式,尽管这不是一种很好的完成方法

tr th, tr td{
 padding: 5px 0; /*I would combine the padding for top and bottom for a more condensed and semantically correct markup*/

 border-bottom: 1px solid #c4c4c4;
}
tr:first-child th, tr:first-child td{
 padding:top: 0;
}
tr:last-child th, tr:last-child td{
 border-bottom:0;
 padding-bottom:0

【讨论】:

  • 这是一个无效的答案吗?它完成了目标
  • 这是一个有效的答案,像我的一样,没有理由投反对票
  • @Danny 您的问题的答案位于您在提出的问题中发布的问题的链接中
  • 我已删除该链接。它不像我想的那样。另外,这个答案是无效的。我需要分别定位“除第一个之外的所有内容”和“除最后一个之外的所有内容”。
  • @Danny 然后将它们分开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2012-05-11
  • 2014-08-19
  • 2012-07-10
  • 1970-01-01
  • 2011-11-05
  • 2012-09-10
相关资源
最近更新 更多