2016-02-09 4 views
8

div複数の背景色や画像をCSSに与えるにはどうすればよいですか?たとえば、上部を赤、中間を青、下を緑にします。複数の背景色、画像をCSSで

+2

こんにちは、このスレッドを見てみましょう。 http://stackoverflow.com/a/6457484/4362192 – cdslijngard

答えて

7

単一linear-gradient背景が

background: linear-gradient(to bottom, 
    red 0, red 33.33%, 
    blue 0, blue 66.66%, 
    green 0, green 100% 
); 

Example十分であるとして| Browser support

+0

ニース。 %記号のない0が何をしているのか説明できますか? –

+1

カラーストップの仕組み:前回のカラーストップよりも値が小さいカラーストップを設定すると、前に定義した最大値に自動的に設定されます。つまり、赤色0、赤色33.33%、青色33.33、青色66.66%、緑色66.66、緑色100%と書かれていますが、反復値が少ない(たとえば、66.66を80%に変更した場合は、 – fcalderan

3

css3のグラデーションが使用されます。

YourDIVID { 
    background: red; /* For browsers that do not support gradients */ 
    background: -webkit-linear-gradient(red, yellow, green); /* For Safari 5.1 to 6.0 */ 
    background: -o-linear-gradient(red, yellow, green); /* For Opera 11.1 to 12.0 */ 
    background: -moz-linear-gradient(red, yellow, green); /* For Firefox 3.6 to 15 */ 
    background: linear-gradient(red, yellow, green); /* Standard syntax */ 
} 
2

TOP、BOTTOM、ビルドアップ配線板等行

#multiple { 
width: 700px; 
height: 500px; 
background:linear-gradient(red, yellow, green, blue, purple, orange); 

}

センター複数色チュートリアル

1

:before and :after pseudo element rulesを使用すると、1つの要素に3つの異なる背景レイヤー(色と画像)を持たせることができます。

注:このソリューションでは、含まれている要素を背景の上に収める必要があります。そうでなければ、リンクはクリック可能ではなく、テキストは選択できません。

実際の例のコードスニペットを実行します。

.layered-backgrounds { 
 
    background-color: #C63; 
 
    background-image: url(https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png); 
 
    background-repeat: no-repeat; 
 
    background-position: center center; 
 
    background-size: cover; 
 
    position: relative; 
 
    height: 400px; 
 
    z-index: 1; 
 
} 
 

 
.layered-backgrounds:before { 
 
    position: absolute; 
 
    content: " "; 
 
    top: 0; 
 
    height: 120px; 
 
    width: 100%; 
 
    background: #39C; 
 
    z-index: 2; 
 
    opacity: 0.8; 
 
} 
 

 
.layered-backgrounds:after { 
 
    position: absolute; 
 
    content: " "; 
 
    bottom: 0; 
 
    height: 120px; 
 
    width: 100%; 
 
    background: #9C3; 
 
    z-index: 3; 
 
    opacity: 0.8; 
 
} 
 

 
.layered-backgrounds .content { 
 
    position: relative; 
 
    z-index: 4; 
 
}
<div class="layered-backgrounds"> 
 
    <div class="content"> 
 
     <a href="#">Clickable</a> and selectable contents. 
 
    </div> 
 
</div>