开发者

css modification (can use ! not)?

I can do .one.two and that targets any element with the class "one" AND the class "two" right?

What if I want to target any element with class "one" but that can't be coupled with "two" ? eg .one !.two or something开发者_运维技巧 like that.

Thx all.


CSS3 includes :not(), but it's not yet supported by IE, I think. Works in recent versions of other browsers:

.one:not(.two) {
  //your styles
}


Try the :not pseudo-class

Or use a negation class: Is there any way to get the CSS :not() selector working in IE and Chrome?


.one {background: red;}
.two {background: green;}
.one.two {background: blue;}

I don't see why you'd need 'not' there as .one would style the same as .one but not .two. If you are already using multiple classes, you should be good to go.


It sounds like you need to read about specificity.

.one will select all class="one" elements, .two will select all class="two" elements.

.one.two will select all class="one two" elements and will override any rules that may have been set for .one or .two for that element.

The .one:not(.two) selector will perform the negation you desire, but it's not cross-browser compatible.

The most cross-browser compatible way of adding a rule like .one:not(.two) is to add an override:

.one
{
  color: blue;
}

.one.two
{
  color: black;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜