2011-07-08 33 views
3

我想用CSS3做一點小事...我想設置一堆與類「.myClass」的元素梯度帶有-xxx線性漸變(爲了兼容性,使用colorzilla.com/gradient-editor生成)。 但是,這些元素中的每個元素都帶有一個Id,並且根據Id,我想在漸變的頂部添加不同的背景圖像(如在多背景語句中)。 像這樣:多個背景圖像/梯度相同的元素,在不同的聲明中聲明,在CSS3

.myClass { 
    /* linear gradient... */ 
} 

#myId1 { 
    background-image: url(img1.png); /* or background: url(...), same result */ 
} 

#myId2 { 
    background-image: url(img2.png); 
} 

的問題是,與#Ids定義的圖像替換而不是使多個背景的漸變。我可以將漸變CSS代碼複製到每個ID,然後有多個背景,但colorzilla給出的兼容性代碼對於漸變非常巨大,因此將其複製到每個ID中會使代碼更難以讀取...

ID有優先權,但我想知道是否有辦法「添加」第二個背景圖像而不是替換它。

如果您有任何想法,非常感謝!

+0

你的元素有多大?他們有固定的大小嗎? – knut

+0

我真的不知道在哪裏回答你們所有人,因爲看起來我不允許「在我的消息中添加答案」:)顯然,在不同的CSS語句中使用多個背景功能是沒有辦法的,所以我會暫時忘記漸變。謝謝你們。哦,我也把所有的CSS放在不同的CSS文件中,與HTML不同,我的意思是Shauna說。 –

+0

好吧,現在我得到了爲什麼我不能發佈一個答案,這不是一個答案:) –

回答

3

您不能在單獨的後臺語句中使用多個背景。如果你想要多個背景,你必須在一個背景聲明中聲明它們。這是因爲在多個後臺語句中,渲染引擎假設您的意思是將新設置的背景替換爲新設置的背景,而不是添加其中的內容。

我通常用CSS3做的一件事情,特別是當仍需要所有供應商前綴時,就是將所有CSS3放入它自己的文件中。這樣,其餘部分保持可讀性,並且不會混入主CSS中。

+0

嗯,是的,你不能,但你可以使用僞元素添加自定義圖像到您的常見漸變... – llange

-1

試試這個。將.myClass中的所有background替換爲background-color

+0

如果它不工作cud你,請列出.myClass的全部內容 – shahalpk

+0

漸變不是背景顏色,它們是圖像值。 –

+0

他說這是一個線性漸變。很明顯,'background-color'不起作用。 – BoltClock

0

如果使用得少CSS,你可以做到以下幾點:

/** 
* Defines a horizontal gradient, but allows additional image url's to be passed  through the CSS3 compliant browsers to display multiple i 
* image on the one element 
*/ 

.background-gradient-multiple(@start-color, @end-color) { 
    .background-gradient-horizontal(@start-color, @end-color); 
} 
.background-gradient-multiple(@start-color, @end-color, @additional-background) { 
    .background-gradient-horizontal(@start-color, @end-color, @additional-background ~ ','); 
} 
.background-gradient-horizontal(@start-color, @end-color, @additional-background: ~'') { 
    background-color: @start-color; 
    background-image: @additional-background -moz-linear-gradient(left, @start-color, @end-color); 
    background-image: @additional-background -webkit-gradient(linear, 0 0, 100% 0, from(@start-color), to(@end-color)); 
    background-image: @additional-background -webkit-linear-gradient(left, @start-color, @end-color); 
    background-image: @additional-background -o-linear-gradient(left, @start-color, @end-color); 
    background-image: @additional-background linear-gradient(to right, @start-color, @end-color); 
    filter: progid:DXImageTransform.Microsoft.gradient([email protected], [email protected], GradientType=0);   
} 

在一個類來執行這一點,你可以這樣做:

.class { .background-gradient-multiple(#000000, #FFFFFF, 'url(images/test.gif) no-repeat top left'); } 
0

使用CSS僞元素,你可以在這裏實現了預期的結果是一個例子: http://jsfiddle.net/2s9uj24m/

<div id="alfa">Alfa</div> 
<div id="bravo">Bravo</div> 
<div id="charlie">Charlie</div> 

竟被CSS ð看起來像:

div 
{ position:relative; 
    background:linear-gradient(to top, #aaa, #eee); 
} 
div::after 
{ content:" "; 
    position:absolute; 
    top:0;right:0;bottom:0;left:0; 
} 
#alfa::after{background-image:url();} 
#bravo::after{background-image:url();} 
#charlie::after{background-image:url();} 

PS:不幸的是沒有在CSS「陣列」​​或「變量」的概念......那豈不是巨大的,有一個CSS文件看起來像:

@variables 
{ color1:#aaa; 
    color2:#eee; 
    var:"the/path/to/an/image.png"; 
} 
.myclass 
{ background:linear-gradient(to top, @color1, @color2),url(@var); 
    other-general-css-properties-for-that-king-of-elements... 
} 
#myID 
{ @var:"the/path/to/another/image.png"; 
} 
:target 
{ @color1:#777; 
    @color2:#aaa; 
} 

只要能夠有顏色的變量將是這樣一個節省時間!

相關問題