简体   繁体   English

css选择器将规则应用于多个类似的元素类

[英]css selectors to apply rules to multiple class of similar element

I have a simple table 我有一张简单的桌子

<tr>
     <td class="first">I am the first</td>
     <td class="second">You are the second</td>
     <td class="third">He is third</td>
     <td class="fourth">Someone else is fourth</td>
     <td class="fifth">That guy is fifht</td>
     <td class="sixth">Who care's about sixth</td>
</tr>

I want to apply css rules on some of classes in td only. 我想在td中的某些类上应用css规则。 I can write something like- 我可以写一些像 -

td.first, td.fourth, td.fifth
{
    color:purple;
}

This works. 这有效。 Or i can use classes. 或者我可以使用课程。 I was wondering is there any efficient/ better way to write selectors in such case. 我想知道在这种情况下是否有任何有效/更好的方法来编写选择器。

My concern: Is browser, is going to look for all class and then search for td for each comma separation. 我关注的是:浏览器是否会查找所有类,然后搜索每个逗号分隔的td。 That means it going to find all three classes and then evaluate tag. 这意味着它将找到所有三个类,然后评估标记。 Is there any way that browser will find all three classes and then matches the tag other than using a single class. 是否有任何方式浏览器将找到所有三个类,然后匹配标记而不是使用单个类。

Addressing Your Concern 解决你的问题

You state: 你说:

My concern: Is browser, is going to look for all td for each comma separation and find the class match. 我担心的是:浏览器是否会为每个逗号分隔查找所有td并找到类匹配。 That means it going to find all td tags three times. 这意味着它将三次找到所有td标签。 If this is true, how can i make browser to search for td tags once and then find class matches. 如果这是真的,我怎么能让浏览器搜索一次td标签,然后找到类匹配。

But that is not how css evaluates, as it goes from right to left . 但这不是css评估的方式, 因为它从右到左 In the case you give: 在你给的情况下:

td.first, td.fourth, td.fifth
{
    color:purple;
}

So it will not search "three times" through td elements. 所以它不会通过td元素搜索“三次”。 Rather, it will match the .first class in your document (where ever it is), then check to see if it is applied to td element, and if so, match. 相反,它将匹配文档中的.first类(无论它在哪里),然后检查它是否应用于td元素,如果是,则匹配。 Then evaluate .fourth , etc. in a similar fashion. 然后以类似的方式评估.fourth等。

So if your concern is iterations through td elements, then your concern is invalid. 因此,如果您关注的是通过td元素的迭代,那么您的担忧是无效的。 Your code is efficient as it is. 您的代码是高效的。

For specific properties, you can create separate classes. 对于特定属性,您可以创建单独的类。 For example, in your case, you can make a class .color and write like this: 例如,在您的情况下,您可以创建一个类.color并像这样写:

<tr>
     <td class="first color">I am the first</td>
     <td class="second">You are the second</td>
     <td class="third">He is third</td>
     <td class="fourth color">Someone else is fourth</td>
     <td class="fifth color">That guy is fifht</td>
     <td class="sixth">Who care's about sixth</td>
</tr>

.color{color:purple;}

You can use the :nth-child property to achieve the same without giving all these different classes to your TDs 您可以使用:nth-​​child属性来实现相同的功能,而无需为TD提供所有这些不同的类

ie: 即:

td:nth-child(1), 
td:nth-child(4), 
td:nth-child(5) {
    color:purple;
}

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

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