在 CSS 中,可以使用“#footer #caption”与“#content #caption”共存?
我打算像这样“嵌套”CSS id
,
#content #caption { color: teal }
...
#footer #caption { margin: 2em 1em }
因为这是 SASS(CSS 生成器)可以嵌套的方式...但是在一个 HTML 文档中,我们不能有两个 id
具有相同的名称,不是这样的,所以上面的嵌套不起作用或者不能很好地起作用。 (特别是如果需要 document.getElementById() 或 $('#caption') 或 $('caption') 来选择元素)。
我们可以使用
#content #content_caption { color: teal }
...
#footer #footer_caption { margin: 2em 1em }
,但是为什么还要多一层嵌套呢?为什么不只是
#content_caption { color: teal }
...
#footer_caption { margin: 2em 1em }
?
I was going to "nest" the CSS id
s like this
#content #caption { color: teal }
...
#footer #caption { margin: 2em 1em }
because that's the way SASS (a CSS generator) can do nesting for... but then in one HTML document, we cannot have two id
s with the same name, isn't that true, so the above nesting won't work or won't work well. (esp if document.getElementById() or $('#caption') or $('caption') is needed to select the element).
We can use
#content #content_caption { color: teal }
...
#footer #footer_caption { margin: 2em 1em }
but then why 1 more level of nesting? why not just
#content_caption { color: teal }
...
#footer_caption { margin: 2em 1em }
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“标题”一词表明它不是唯一标识符。如果是这样,我会将标题声明为一个类。以下内容是完全合法有效的:
The word "caption" would indicate that it is not an unique identifier. If so, I would declare the caption as a class. The following would be completely legal and valid:
没有理由这样做。 id 是一个非常重的选择器,它应该足以更改样式规则。如果没有,请在之前附加#content 或更改获胜规则的选择器。
no reason for that. id is a really heavy selector, it should be enough to change the styling rules. if not append the #content before or change the selectors of the rules that are winning.
如果单个页面上没有两个 id="caption" 的元素,那就完全没问题。
然而,我从命名(内容和页脚)猜想,有不止一个 id =“caption”,这是非常糟糕的。您应该记住id必须是唯一的!使用“class”代替,例如
If there are no two elements with id="caption" on the single page, it's perfectly OK.
However, I guess from the naming (content & footer) that there's more than one with id = "caption", which is very bad. You should remember that id must be unique! Use "class" instead, like
首先,像“caption”这样的东西听起来确实更像是一个类:
你说:这是 **the** 页脚(特定 id),但是 这是 **a** 标题(普通班)。
这是在 Sass 中嵌套选择器的另一种方法:(
“&”引用它所包含的任何内容。)
First, something like "caption" really does sound more like a class:
You say: this is **the** footer (specific id), but this is **a** caption (general class).
Here's another way you can nest the selectors in Sass:
(The "&" references whatever it's contained in.)