2011-06-16 92 views
39

說我有一個div像這樣的是要具有完全相同的屬性與背景圖像或類似的東西:CSS繼承

div.someBaseDiv 
{ 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

,我想從它繼承這樣的:

div.someBaseDiv someInheritedDiv 
{ 
    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 

當然,我很確定這是寫錯了,我不害怕尋求幫助,所以有人可以告訴我如何使這項工作,幷包括HTML標記?

+0

我不知道你在做什麼?這是一個選擇器「div.someBaseDiv someInhertedDiv」。它與繼承無關。選擇器的最後部分「someInhertedDiv」,我沒有得到那是什麼。 CSS繼承意味着一些但不是所有在父/祖先元素上設置的屬性都被子/後代元素所接受。例如,如果你有一個div元素,如div#parent,它的風格是say,「color:blue」,並且div#parent包含另一個div,比如div#child,那麼div#child也會得到「顏色:藍色」的風格 – Jawad 2011-06-16 19:01:17

+0

你可能會混淆「someInhertedDiv」。如前所述,並非所有的屬性都是繼承的,只有一些屬性,例如顏色,字體大小,字體重量等等。您可能會將HTML設置爲「

I am a parent
I am a child
」,CSS爲「div#parent {color:blue;}」,現在div#孩子也會有藍色。這是工作中的CSS繼承。 – Jawad 2011-06-16 19:06:52

回答

57

最簡單的方法就是將someInheritedDiv元素添加到第一條規則中。

div.someBaseDiv, 
#someInheritedDiv 
{ 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

這會告訴您的#someInheritedDiv應用與div.someBaseDiv相同的樣式。然後你擴展這一套樣式更具體的您#someInheritedDiv:

#someInheritedDiv 
{ 
    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 

這是怎麼特異性在CSS作品。

+0

perfect thankyou – Jonesopolis 2013-12-10 13:52:44

3

這真的取決於你的標記。如果您的繼承元素位於類someBasDiv的div下,那麼它的所有子元素都將自動繼承這些屬性。

但是,如果你想繼承在任何地方someBaseDiv類的標記,你可以只讓你想繼承元素,同時使用這些類的這樣的:

<div class="someBaseDiv someInheritedDiv"> 

和你的CSS會是這樣:

div.someBaseDiv 
{ 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

div.someInheritedDiv 
{ 
    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 
13

對於這個任務,我會建議你使用CSS的強大的擴展名爲LESS。它可以編譯成CSS或者可以通過JavaScript文件實時使用,如鏈接所解釋的。

LESS支持繼承(幾乎),如你所描述的。 documentation有詳細信息(請參見「Mixins」一節)。

對於示例,代碼越少是:

.someBaseDiv { 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

someInheritedDiv { 
    .someBaseDiv; 

    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 

注意,它必須是.someBaseDiv而不是div.someBaseDiv

+15

只是一個註釋 - LESS並不是CSS的擴展。這是一個用Javascript編寫的預處理器。 – spliter 2013-01-10 14:23:44

7

你想要做的是使一些CSS適用於兩種不同類型的的元素,但也允許它們有一些差異。

<div class="base"> 
    <div class"inherited"> 
    </div> 
</div> 

和CSS:你可以使用一些簡單的HTML做到這一點

.base, .inherited{ 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

.inherited{ 
    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 

這將共享屬性添加到這兩種類型的div,但具體的人只能得出div小號

20

使用這兩個類並將它們組合如下:

.baseClass 
{ 
    margin-top:3px; 
    margin-left:auto; 
    margin-right:auto; 
    margin-bottom:0px; 
} 

.baseClass.otherClass /* this means the element has both baseClass and otherClass */ 
{ 
    background-image:url("images/worldsource/customBackground.gif"); 
    background-repeat:no-repeat; 
    width:950px; 
    height:572px; 
} 

的標記如下:

<div class="baseClass otherClass"></div> 

現在,以這種方式,如果需要,您可以覆蓋基類...因爲你不必不斷添加新的類名的基類的定義,這是一個有點清潔器。

+2

這對我來說非常好! – 2013-06-06 04:52:23

2

如果希望所有與特定的類內的DIV從基類繼承建的HTML標記是這樣的:

<div class="parent"> 
    <div class="child">X</div> 
</div> 

而CSS

.parent { border: 2px solid red; color: white } 
.parent .child { background: black } 

如果所有div必須繼承將.child更改爲div

See this example on jsFiddle

下面的代碼

// parent  // all DIVs inside the parent 
.someClass div { } 

方式:頂部元件(任)用類someClass將樣式添加到其所有兒童的DIV(遞歸)。

+0

沒問題..謝謝你的回覆。 – jazzBox 2011-06-17 19:42:42