CSS 中下一个元素的选择器的语法是什么?
如果我有一个标题标签 title
h1.hc-reform{
float:left;
font-size:30px;
color:#0e73bb;
font-weight:bold;
margin:10px 0px;
}
,之后我有一个段落 stuff here
。
如何确保使用 CSS 后 h1.hc-reform
后面的每个
标签使用:
clear:both;
都会那是:
h1.hc-reform > p{
clear:both;
}
由于某种原因,这不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这称为相邻兄弟选择器,它由加号...
注意:IE6 或更早版本不支持此功能。
This is called the adjacent sibling selector, and it is represented by a plus sign...
Note: this is not supported in IE6 or older.
您可以使用 同级选择器
~
:这会选择所有
.hc-reform
之后的 >p 元素,而不仅仅是第一个元素。You can use the sibling selector
~
:This selects all the
p
elements that come after.hc-reform
, not just the first one.没有
>
是子选择器。你想要的是
+
所以尝试
h1.hc-reform + p
浏览器支持不是很好
no
>
is a child selector.the one you want is
+
so try
h1.hc-reform + p
browser support isn't great
>
是一个子选择器。因此,如果您的 HTML 如下所示:... 那么这就是您的票。
但是,如果您的 HTML 如下所示:
那么您需要 相邻选择器:
The
>
is a child selector. So if your HTML looks like this:... then that's your ticket.
But if your HTML looks like this:
Then you want the adjacent selector:
不完全是。
h1.hc-reform > p
表示“h1.hc-reform
下面的任意p
级别”。你想要的是
h1.hc-reform + p
。当然,这可能会导致旧版本的 Internet Explorer 出现一些问题;如果您想让页面与旧版 IE 兼容,您将不得不手动向段落添加类或使用一些 JavaScript(例如,在 jQuery 中,您可以执行类似$('h1. hc-reform').next('p').addClass('first-paragraph')
)。更多信息:http://www.w3.org/TR/CSS2/selector.html 或 http://css-tricks.com/child-and-兄弟选择器/
Not exactly. The
h1.hc-reform > p
means "anyp
exactly one level underneathh1.hc-reform
".What you want is
h1.hc-reform + p
. Of course, that might cause some issues in older versions of Internet Explorer; if you want to make the page compatible with older IEs, you'll be stuck with either adding a class manually to the paragraphs or using some JavaScript (in jQuery, for example, you could do something like$('h1.hc-reform').next('p').addClass('first-paragraph')
).More info: http://www.w3.org/TR/CSS2/selector.html or http://css-tricks.com/child-and-sibling-selectors/