我有类似以下这样的东西:
<div id="content>
<h1>Welcome to Motor City Deli!</h1>
<div style=" font-size: 1.2em; font-weight: bolder;">Sep 19, 2010</div>
<div > ... </div>
Some content
Author
Date
我有类似以下这样的东西:
<div id="content>
<h1>Welcome to Motor City Deli!</h1>
<div style=" font-size: 1.2em; font-weight: bolder;">Sep 19, 2010</div>
<div > ... </div>
你问题的最正确答案是...
#content > div:first-of-type { /* css */ }
#content > div:nth-of-type(1) { /* css */ }
#content > div:not(:last-of-type) { /* css */ }
- Jeremy Moritz#content > div:not(:first-of-type):not(:last-of-type) { /* css */ }
- Jeremy Moritz#content div:first-child {
/*css*/
}
<div>
不是第一个子元素(它的第一个子元素是<h1>
)。 - Josh Leitzel如果我们可以假设H1标签始终存在,那么
div h1+div {...}
但不要害怕指定内容div的id:
#content h1+div {...}
目前在不使用像jQuery这样的JavaScript库时,这是您可以跨浏览器获得的最佳解决方案。 使用h1 + div可以确保只有H1后面的第一个div获得样式。虽然存在其他选择,但它们依赖于CSS3选择器,因此在大多数IE安装中无法工作。
<div>
之前有一个<h1>
。我建议你为<div>
添加一个类,例如<div class="first">
,然后进行样式设置,或者如果你真的不能添加类,请使用jQuery:$('#content > div.first')
。$('#content > div.first)
- Mateusz Odelga