如何将CSS样式应用于不包含子类的类

前端之家收集整理的这篇文章主要介绍了如何将CSS样式应用于不包含子类的类 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要在第一个faq-category-group类的div上设置样式,而不会影响faq-category-indent内的faq-category-group的样式.如何才能做到这一点?

这些类是由PHP模块自动生成的,因此不能更改类名以使选择器更容易.

  1. <div class="faq">
  2. <div class="faq-category-group">Content</div>
  3. <div class="faq-category-indent">
  4. <div class="faq-category-group">Content</div>
  5. </div>
  6. </div>
最佳答案
通过该结构,仅选择作为< div class =“ faq”>的子级的组.并将不需要的样式应用于缩进的组. < div class =“ faq-category-indent”>中包含的组.不会受到影响.

  1. .faq .faq-category-group {
  2. /* Styles for all groups */
  3. }
  4. .faq > .faq-category-group {
  5. /* Styles for non-indented groups */
  6. }

当然,这假定您不关心IE6.否则,另一个更冗长的解决方案是:

  1. .faq .faq-category-group,.faq .faq-category-indent .faq-category-group {
  2. /* Styles for all groups */
  3. }
  4. .faq .faq-category-group {
  5. /*
  6. * Styles for non-indented groups.
  7. * Works because .faq .indent .group above is more specific than
  8. * this one,so the above rule will override this one.
  9. */
  10. }

这是涵盖两种情况的jsFiddle example.

猜你在找的CSS相关文章