2016-08-01 60 views
1

我有html。恩。像這樣:如何除了div中的所有元素CSS與類?

<form class="smart-form"> 
    <div> 
    <p></p> 
    <i></i> 
    <div> 
     <span class="classname"><b>text</b></span> 
    </div> 
    </div> 
</form> 

而且有CSS代碼爲所有表單元素,但我需要除此規則這是內部.classname的所有元素:

.smart-form *, 
.smart-form *:after, 
.smart-form *:before { 
    margin: 0; 
    padding: 0; 
    box-sizing: content-box; 
    -moz-box-sizing: content-box; 
} 

我想是這樣的:

.smart-form *:not(> .classname *) {} // but its wrong 

回答

2

this SO answer通過@ BoltClock♦:

:not()一次只接受一個簡單的選擇器;這是在選擇器3中提到的規格:

&hellip;

如果你想自己使用這個否定,選擇所有其他元素,那麼沒有辦法只使用一個選擇器。

換句話說,沒有辦法在:not()選擇器中嵌套後裔選擇器。


作爲替代方案,我建議有點在你的方法範式轉變的:而不是選擇所有元素除了的那些類.classname內,加所有你想要的其他規則元素,然後通過調整.classname樣式來反轉它們。即,:

.smart-form * { 
    /* general CSS styles for .smart-form children */ 
} 
.smart-form .classname, form .classname * { 
    /* specific CSS styles for .classname children */ 
} 

這應該選擇所有的.classname類的後代。

.smart-form *, 
 
.smart-form *:after, 
 
.smart-form *:before { 
 
    margin: 0; 
 
    padding: 0; 
 
    box-sizing: content-box; 
 
    -moz-box-sizing: content-box; 
 
} 
 

 
/* you can use initial or auto in most cases to set the properties back to default */ 
 
.smart-form .classname, .smart-form .classname * { 
 
    margin: initial; 
 
    padding: initial; 
 
    box-sizing: initial; 
 
    -moz-box-sizing: initial; 
 
}
<form class="smart-form"> 
 
    <div> 
 
    <p></p> 
 
    <i></i> 
 
    <div> 
 
     <span class="classname"><b>text</b></span> 
 
    </div> 
 
    </div> 
 
</form>

在上面,樣式設置爲所有元素的例子,但隨後它們被那些只爲.classname逆轉。

+0

但我需要所有與class =「noStyle」元素內的元素.... – Haroldas

+0

@Haroldas對不起,我不明白在這個問題。我會更新我的答案。抓緊! –

+0

@Haroldas我更新了我的答案。 –

0

我認爲你不應該把CSS放在它上面,只是把CSS放在你想要的div裏面。如果你只是想要例外,那麼把css放在什麼位置上呢?

相關問題