Html 在 Div ID 中设置 Div 类的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2556701/
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
Styling a Div Class within Div ID
提问by Zach Smith
I have a page that has the following div element:
我有一个包含以下 div 元素的页面:
<div id="name" class="class_name">
Which of the following should I use to style the class?
我应该使用以下哪项来设置类的样式?
#name .class_name
name .class_name
#name class_name
回答by Daniel Vassallo
Just #name
would be enough to apply the style only to that specific element:
仅#name
将样式应用于该特定元素就足够了:
#name {
// your styles here
}
If you want to apply the style to all the elements using the class_name
class, then you can use:
如果要将样式应用于使用class_name
该类的所有元素,则可以使用:
.class_name {
// your styles here
}
回答by Boris Guéry
#name .class_name will apply to the children elements of your div with the .class_name class.
#name .class_name 将应用于具有 .class_name 类的 div 的子元素。
There must not be any spaces between.
之间不能有任何空格。
You should use: #name.class_name
because it is the correct syntax.
您应该使用:#name.class_name
因为它是正确的语法。
But, when working with identifiers, it should be enough to use it alone.
但是,在使用标识符时,单独使用它应该就足够了。
#name {
// some properties
}
But let's say you have a unique block, which inherits some generic styles, then you could use #idenifier.class to mix styles.
但是假设您有一个独特的块,它继承了一些通用样式,那么您可以使用 #idenifier.class 来混合样式。
#comments {
width: 453px;
}
#comments.links
{
border: 0 none;
}
回答by Federico klez Culloca
If I got your question right you use
如果我问对了你的问题,你就用
#name .class_name
as in
如
#name .class_name {
color:#000;
}
But actually you don't need to do that.
If #name
and .class_name
are oly used in that div you can just drop the .class_name
. Otherwise, if you use multiple stuff with .class_name
that don't share the same properties as div #name
, you should separate them.
但实际上你不需要这样做。如果#name
和.class_name
仅在该 div 中使用,您可以将.class_name
. 否则,如果您使用多个与.class_name
div 不共享相同属性的内容#name
,则应将它们分开。
.class_name {
color:#000;
}
#name {
width:600px;
}
回答by womp
ID's should be unique, so there isn't much point in styling both the id of the div and the class_name.
ID 应该是唯一的,因此设置 div 的 id 和 class_name 的样式没有多大意义。
Just using the ID of the div is fine if that's your goal.
如果这是您的目标,只需使用 div 的 ID 就可以了。
#name
{
//styles
}
If you want to style every div that has that class name, then just use
如果你想为每个具有该类名的 div 设置样式,那么只需使用
.class_name
{
//styles
}
回答by Brian Walker
You can read about CSS selectors here:
您可以在此处阅读有关 CSS 选择器的信息:
http://reference.sitepoint.com/css/selectorref
http://reference.sitepoint.com/css/selectorref
In the CSS you can reference it by class:
在 CSS 中,您可以按类引用它:
.class_name { color: green; }
or by id using #id
或通过 id 使用 #id
#name { color: green; }
or by element type
或按元素类型
div { color: green; }
//or by combinations of the above
//或通过以上组合
div.class_name { color: green; }
div#name { color: green; }
回答by jeanreis
If you already have #name and .class_name styled, but for some reason you want to style
如果你已经有 #name 和 .class_name 样式,但由于某种原因你想要样式
<div id="name" class="class_name"></div>
(that is, a specific style to the element with an id of "name" AND a class of "class_name"), you can use
(即,id 为“name”和类为“class_name”的元素的特定样式),您可以使用
#name.class_name {
/* note the absence of space between the id and class selector */
...
}
Bear in mind that this selector is not supported by Internet Explorer 6.
请记住,Internet Explorer 6 不支持此选择器。