Html 你能用 CSS 定位 ID 中的 ID 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18133624/
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 you target with CSS an ID within ID?
提问by Ryan
My example would be within an HTML file, that say you don't have access to change - only the CSS via a stylesheet. Can you target an ID within an ID the same as you can Classes?
我的示例将在 HTML 文件中,也就是说您无权进行更改 - 只有通过样式表的 CSS。你能像你一样瞄准一个 ID 中的一个 ID 吗?
#id1 #id2 {styles...}
similar to how you do it with CSS:
类似于您如何使用 CSS:
.class1 .class2 {styles...}
I could just be having a major brain fail here.
我可能只是在这里出现了严重的大脑故障。
回答by dezman
yeah, like this:
是的,像这样:
#one #two {
color: purple;
}
will select for:
将选择:
<div id="one">
<div id="two"></div>
</div>
this is really not necessary though, because you are only supposed to have one id of the same name per page, so the selector #two {}
would be fine by itself.
不过这真的没有必要,因为你应该每页只有一个同名的 id,所以选择器#two {}
本身就可以了。
回答by Spudley
Yes, you can do that; it's perfectly valid. But it's also generally pointless, given that an ID has to be unique within a page, so just selecting the single ID ought to always be sufficient to select exactly the element you want; you shouldn't need any additional parent selector to qualify it, whether another ID or a class or anything else.
是的,你可以这样做; 这是完全有效的。但这通常也是没有意义的,因为 ID 在页面内必须是唯一的,所以只选择单个 ID 应该总是足以准确选择您想要的元素;您不需要任何额外的父选择器来限定它,无论是另一个 ID、类还是其他任何东西。
I can see one use case for it, where you have an element with an ID that is dynamic in where is appears in the page that could appear in different locations for whatever reason, and you want it to look different according to where it in the page it appears.
我可以看到它的一个用例,其中您有一个元素,其 ID 是动态的,出现在页面中的位置,无论出于何种原因都可能出现在不同位置,并且您希望它根据它在页面中的位置看起来不同页面出现。
For that, it might be valid to have #id1 #id2
selector. But it's probably a fairly rare use case, and even for this usage, classes might be a more appropriate tool for the job.
为此,拥有#id1 #id2
选择器可能是有效的。但这可能是一个相当罕见的用例,即使对于这种用法,类也可能是更适合这项工作的工具。
回答by Jon
Yes
是的
#id1 #id2 {
}
This will target all of #id2
within #id1
这将针对所有#id2
内部#id1
回答by Quentin
Yes. You can put combinators between any selectors that you like.
是的。您可以在您喜欢的任何选择器之间放置组合器。
回答by Jez D
Yes
是的
#first #second{
color: #000080;
}
and
和
<div id="first">
<p id="second">This is text and will be dark blue</p>
</div>
回答by Octane
Yes you can
是的你可以
#id1 #id2 {
height:200;
}
and also similarly
也同样
.class1 #id1 {
height:200;
}
.class1 input[type="radio"] {
border: 1px solid #ccc;
}
h1, p{
}