table.class tr, th 和 table.class tr, th 之间有区别吗?
在很长一段时间里,我都是以这种方式设计我的元素。
table .class tr, th{
}
今天我遇到了一些与 css 相关的疯狂错误,我发现它可能在过去偶然起作用,事实上它所做的不是选择某个类的表,而是选择表后跟某个类的元素。
我有点假设 css 会忽略 table 和 .class 之间的空格。但确实如此吗?
table.class tr, th{
}
这会有所不同吗?我不敢相信我之前没有想到这一点! (不好意思)
谢谢!
For the longest time I was styling my elements in this way.
table .class tr, th{
}
Today I had some insane css related bugs and it occurred to me that it may have worked in the past by accident and in fact what it is doing is not selecting tables of a certain class but selecting tables followed by elements of a certain class.
I kind of assumed that css would just ignore the space between table and .class. But does it?
table.class tr, th{
}
Would this work differently? I can't believe I didn't think about this before! (embarrassed)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
table.class tr
选择此:而
table.class tr
选择此:table .class tr
selects this:While
table.class tr
selects this:您在问题中回答了它:
table .class tr, th
... 在内的
和第.class 元素
中选择tr
表。
table.class tr, th
... 选择table 中的
和tr
类为“class”第
。空间有所不同。
通过空格,您可以在
table
和tr
之间选择另一个元素(可能是thead
或tbody
?)如果没有空格,您将选择一个带有
class
类的table
。顺便说一句,我不确定你想要这个,但是
, th
意味着“也为所有th
提供相同的样式”。You answered it in your question:
table .class tr, th
... selects atr
inside a.class element
inside atable
and ath
.table.class tr, th
... selects atr
inside atable with the class "class"
and ath
.The space makes a difference.
With the space, you are selecting another element in between the
table
and thetr
(perhaps athead
ortbody
?)Without the space, you are selecting a
table
with the classclass
.BTW, I am not sure you want this, but the
, th
means "also give these same styles to allth
s".table.foo
选择具有 foo 类的表。table .foo
选择表中具有 foo 类的所有元素。因此,您之前的选择器:
table .class tr, th
选择具有类class
且位于表内的元素内的所有tr
,以及页面上的所有th
。table.foo
selects tables with the class foo.table .foo
selects all elements that have the class foo, and are inside a table.So your selector before:
table .class tr, th
selects alltr
s that are inside an element which has classclass
and is inside a table, as well as allth
s on the page.table .class tr
将选择一个tr
:但什么也没有:
而
table.class tr
将选择一个tr
:AND a
tr
:但什么也没有:
table .class tr
would select atr
:but nothing:
While
table.class tr
would select atr
:AND a
tr
:But nothing: