这个带有逗号的CSS选择器到底匹配什么? [英] What exactly does this CSS selector with a comma match?

查看:68
本文介绍了这个带有逗号的CSS选择器到底匹配什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CSS选择器有疑问.

I have a question about CSS selectors.

在我的CSS文件中,我有以下代码:

In my CSS file I have the following code:

.table_legenda th, td {
    text-align: left;
    vertical-align: top;
    font-weight: bold;
    color: #76818a;
    border-bottom: 1px solid #76818a;
    border-left: 1px solid #76818a;
    white-space: nowrap;
    overflow: hidden;
}

究竟选择了哪些元素?

我认为它应该在具有类 table_legenda .

I thought that it should select all the th and td elements inside a table having the class table_legenda.

但是,当我对其进行测试时,该样式也会应用到 not 没有 table_legenda 类的其他表中的 td 元素(但还有另一堂课.

However, when I test it, the style also gets applied to td elements inside other tables that do not have the table_legenda class (but do have another class).

为什么会这样?我想念什么?

Why does that happen? What am I missing?

推荐答案

您误解了逗号的优先级.

You are misunderstanding the precedence of the comma.

.table_legenda th, td {}

等效于:

.table_legenda th {}
td {}

执行以下操作:

.table_legenda th {}
.table_legenda td {}


每次使用逗号时,都需要指定完整的选择器:


You need to specify the complete selector each time you have a comma:

.table_legenda th,
.table_legenda td {}


诸如 SASS 之类的预处理工具可以为您提供替代语法:


A preprocessing tool such as SASS can give you alternative syntax:

.table_legenda {
    th, td {}
}

这篇关于这个带有逗号的CSS选择器到底匹配什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆