English 中文(简体)
CSS3 - Color
  • 时间:2024-09-17

CSS3 - Colors


Previous Page Next Page  

CSS3 has Supported additional color properties as follows −

    RGBA colors

    HSL colors

    HSLA colors

    Opacity

RGBA stands for Red Green Blue Alpha.It is an extension of CSS2,Alpha specifies the opacity of a color and parameter number is a numerical between 0.0 to 1.0. A Sample syntax of RGBA as shown below −

#d1 {background-color: rgba(255, 0, 0, 0.5);} 
#d2 {background-color: rgba(0, 255, 0, 0.5);}  
#d3 {background-color: rgba(0, 0, 255, 0.5);}

HSL stands for hue, saturation, pghtness.Here Huge is a degree on the color wheel, saturation and pghtness are percentage values between 0 to 100%. A Sample syntax of HSL as shown below −

#g1 {background-color: hsl(120, 100%, 50%);}  
#g2 {background-color: hsl(120, 100%, 75%);}  
#g3 {background-color: hsl(120, 100%, 25%);}

HSLA stands for hue, saturation, pghtness and alpha. Alpha value specifies the opacity as shown RGBA. A Sample syntax of HSLA as shown below −

#g1 {background-color: hsla(120, 100%, 50%, 0.3);}  
#g2 {background-color: hsla(120, 100%, 75%, 0.3);}  
#g3 {background-color: hsla(120, 100%, 25%, 0.3);}  

opacity is a thinner paints need black added to increase opacity. A sample syntax of opacity is as shown below −

#g1 {background-color:rgb(255,0,0);opacity:0.6;}  
#g2 {background-color:rgb(0,255,0);opacity:0.6;}  
#g3 {background-color:rgb(0,0,255);opacity:0.6;} 

The following example shows rgba color property.

<html>
   <head>
      <style>
         #p1 {background-color:rgba(255,0,0,0.3);}
         #p2 {background-color:rgba(0,255,0,0.3);}
         #p3 {background-color:rgba(0,0,255,0.3);}
      </style>
   </head>

   <body>
      <p>RGBA colors:</p>
      <p id = "p1">Red</p>
      <p id = "p2">Green</p>
      <p id = "p3">Blue</p>
   </body>
</html>

It will produce the following result −