具有多个子类的选择器的 HTML 语法
我正在尝试为选择器的多个子类定义 CSS 规则。这是 html 的示例。
<div id="row1">
<div class="col1">
<div class="col2">
<div class="col3">
</div>
<div id="row2">
<div class="col1">
<div class="col2">
<div class="col3">
</div>
假设我想让 row1 内的 col1、col2、col3 的宽度都相同。我尝试了这个 css,但它并不局限于 row1:
#row1 .col1, .col2, .col3{
width: 80px;
}
我可能可以在每个 .col 前面添加 #row1,但这看起来不太好。这样做的正确方法是什么?
I am trying to define a css rule for multiple sublcasses of a selector. Here is an example of the html.
<div id="row1">
<div class="col1">
<div class="col2">
<div class="col3">
</div>
<div id="row2">
<div class="col1">
<div class="col2">
<div class="col3">
</div>
Say I want to make the width of col1, col2, col3 within row1 all the same. I tried this css but it doesnt stay specific to row1:
#row1 .col1, .col2, .col3{
width: 80px;
}
I can probably add #row1 infront of every .col but that wouldnt look as nice. What is the correct way to do this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该这样做:
该规则适用于作为
#row1
子级的每个div
元素。我指定了一个子选择器(>
),以防您不想应用规则的嵌套div
元素。This should do:
The rule applies to every
div
element that is a child of#row1
. I specified a child selector (the>
) in case you have nesteddiv
elements that you do not want the rule to apply to.这是唯一的方法,至少可以得到你所描述的规则。
使用您拥有的标记,您可以获得相同的效果:
That is the only way to do that, at least to get the rule you describe.
With the markup you have, you could get the same effect with: