简体   繁体   English

使用CSS根据顶级div设置div类的样式

[英]Styling for div classes based on top div by using CSS

Hi I have a scenario where I have to style a h3 element based on parent div class 嗨,我有一个场景,我必须根据父div类设置h3元素的样式

Example: 例:

<div class="First">
 <p>This is sample text</p>
</div>
<div class="Second">
 <h3>This is sample header</h3>
</div>

Now what I want to do is if there is a p tag inside class named First, I want to change the style of h3 tag of second class. 现在,我想做的是在名为First的类中是否有一个p标记,我想更改第二类的h3标记的样式。

Answer: Without altering the HTML, this is currently not possible with CSS alone. 答: 如果不更改HTML,当前仅靠CSS是不可能的。

Further information: The CSS Selectors Level 4 specs introduce(d) the new parent selector $ : 更多信息: CSS选择器第4级规范介绍了新的父选择器$

$div > p {
    // styles here
}

The selector above would target the div , not the p . 上面的选择器将div目标, 而不是 p Of course, only divs, which have a direct p child element. 当然,只有div具有直接的p子元素。 Still, this wouldn't help much with further additions and as BoltClock noted, the specs haven't been revised for a couple of years and a parent selector might not even come soon. 尽管如此,这对进一步添加并没有多大帮助,而且正如BoltClock所指出的那样,规范已经有两年没有修改过了,父选择器甚至可能不会很快推出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM