在CSS3中常用的幾種色彩坡形模式

來源:互聯網
上載者:User
現在html5 css3已經越來越流行,用CSS3實現p漸層已經不是什麼難事了,這篇文章給大家整理了現在常用的三種色彩坡形模式,包括線性漸層、放射狀漸層和重複的線性漸層,文中通過範例程式碼介紹的很詳細,有需要的朋友們可以參考借鑒,下面來一起看看吧。

一、線性漸層:linear-gradient

文法:

  <linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner] ,]? <color-start>[, <color-end>]+)        <side-or-corner> = [left | right] || [top | bottom]        <color-start|end> = <color>[ <length>|<percentage>]?

下述值用來表示漸層的方向,可以使用角度或者關鍵字來設定:

<angle>:用角度值指定漸層的方向(或角度)。

to left:設定漸層為從右至左。相當於: 270deg

to right:設定漸層從左至右。相當於: 90deg

to top:設定漸層從下到上。相當於: 0deg

to bottom:設定漸層從上到下。相當於: 180deg。這是預設值,等同於留空不寫。

<color-start|end> 用於指定漸層的起止顏色:

<color>:指定顏色。

<length>:用長度值指定起止色位置。不允許負值

<percentage>:用百分比指定起止色位置。

樣本:

p {      width: 200px;      height: 100px;      margin: 10px 5px;      border: 1px solid #ddd000;  }  #LinearStartToEnd {    float:left;    background: linear-gradient(#ff0000, #00ff00);  }  #LinearPercentage {    float:left;    background: linear-gradient(#0000ff, #ff0000 52%, #00ff00);  }  #LinearAnglePercentage {    float:left;    background: linear-gradient(90deg, #ff0000 20%, #00ff00 50%, #000000 80%);  }  #LinearAngle {    float:left;    background: linear-gradient(30deg, #ffff00 30%, #ff0000, #00ff00);  }  #LinearTopRight {    float:left;    background: linear-gradient(to right top, #00ff00, #ff0000 50%, #0000ff);  }

二、放射狀漸層:radial-gradient

文法:

  <position> = [ <length>① | <percentage>① | left | center① | right ]? [ <length>② | <percentage>② | top | center② | bottom ]?     <shape> = circle | ellipse     <size> = <extent-keyword>|[<circle-size>||<ellipse-size>]     <extent-keyword> = closest-side | closest-corner | farthest-side | farthest-corner     <circle-size> = <length>     <ellipse-size> = [ <length>| <percentage> ]{2}     <shape-size> = <length>| <percentage>     <radial-gradient> = radial-gradient([ [ <shape>|| <size> ] [ at <position> ]? , | at <position> , ]?<color-start>[[ , <color-end>]]+)

<position> 確定圓心的位置。如果提供2個參數,第一個表示橫座標,第二個表示縱座標;如果只提供一個,第二值預設為50%,即center

<length>①:用長度值指定放射狀漸層圓心的橫座標值。可以為負值。

<percentage>①:用百分比指定放射狀漸層圓心的橫座標值。可以為負值。

<length>②:用長度值指定放射狀漸層圓心的縱座標值。可以為負值。

<percentage>②:用百分比指定放射狀漸層圓心的縱座標值。可以為負值。

center①:設定中間為放射狀漸層圓心的橫座標值。

center②:設定中間為放射狀漸層圓心的縱座標值。

left:設定左邊為放射狀漸層圓心的橫座標值。

right:設定右邊為放射狀漸層圓心的橫座標值。

top:設定頂部為放射狀漸層圓心的縱座標值。

bottom:設定底部為放射狀漸層圓心的縱座標值。

<shape> 確定圓的類型

circle:指定圓形的放射狀漸層

ellipse:指定橢圓形的放射狀漸層。

<extent-keyword> circle | ellipse 都接受該值作為 size。

closest-side:指定放射狀漸層的半徑長度為從圓心到離圓心最近的邊。

closest-corner:指定放射狀漸層的半徑長度為從圓心到離圓心最近的角。

farthest-side:指定放射狀漸層的半徑長度為從圓心到離圓心最遠的邊。

farthest-corner:指定放射狀漸層的半徑長度為從圓心到離圓心最遠的角。

<circle-size> circle 接受該值作為 size。

<length>:用長度值指定正圓放射狀漸層的半徑長度。不允許負值。

<ellipse-size> ellipse 接受該值作為 size。

<length>:用長度值指定橢圓放射狀漸層的橫向或縱向半徑長度。不允許負值。

<percentage>:用百分比指定橢圓放射狀漸層的橫向或縱向半徑長度。不允許負值。

樣本:

#RadialCenterCircle {    float:left;      background: radial-gradient(circle at center, #ff0000, #ffff00, #00ffff);  }  #RadialClosestSide {    float:left;      background: radial-gradient(circle closest-side, #ff0000, #00ff00, #ffff00);  }  #RadialFarthestSide {    float:left;      background: radial-gradient(farthest-side, #ff0000 20%, #ffff00 60%, #00ff00 80%);  }  #RadialRightTop {    float:left;      background: radial-gradient(at right top, #ff0000, #ffff00, #00ff00);  }  #RadialRadiusCenter {    float:left;      background: radial-gradient(farthest-side at top right, #ff0000, #ffff00, #01fefe);  }  #RadialGroup {    float:left;      background:          radial-gradient(farthest-side at top right, #ff0000, #ffff00, #009f00, transparent),          radial-gradient(60px at top left, #ff0000, #ffff00, #00ff0e);  }

三、重複的線性漸層:repeating-linear-gradient

文法和參數類似線性漸層,這裡不在贅述。詳情請參考CSS手冊。

樣本:

#RepeatingLinearPercentage{    float:left;      background: repeating-linear-gradient(#ff0000, #00ff00 10%, #000000 15%);  }  #RepeatingLinearRight {    float:left;      background: repeating-linear-gradient(to right, #ff0000, #00ff00 10%, #000000 15%);  }  #RepeatingLinearAngle {    float:left;      background: repeating-linear-gradient(45deg, #ff0000, #00ff00 10%, #0000ff 15%);  }  #RepeatingLinearBottomLeft {    float:left;      background: repeating-linear-gradient(to bottom left, #00ffff, #ff0000 10%, #00ff00 15%);  }

四、重複的放射狀漸層:repeating-radial-gradient

文法和參數類似放射狀漸層,這裡不在贅述。詳情請參考CSS手冊。

樣本:

#RepeatingRadialCircle {    float:left;      background: repeating-radial-gradient(circle, #ff0000 0, #00ff00 10%, #0000ff 15%);  }  #RepeatingRadialTopLeft {    float:left;      background: repeating-radial-gradient(at top left, #ff0000, #00ff00 10%, #0de0f0 15%, #ffff00 20%, #000000 25%);  }  #RepeatingRadialClosestCorner {    float:left;      background: repeating-radial-gradient(circle closest-corner at 20px 50px, #00ff00, #ff0000 10%, #00ffff 20%, #ffff00 30%, #ff00ff 40%);  }

完整的例子:

<!DOCTYPE html>  <html>  <head>  <meta charset="utf-8" />  <title>ImageCSS3</title>  <style>  p {      width: 200px;      height: 100px;      margin: 10px 5px;      border: 1px solid #ddd000;  }  #LinearStartToEnd {    float:left;    background: linear-gradient(#ff0000, #00ff00);  }  #LinearPercentage {    float:left;    background: linear-gradient(#0000ff, #ff0000 52%, #00ff00);  }  #LinearAnglePercentage {    float:left;    background: linear-gradient(90deg, #ff0000 20%, #00ff00 50%, #000000 80%);  }  #LinearAngle {    float:left;    background: linear-gradient(30deg, #ffff00 30%, #ff0000, #00ff00);  }  #LinearTopRight {    float:left;    background: linear-gradient(to right top, #00ff00, #ff0000 50%, #0000ff);  }    #RadialCenterCircle {    float:left;      background: radial-gradient(circle at center, #ff0000, #ffff00, #00ffff);  }  #RadialClosestSide {    float:left;      background: radial-gradient(circle closest-side, #ff0000, #00ff00, #ffff00);  }  #RadialFarthestSide {    float:left;      background: radial-gradient(farthest-side, #ff0000 20%, #ffff00 60%, #00ff00 80%);  }  #RadialRightTop {    float:left;      background: radial-gradient(at right top, #ff0000, #ffff00, #00ff00);  }  #RadialRadiusCenter {    float:left;      background: radial-gradient(farthest-side at top right, #ff0000, #ffff00, #01fefe);  }  #RadialGroup {    float:left;      background:                  radial-gradient(farthest-side at top right, #ff0000, #ffff00, #009f00, transparent),                  radial-gradient(60px at top left, #ff0000, #ffff00, #00ff0e);  }    #RepeatingLinearPercentage{    float:left;      background: repeating-linear-gradient(#ff0000, #00ff00 10%, #000000 15%);  }  #RepeatingLinearRight {    float:left;      background: repeating-linear-gradient(to right, #ff0000, #00ff00 10%, #000000 15%);  }  #RepeatingLinearAngle {    float:left;      background: repeating-linear-gradient(45deg, #ff0000, #00ff00 10%, #0000ff 15%);  }  #RepeatingLinearBottomLeft {    float:left;      background: repeating-linear-gradient(to bottom left, #00ffff, #ff0000 10%, #00ff00 15%);  }    #RepeatingRadialCircle {    float:left;      background: repeating-radial-gradient(circle, #ff0000 0, #00ff00 10%, #0000ff 15%);  }  #RepeatingRadialTopLeft {    float:left;      background: repeating-radial-gradient(at top left, #ff0000, #00ff00 10%, #0de0f0 15%, #ffff00 20%, #000000 25%);  }  #RepeatingRadialClosestCorner {    float:left;      background: repeating-radial-gradient(circle closest-corner at 20px 50px, #00ff00, #ff0000 10%, #00ffff 20%, #ffff00 30%, #ff00ff 40%);  }    </style>  </head>  <body>  <!-- 指定線性漸層起止色 -->  <p id="LinearStartToEnd"></p>  <!-- 指定線性漸層起止色位置 -->  <p id="LinearPercentage"></p>  <!-- 指定線性漸層色彩坡形方向和起止色位置 -->  <p id="LinearAnglePercentage"></p>  <!-- 指定線性漸層色彩坡形方向 -->  <p id="LinearAngle"></p>  <!-- 設定漸層從右上到左下 -->  <p id="LinearTopRight"></p>    <!-- 浮動p換行,此處指定p寬高和邊界,是為了覆蓋前面定義的p統一CSS樣式,   可以嘗試去掉指定的p寬高和邊界,看看效果 -->  <p style="width:0; height:0; border:none; clear:both"></p>  <!-- 以中心點為圓心的圓形放射狀漸層 -->  <p id="RadialCenterCircle"></p>  <!-- 放射狀漸層半徑長度:圓心到離圓心最近邊的長度 -->  <p id="RadialClosestSide"></p>  <!-- 放射狀漸層半徑長度:圓心到離圓心最遠邊的長度 -->  <p id="RadialFarthestSide"></p>  <!-- 左邊為放射狀漸層圓心的橫座標值,頂邊為放射狀漸層圓心的縱座標值 -->  <p id="RadialRightTop"></p>  <!-- 同時指定放射狀漸層的圓心和半徑 -->  <p id="RadialRadiusCenter"></p>  <!-- 放射狀漸層組合 -->  <p id="RadialGroup"></p>    <p style="width:0; height:0; border:none; clear:both"></p>  <!-- 指定顏色起止色位置的重複線性漸層 -->  <p id="RepeatingLinearPercentage"></p>  <!-- 從左至右漸層的重複線性漸層 -->  <p id="RepeatingLinearRight"></p>  <!-- 漸層角度為45度的重複線性漸層 -->  <p id="RepeatingLinearAngle"></p>  <!-- 從左下到右上的重複線性漸層 -->  <p id="RepeatingLinearBottomLeft"></p>    <p style="width:0; height:0; border:none; clear:both"></p>  <!-- 圓形重複放射狀漸層 -->  <p id="RepeatingRadialCircle"></p>  <!-- 漸層方向為左上到右下的重複放射狀漸層 -->  <p id="RepeatingRadialTopLeft"></p>  <!-- 重複放射狀漸層:漸層半徑長度為從圓心到離圓心最近的角的距離 -->  <p id="RepeatingRadialClosestCorner"></p>    </body>  </html>

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.