2012-04-28 56 views
1

在以下情况下,如何使用:not()div.note em从粗体中排除?CSS3:不是()选择器

div#content em { 
    font-weight: bold; 
} 

/* I want to remove the following 
    in favor of the :not selector being applied to the above */ 
div#content div.note em { 
    font-weight: normal; 
} 

PS:这是一个精简的例子,其实我有很多的样式div#content em,我想申请到一切,除了div.note em。这就是为什么我不希望覆盖所有这些手动lateron ...

+0

由于后代组合子的性质,单靠':not()'不能可靠地做到这一点。您必须创建一些取决于您的HTML结构的复杂选择器,并且努力做到这一点不值得仅仅使用您的'div#content div.note em'覆盖规则。 – BoltClock 2012-04-28 15:06:03

+0

(我不知道这样的选择器是否可能,这取决于你的标记。) – BoltClock 2012-04-28 15:16:38

+0

好吧,标记与CSS建议非常相似......想象一下'div#content'就像'article'然后'div.note'成为该文章中突出显示的信息框,除了'p','blockquote',图像等其他所有内容外。 – Ben 2012-04-28 15:26:47

回答

3

如果你知道#content.note之间的确切层次,你可以使用子选择器,使不够具体工作的否定。如果.note#content一个孩子,你用:

#content > :not(.note) em, #content > em {} 

如果有#content.note之间的两个<div> S,这样做:

#content > div > div > :not(.note) em, #content > em {} 

你可能最好使用你有什么,但是,只是压倒作为.note后裔的em元素。

+0

另外,除非添加标签更改选择器(即,您有#content或.note元素不是div),否则您确实不需要使用标签限定您的id和class选择器。 – HaleFx 2012-04-28 15:37:44