CSS (Cascading Style Sheets) supports various color values that can be used to define colors for elements on a web page. Here are some commonly used CSS color values:
1. Named Colors
CSS supports a set of predefined color names, which can be used directly. For example:
color: red;
background-color: aqua;
border-color: red;
2. Hexadecimal Notation
This is the most common way to represent colors in CSS. Hex values use a combination of six hexadecimal digits (0-9 and A-F) preceded by a #
symbol to define a color. The first two digits represent the red value, the next two digits represent the green value, and the last two digits represent the blue value.
color: #FF0000; /* Red */
background-color: #00FF00; /* Green */
Short Hexadecimal Notation
If the red, green, and blue values are identical, you can use a hexadecimal shorthand notation with only three characters. For example:
color: #F00; /* (short for #FF0000) */
background-color: #0F0; /* (short for #00FF00) */
border-color: #00F; /* (short for #0000FF) */
3. RGB Function
The rgb()
function allows you to specify color values using individual red, green, and blue values ranging from 0 to 255. For example:
color: rgb(255, 0, 0); /* (red) */
background-color: rgb(0, 255, 0); /* (green) */
border-color: rgb(0, 0, 255); /* (blue) */
In CSS, the rgb()
functional notation can also accept values in percentage for the RGB color model. Instead of using integer values from 0 to 255 for red, green, and blue, you can use percentages from 0% to 100%. For example:
/* Pure red (100% red, 0% green, 0% blue) */
color: rgb(100%, 0%, 0%);
/* Dark green (0% red, 50% green, 0% blue) */
background-color: rgb(0%, 50%, 0%);
/* Shade of blue (20% red, 40% green, 80% blue) */
border-color: rgb(20%, 40%, 80%);
4. RGBA Function
Similar to the rgb()
function, the rgba()
function allows you to add an alpha (transparency) value, ranging from 0 to 1, to the color. A value of 0 means fully transparent, and 1 means fully opaque. For example:
color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
background-color: rgba(0, 255, 0, 0.8); /* Semi-transparent green */
5. HSL Function
The hsl()
function allows you to specify color values using hue (0 to 360 degrees), saturation (0% to 100%), and lightness (0% to 100%). For example:
color: hsl(0, 100%, 50%); /* (red) */
background-color: hsl(120, 100%, 50%); /* (green) */
border-color: hsl(240, 100%, 50%); /* (blue) */
6. HSLA Function
Similar to the hsl()
function, the hsla()
function allows you to add an alpha (transparency) value. For example:
/* Red with 100% opacity */
color: hsla(0, 100%, 50%, 1);
/* Semi-transparent green */
background-color: hsla(120, 100%, 50%, 0.7);
/* Blue with 80% lightness and 50% opacity */
border-color: hsla(240, 100%, 80%, 0.5);
7. currentColor
currentColor
is a special keyword that refers to the current value of the color property. It is useful for applying the same color to other properties. For example:
color: blue;
border: 2px solid currentColor; /* Border color will be the same as the text color (blue) */
8. Transparent
transparent
is a keyword used to make an element fully transparent, effectively making it invisible.
border: 2px solid transparent; /* Transparent border */
background-color: transparent; /* Transparent background */
9. System Colors
CSS supports some system-specific colors like "ButtonFace," "WindowText," etc. However, these are less commonly used.
color: ButtonText;
background-color: ButtonFace;
These are some of the common ways to define color values in CSS. You can use any of these color value formats like HSL, HSLA, and RGB/RGBA to define colors for text, backgrounds, box shadows, borders, and other CSS properties. Now, go ahead and experiment with different CSS color values to create beautiful and visually appealing websites!