CSS :first-child 没有按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4505686/
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
:first-child not working as expected
提问by The Muffin Man
I'm trying to select the first h1
inside a div
with a class called detail_container
. It works if h1
is the first element within this div
, but if it comes after this ul
it won't work.
我想选择第一个h1
内div
有一个叫做类detail_container
。如果h1
是 this 中的第一个元素div
,ul
它会起作用,但如果它在此之后,它将不起作用。
<style type="text/css">
.detail_container h1:first-child
{
color:blue;
}
</style>
</head>
<body>
<div class="detail_container">
<ul>
<li></li>
<li></li>
</ul>
<h1>First H1</h1>
<h1>Second H1</h1>
</div>
</body>
</html>
I was under the impression that the CSS I have will select the first h1
no matter where it is in this div
. How can I make it work?
我的印象是,我拥有的 CSS 将选择第一个,h1
无论它在哪里div
。我怎样才能让它工作?
回答by BoltClock
The h1:first-child
selector means
该h1:first-child
选择装置
Select the first child of its parent
if and only if it's anh1
element.
当且仅当它是一个h1
元素时,才选择其父项的第一个子项。
The :first-child
of the container here is the ul
, and as such cannot satisfy h1:first-child
.
所述:first-child
容器的这里是ul
,这样不能满足h1:first-child
。
There is CSS3's :first-of-type
for your case:
:first-of-type
您的情况有 CSS3 :
.detail_container h1:first-of-type
{
color: blue;
}
But with browser compatibility woes and whatnot, you're better off giving the first h1
a class, then targeting that class:
但是由于浏览器兼容性问题等等,你最好给第h1
一个类,然后针对该类:
.detail_container h1.first
{
color: blue;
}
回答by Rob Sobers
:first-child
selects the first h1
if and only ifit is the first child of its parent element. In your example, the ul
is the first child of the div
.
:first-child
选择第一个h1
当且仅当它是其父元素的第一个子元素。在您的示例中,ul
是 的第一个孩子div
。
The name of the pseudo-class is somewhat misleading, but it's explained pretty clearly herein the spec.
伪类的名称有些误导,但它的解释很清楚这里的规范。
jQuery's :first
selector gives you what you're looking for. You can do this:
jQuery 的:first
选择器为您提供了您正在寻找的东西。你可以这样做:
$('.detail_container h1:first').css("color", "blue");
$('.detail_container h1:first').css("color", "blue");
回答by Grekz
For that particular case you can use:
对于这种特殊情况,您可以使用:
.detail_container > ul + h1{
color: blue;
}
But if you need that same selector on many cases, you should have a class for those, like BoltClock said.
但是,如果您在许多情况下都需要相同的选择器,那么您应该为这些选择器创建一个类,就像 BoltClock 所说的那样。
回答by Fabrice
you can also use
你也可以使用
.detail_container h1:nth-of-type(1)
By changing the number 1 by any other number you can select any other h1 item.
通过将数字 1 更改为任何其他数字,您可以选择任何其他 h1 项目。
回答by GhostToast
You could wrap your h1
tags in another div
and then the first one would be the first-child
. That div
doesn't even need styles. It's just a way to segregate those children.
您可以将您的h1
标签包装在另一个中div
,然后第一个将是first-child
. 那div
甚至不需要样式。这只是隔离那些孩子的一种方式。
<div class="h1-holder">
<h1>Title 1</h1>
<h1>Title 2</h1>
</div>
回答by Edmond Chow
The element cannot directly inherit from <body>
tag.
You can try to put it in a <dir style="padding-left:0px;"></dir>
tag.
元素不能直接从<body>
标签继承。您可以尝试将其放入<dir style="padding-left:0px;"></dir>
标签中。