CSS は、色名、16進数、RGB をサポートしています。
さらに、CSS3 では次の指定も加えられました:
表中の数字は、完全にCSS3 色の値/プロパティをサポートした最初のブラウザのバージョンです。
プロパティ | |||||
---|---|---|---|---|---|
RGBA, HSL, および HSLA | 4.0 | 9.0 | 3.0 | 3.1 | 10.1 |
opacity | 4.0 | 9.0 | 2.0 | 3.1 | 10.1 |
RGBA カラー値は、色の不透明度を指定するαチャンネルによって、RGB カラー値を拡張したものです。
RGBA カラー値は、次のように指定します:rgba(red, green, blue, alpha)。 alpha パラメータは、0.0(完全に透明)から 1.0(完全に不透明)の間の数です。
次の例は、いろいろな RGBA カラーを定義しています:
#p1 {background-color: rgba(255, 0, 0, 0.3);} /* red
with opacity */
#p2 {background-color: rgba(0, 255, 0, 0.3);} /* green with opacity */
#p3 {background-color: rgba(0, 0, 255, 0.3);} /* blue
with opacity */
Try it Yourself ❯
HSL は、Hue(色相)、Saturation(彩度)および Lightness(明度)の略です。
>HSL カラー値は、次のように指定します:hsl(hue, saturation, lightness)。
次の例は、いろいろな HSL カラーを定義しています:
#p1 {background-color: hsl(120, 100%, 50%);} /* green */
#p2 {background-color: hsl(120, 100%, 75%);} /* light green */
#p3 {background-color: hsl(120, 100%, 25%);} /* dark
green */
#p4 {background-color: hsl(120, 60%, 70%);} /* pastel green */
Try it Yourself ❯
HSLA カラー値は、色の不透明度を指定するαチャンネルによって、HSL カラー値を拡張したものです。
HSLA カラー値は、次のように指定します:hsla(hue, saturation, lightness, alpha)。ここで、alpha パラメータは不透明度を定義します。 alpha パラメータは、0.0(完全に透明)から 1.0(完全に不透明)の間の数です。
次の例は、いろいろな HSLA カラーを定義しています:
#p1 {background-color: hsla(120, 100%, 50%, 0.3);} /* green with opacity */
#p2 {background-color: hsla(120, 100%, 75%, 0.3);} /* light green with opacity */
#p3 {background-color: hsla(120, 100%, 25%, 0.3);} /* dark
green with opacity */
#p4 {background-color: hsla(120, 60%, 70%, 0.3);} /* pastel green
with opacity */
Try it Yourself ❯
CSS3 opacity プロパティは、指定した RGB 値の不透明度を設定します。
opacity プロパティ値は、0.0(完全に透明)から 1.0(完全に不透明)の間の数字でなければなりません。
上では、テキストも不透明になることに注意してください。
次の例は、opacity を持つ、いろいろな RGB 値を表示します:
#p1 {background-color:rgb(255,0,0);opacity:0.6;} /* red with opacity
*/
#p2 {background-color:rgb(0,255,0);opacity:0.6;} /* green with
opacity */
#p3 {background-color:rgb(0,0,255);opacity:0.6;} /* blue
with opacity */
Try it Yourself ❯
Exercise 1 ❯ Exercise 2 ❯ Exercise 3 ❯ Exercise 4 ❯