简体   繁体   English

使用多个“>”CSS选择器

[英]Using multiple “>” CSS selectors

Given this html : 给出这个HTML:

<table id="my-table">
  <tr>
    <td>
       I want to apply my style to this
    </td>
    <td>
      <table>
        <tr>
          <td>
            But not to this
          </td>
        </tr>
      </table>  
    </td>
  </tr>
</table>

I would like to apply style to the cells that are first level children of the table. 我想将样式应用于作为表的第一级子元素的单元格。

I thought I could use this : 我以为我可以用这个:

#my-table > tr > td {
    color: #ff0000;
}

... But it doesn't work. ......但它不起作用。 Is it because you can't use multiple > selectors ? 是因为你不能使用多个>选择器吗? How can I do it ? 我该怎么做 ?

There are two aspects to what's going on: 发生了什么有两个方面:

  1. The browser will insert a tbody element if you don't include one (or at least, most do, most of the time; I always use an explicit one, so I don't know the edge cases), and so even if you don't have it in your HTML, you need it in your selector if you're using > (the child combinator). 如果你不包含一个tbody元素,那么浏览器会插入一个tbody元素(或者至少,大多数情况下都是这样;我总是使用一个明确的元素,所以我不知道边缘情况),所以即使你如果您正在使用> (子组合子),则在您的HTML中没有它,您需要在选择器中使用它。 That would change your selector to #my-table > tbody > tr > td . 这会将您的选择器更改为#my-table > tbody > tr > td (I advocate always including tbody explicitly in the HTML, just to avoid confusion and edge cases.) (我主张在HTML中明确包含tbody ,以避免混淆和边缘情况。)

  2. The table inside the td inherits its color from the td it's inside. 里面的表格td继承其颜色td这里面。 So although your selector targets the correct elements, their descendant elements inherit the color. 因此,尽管您的选择器以正确的元素为目标,但它们的后代元素会继承颜色。

You can work around that by giving an explicit color to #my-table td elements, and then the special color only to #my-table > tbody > tr > td elements. 您可以通过为#my-table td元素提供明确的颜色来解决这个问题,然后只为#my-table > tbody > tr > td元素提供特殊颜色。

Example (note the tbody in the HTML and also in the selector): 示例(请注意HTML中的tbody以及选择器中的tbody ):

 #my-table td { color: black; } #my-table > tbody > tr > td { color: #ff0000; } 
 <table id="my-table"> <tbody> <tr> <td> I want to apply my style to this </td> <td> <table> <tr> <td> But not to this </td> </tr> </table> </td> </tr> </tbody> </table> 

In a comment you've said you don't control the inner table. 在评论中你说过你不控制内表。 If you control the outer table, you can solve this by just putting a class on the cells you want to apply the rule to, and then have the rule only apply to td s with that class: 如果您控制外部表,则可以通过在要应用规则的单元格上放置一个类来解决此问题,然后将该规则仅应用于具有该类的td

Example (note the tbody in the HTML and also in the selector): 示例(请注意HTML中的tbody以及选择器中的tbody ):

 #my-table > tbody > tr > td.first { color: #ff0000; } 
 <table id="my-table"> <tbody> <tr> <td class="first"> I want to apply my style to this </td> <td> <table> <tr> <td> But not to this </td> </tr> </table> </td> </tr> </tbody> </table> 

Hi now you can try to this way 嗨,现在你可以尝试这种方式

 #my-table > tbody> tr > td { color: #ff0000; } #my-table td{color:#000;} 
 <table id="my-table"><tbody> <tr> <td> I want to apply my style to this </td> <td> <table><tbody> <tr> <td> But not to this </td> </tr></tbody> </table> </td> </tr></tbody> </table> 

The tag is used to group the body content in an HTML table. 该标记用于对HTML表格中的正文内容进行分组。

The element is used in conjunction with the and elements to specify each part of a table (body, header, footer). 该元素与和元素一起使用以指定表的每个部分(正文,页眉,页脚)。

Browsers can use these elements to enable scrolling of the table body independently of the header and footer. 浏览器可以使用这些元素来独立于页眉和页脚滚动表体。 Also, when printing a large table that spans multiple pages, these elements can enable the table header and footer to be printed at the top and bottom of each page. 此外,当打印跨越多个页面的大表时,这些元素可以使表格页眉和页脚打印在每页的顶部和底部。

The tag must be used in the following context: As a child of a element, after any , , and elements. 标记必须在以下上下文中使用:作为元素的子元素,在any,和元素之后。

more about tbody 更多关于tbody

As far as I see it you need the :first-child as well, otherwise you are stil hitting the last TD, if you want a border on it: 据我所知,你需要:第一个孩子,否则如果你想要一个边框,你就会击中最后一个TD:

  #my-table > tbody > tr > td:first-child { color: #ff0000; border: 1px solid black; } 
 <table id="my-table"> <tr> <td> I want to apply my style to this </td> <td> <table> <tr> <td> But not to this </td> </tr> </table> </td> </tr> </table> 

color has the property that it's being applied to all of it's childs. color具有应用于所有孩子的属性。 Therefor you will need to limit it. 因此,您需要限制它。 You can do this with > and :nth-child(n) 您可以使用>:nth-child(n)

#my-table > tbody > tr > td:nth-child(1) {
    color: #ff0000;
}

Your HTML should have a tbody but it might not be necessary, browsers will create them for you, but it's advised to use tbody yourself. 你的HTML应该有一个tbody但可能没有必要,浏览器会为你创建它们,但是建议你自己使用tbody

You can modify this if your tables are getting larger.. for example with using formulas like 3n+1 , odd/even etc.. also you can use multiple spaces or > and element tags to specify all your needs. 如果你的表变得更大,你可以修改它...例如使用3n+1odd/even等公式。你也可以使用多个空格或>和元素标签来指定你的所有需求。 Depending on what you want. 取决于你想要的。

More info about nth-child() here 有关更多信息nth-child()here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM