首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >根据元素的子元素将CSS样式应用于元素

根据元素的子元素将CSS样式应用于元素
EN

Stack Overflow用户
提问于 2010-02-24 22:03:30
回答 4查看 208.5K关注 0票数 223

是否可以为元素定义CSS样式,该样式仅在匹配的元素包含特定元素(作为直接子项)时应用?

我认为用一个例子来解释这一点最好。

注意:我正在尝试样式化父元素,这取决于它包含的子元素。

<style>
  /* note this is invalid syntax. I'm using the non-existing
   ":containing" pseudo-class to show what I want to achieve. */
  div:containing div.a { border: solid 3px red; }
  div:containing div.b { border: solid 3px blue; }
</style>

<!-- the following div should have a red border because
     if contains a div with class="a" -->
<div>
  <div class="a"></div>
</div>

<!-- the following div should have a blue border -->
<div>
  <div class="b"></div>
</div>

CSSNote2CSS:我知道我可以使用javascript来实现这一点,但我只是想知道是否可以使用一些(对我来说)未知的特性。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2326499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档