欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CSS3中各種顏色屬性的使用教程

abgne   發(fā)布時間:2016-05-17 11:47:33   作者:佚名   我要評論
CSS3中在rgb顏色的基礎(chǔ)上又有了透明度的設(shè)置功能成為rgba屬性,除此之外下面還會講到CSS3新增的hsl及hsla顏色的用法,下面就來看一下CSS3中各種顏色屬性的使用教程

rgba 顏色
原本 CSS 中的顏色除了可用 16 進位來表示 RGB 顏色值之外,還可以使用預(yù)先定義好的顏色名稱。而 RGBA 是以 RGB 為基礎(chǔ)再加上不透明度的屬性。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. rgba(redgreenblue, opacity)  

其中的 red、green 及 blue 等顏色值是用 10 進位或是百分比(%)的方式來表示,允許的值為 0 到 255 之間的整數(shù)值;而 opacity 不透明度允許的值為 0 到 1 之間帶有小數(shù)的數(shù)值。

快速的看一下實際的范例:

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <body>  
  2.  <div class="rgba1">rgba(0, 0, 0, 1)</div>  
  3.  <div class="rgba2">rgba(100%, 100%, 0%, 0.75)</div>  
  4.  <div class="rgba3">rgba(144, 180, 60, 1)</div>  
  5.  <div class="rgba4">rgba(204, 102, 102)</div>  
  6.  <div class="rgba5">rgba(300, -102, 0, 1)</div>  
  7.     
  8.  <div id="log"></div>  
  9. </body>  

接著換 CSS:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. div {   
  2.  width220px;   
  3.  height50px;   
  4.  floatleft;   
  5.  margin-right10px;   
  6.  line-height50px;   
  7.  text-aligncenter;   
  8.  border1px solid #000;   
  9. }  

基本用法:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .rgba1 {   
  2.  /* 等同于 #ffffff 或是 rgba(100%, 100%, 100%, 1) */  
  3.  color: rgba(255, 255, 255, 1);   
  4.  /* 等同于 #000000 或是 rgba(0%, 0%, 0%, 1) */  
  5.  background-color: rgba(0, 0, 0, 1);   
  6. }   
  7.     
  8. .rgba3 {   
  9.  background-color: rgba(144, 180, 60, 1);   
  10. }  

2016517115126207.png (500×70)

除了給實際的顏色值之外,也可以用百分比(%)來表示:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .rgba2 {   
  2.  background-color: rgba(100%, 100%, 0%, 0.75);   
  3. }  

2016517115159144.png (255×70)

若是 opacity 值沒給的話,預(yù)設(shè)會用 0;這樣表示是透明(transparent)喔:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .rgba4 {   
  2.  /* 沒有指定 opacity 值時, 預(yù)設(shè)使用 0 */  
  3.  background-color: rgba(204, 102, 102);   
  4. }  

2016517115223803.png (255×70)

如果顏色值超出限定的范圍時,則會用最接近的數(shù)值:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .rgba5 {   
  2.  /* 超出范圍, 所以變成最接近的 rgba(255, 0, 0, 1) */  
  3.  background-color: rgba(300, -102, 0, 1);   
  4. }  

2016517115250487.png (255×70)

只要是顏色值的部份都能使用 rgab() 來設(shè)定。但當(dāng)要用程序來存取時,可能要注意一下各瀏覽器間的差異喔:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. $(function(){   
  2.  var str = '';   
  3.  $('div:not(#log)').each(function(){   
  4.   var $this = $(this);   
  5.   str += '.' + $this.attr('class') + ' : ' + $this.css('background-color') + '<br />';   
  6.  });   
  7.  $('#log').html(str);   
  8. });  

hsl 及 hsla 顏色
在 CSS3 中新增了 HSL 及 HSLA 等兩種跟顏色有關(guān)的屬性。其中 H 為 hue(色相)、S 為 saturation(飽合度)、L 為 lightness(亮度)。HSLA 就跟 RGBA 一樣,都是在原本的屬性中多加入了不透明度的設(shè)定而已。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. hsl(hue, saturation, lightness);   
  2.     
  3. hsla(hue, saturation, lightness, opacity);  

hue 為整數(shù)的角度值,基本上是從 0 到 360 之間,因為它是經(jīng)過一個簡單的計算來處理所輸入的值:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. (((x mod 360) + 360) mod 360)  

所以就算設(shè)定是 -10 的話,經(jīng)過計算后它也會變換成 350。而 0 或 360 表示紅色;60 表示黃色;120 表示綠色;240 表示藍色。

saturation 的表示方式為百分比(%);100% 就是最大飽合度,而 0% 則為灰色調(diào)。lightness 的表示方式也一樣是百分比(%);以 50% 為正常亮度為分界,百分比越高則會越接近白色(100%),而百分比越低則會越接近黑色(0%)。而 opacity 透明度允許的值為 0 到 1 之間帶有小數(shù)的數(shù)值。

一樣來看個簡單的范例:

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <body>  
  2.  <div class="hsl1">hsl(0, 50%, 50%)</div>  
  3.  <div class="hsl2">hsl(240, 80%, 50%)</div>  
  4.  <div class="hsl3">hsl(30, 100%, 50%)</div>  
  5.  <div class="hsla1">hsla(0, 100%, 0%, 0.8)</div>  
  6.  <div class="hsla2">hsla(210, 100%, 50%, 0.8)</div>  
  7.     
  8.  <div id="log"></div>  
  9. </body>  

來看看 CSS 的用法:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. div {   
  2.  width220px;   
  3.  height50px;   
  4.  floatleft;   
  5.  margin-right10px;   
  6.  line-height50px;   
  7.  text-aligncenter;   
  8.  border1px solid #000;   
  9. }   
  10. .hsl1 {   
  11.  color: hsl(0, 100%, 100%);   
  12.  background-color: hsl(0, 100%, 50%);   
  13. }   
  14. .hsl2 {   
  15.  color: hsl(0, 100%, 100%);   
  16.  background-color: hsl(240, 50%, 50%);   
  17. }   
  18. .hsl3 {   
  19.  background-color: hsl(30, 100%, 50%);   
  20. }   
  21. .hsla1 {   
  22.  color: hsl(0, 100%, 100%);   
  23.  background-color: hsla(0, 100%, 0%, 0.8);   
  24. }   
  25. .hsla2 {   
  26.  background-color: hsla(210, 100%, 50%, 0.8);   
  27. }  

2016517115405278.png (500×240)

基本上的用法就跟 rgb() 及 rgba() 一樣。但有趣的是,當(dāng)筆者嘗試使用 jQuery 取出顏色值時,原本用 hsl() 或 hsdl() 來設(shè)定的都會是轉(zhuǎn)換成 rgb() 及 rgba() 后的值。

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. $(function(){   
  2.  var str = '';   
  3.  $('div:not(#log)').each(function(){   
  4.   var $this = $(this);   
  5.   str += '.' + $this.attr('class') + ' : ' + $this.css('background-color') + '<br />';   
  6.  });   
  7.  $('#log').html(str);   
  8. });  

相關(guān)文章

最新評論