Html 我可以在 CSS 中同时使用 DIV 类和 ID 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4371733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use DIV class and ID together in CSS?
提问by Billa
Can I use DIV Class and ID together in CSS? For example:
我可以在 CSS 中同时使用 DIV 类和 ID 吗?例如:
<div class="x" id="y">
--
</div>
回答by David says reinstate Monica
Yes, yes you can.
是的,是的,你可以。
#y.x {
/* will select element of id="y" that also has class="x" */
}
Similarly:
相似地:
.x#y {
/* will select elements of class="x" that also have an id="y" */
}
Incidentally this might be useful in some use cases (wherein classes are used to represent some form of event or interaction), but for the most part it's not necessarilythat useful, since id
s are unique in the document anyway. But if you're using classes for user-interaction then it canbe useful to know.
顺便说一句,这在某些用例中可能很有用(其中类用于表示某种形式的事件或交互),但在大多数情况下,它不一定有用,因为id
无论如何 s 在文档中都是唯一的。但是,如果你正在使用类用户交互那么它可能需要了解的。
回答by Pete
You can also use as many classes as needed on a tag, but an id must be unique to the document. Also be careful of using too many divs, when another more semantic tag can do the job.
您还可以根据需要在标签上使用任意数量的类,但 id 必须是文档唯一的。还要小心使用太多的 div,因为另一个更具语义的标签可以完成这项工作。
<p id="unique" class="x y z">Styled paragraph</p>
回答by skajfes
Of course you can.
当然可以。
Your HTML there is just fine. To style the elements with css you can use the following approaches:
你的 HTML 就好了。要使用 css 设置元素样式,您可以使用以下方法:
#y {
...
}
.x {
...
}
#y.x {
...
}
Also you can add as many classes as you wish to your element
您也可以根据需要向元素添加任意数量的类
<div id="id" class="classA classB classC ...">
</div>
And you can style that element using a selector with any combination of the classes and id. For example:
您可以使用带有类和 id 的任意组合的选择器来设置该元素的样式。例如:
#id.classA.classB.classC {
...
}
#id.classC {
}
回答by Quentin
That's HTML, but yes, you can bang pretty much any selectors you like together.
那是 HTML,但是是的,您几乎可以将任何您喜欢的选择器组合在一起。
#x.y { }
(And the HTML is fine too)
(而且 HTML 也很好)
回答by Azhaar Ahmad Jan
Yes, in one single division you can use both but it's not very common. While styling you will call both so it will cause some ambiguity if you don't properly choose "x" and "y".
Use #
for ID and .
for class. And for overall division you will either do separate styling or write: #x .y
for styling purposes.
是的,在一个部门中,您可以同时使用两者,但这并不常见。在样式化时,您将同时调用两者,因此如果您没有正确选择“x”和“y”,则会导致一些歧义。使用#
的ID和.
类。对于整体划分,您将进行单独的造型或写作: #x .y
出于造型目的。
回答by gdj
Yes, why not? Then CSS that applies to class "x" AND CSS that applies to ID "y" applies to the div.
是的,为什么不?然后适用于类“x”的CSS和适用于ID“y”的CSS适用于div。
回答by matthewpavkov
If you want to target a specific class andID in CSS, then use a format like div.x#y {}
.
如果要在 CSS 中定位特定的类和ID,请使用类似div.x#y {}
.
回答by GolezTrol
#y.x should work. And it's convenient too. You can make a page with different kinds of output. You can give a certain element an id, but give it different classes depending on the look you want.
#yx 应该可以工作。而且也很方便。您可以制作具有不同类型输出的页面。你可以给某个元素一个 id,但根据你想要的外观给它不同的类。
回答by Trufa
Yes you can.
是的你可以。
You just need to understand what they are for, the class
is more general and can be used several times, the id
(is like your id's) you can use it only once.
您只需要了解它们的用途,class
更通用并且可以使用多次,id
(就像您的 ID 一样)您只能使用一次。
This excellent tutorial helped me with that:
这个优秀的教程帮助我做到了:
The Difference Between ID and Class
Though it's not an exact answer to your question I'm sure it will help you a lot!
虽然这不是您问题的确切答案,但我相信它会对您有很大帮助!
Good luck!
祝你好运!
EDIT: Reading your question, I just want to clarify that:
编辑:阅读您的问题,我只想澄清一下:
<div class="x" id="y">
--
</div>
And that if you want to "use them" in CSS for styling purposes you should do as David Says: #x.y { }
如果你想在 CSS 中“使用它们”用于样式目的,你应该按照大卫说的做:#x.y { }