Rob*_*Rob 99 html css css-selectors
我在页面上有以下HTML代码:
<h4>Some text</h4>
<p>
Some more text!
</p>
Run Code Online (Sandbox Code Playgroud)
在我的.css我有以下选择器来设置h4元素的样式.上面的HTML代码只是整个代码的一小部分; 还有几个div属于一个影子箱的东西:
#sb-wrapper #sb-wrapper-inner #sb-body #myDiv h4
{
color : #614E43;
margin-top : 5px;
margin-left : 6px;
}
Run Code Online (Sandbox Code Playgroud)
所以,我的h4元素有正确的样式,但我也希望p在HTML中设置样式.
CSS选择器有可能吗?如果是,我该怎么做?
Phr*_*ogz 183
#many .more.selectors h4 + p { ... }
Run Code Online (Sandbox Code Playgroud)
这称为相邻的兄弟选择器.
Joe*_*lon 28
对于您的文字示例,您需要使用相邻的选择器(+).
h4 + p {color:red}//any <p> that is immediately preceded by an <h4>
<h4>Some text</h4>
<p>I'm red</p>
<p>I'm not</p>
Run Code Online (Sandbox Code Playgroud)
但是,如果要选择所有连续段落,则需要使用通用兄弟选择器(〜).
h4 ~ p {color:red}//any <p> that has the same parent as, and comes after an <h4>
<h4>Some text</h4>
<p>I'm red</p>
<p>I am too</p>
Run Code Online (Sandbox Code Playgroud)
小智 14
试图解决这种类型的事情时,只需点击这个.
我做了一个选择器来处理除了p之外的其他元素.
.here .is.the #selector h4 + * {...}
Run Code Online (Sandbox Code Playgroud)
希望这有助于找到它的人:)