我有一個CSS類「MyParent」正在由多個子類繼承工作作爲exected。 我想要「MyParent」類的一些屬性,應該由子類重寫,因爲它們需要重寫,並且這個重寫不應該影響其他子類。 請找到下面的例子。CSS:繼承是不是在子類
.MyParent {
border-style: solid;
border-color: red;
background-color: yellow;
color: red;
}
.MyChild1 .MyParent{
background-color: blue !important;
color: white !important;
}
.MyChild2 .MyParent{
background-color: yellow !important;
color: black !important;
}
而我的Html如下所示。
<div class="MyParent">
this is parent div
</div>
<div class="MyChild1">
this is child1
</div>
<div class="MyChild2">
this is child2
</div>
我期待與黃背對地表的顏色與字體顏色顯示的MyParent DIV是紅色的,這是工作的罰款,但如預期,雖然我替換的屬性,其餘的div不工作。我所做的是,請找到下面的代碼。
.MyChild1, .MyParent{
background-color: blue !important;
color: white !important;
}
.MyChild2, .MyParent{
background-color: green !important;
color: white !important;
}
現在,它的部分工作,但屬性格式我們在課堂上「MyChild2」是影響父類的屬性來覆蓋。正因爲如此,具有類「MyParent」的div因爲這種繼承而沒有按預期得到渲染。由於這種情況,在應用程序的大部分位置中,輸出受到干擾。在情況下,如果我想這如預期,我可以寫一個多子類中工作,我們可以做到這一點,還寫子類每一次很討厭也有的像BORDERCOLOR屬性是沒有得到繼承。但爲什麼在CSS中這種行爲,我們如何糾正它。有人可以解釋我嗎?
在此先感謝, 薩蘭。如果它們的值被設置爲inherit
你是對的@SLaks :)你從我這裏應該有一個+1! –
這不完全正確。有些屬性是繼承的,有些則不是。繼承的初始值不是'inherit',但是當級聯不會產生一個值時它們會繼承。繼承和非繼承都可以強制繼承'inherit'。見http://www.w3.org/TR/css-cascade-4/#inheriting – Oriol