Learn CSS Colors: A Complete Guide
Colors are an important part of CSS, as they allow you to change the color of text, backgrounds, borders, and other elements on your web page.
CSS supports a variety of color formats, including:
Hexadecimal
Hexadecimal colors use a pound sign (#) followed by six digits (three pairs of two-digit numbers) to represent red, green, and blue (RGB) values.
As an example:
div {
color: #007bff;
background-color: #ffffff;
border: 1px solid #cccccc;
}
RGB
RGB colors use the red, green, and blue color channels to create a wide range of colors. RGB colors can be specified using the rgb()
function, which takes three values between 0 and 255.
As an example:
div {
color: rgb(0, 123, 255);
background-color: rgb(255, 255, 255);
border: 1px solid rgb(204, 204, 204);
}
RGBA
RGBA colors are similar to RGB colors, but they also include an alpha channel that specifies the opacity of the color. The alpha channel is represented by a value between 0 and 1.
As an example:
div {
color: rgba(0, 123, 255, 0.5);
background-color: rgba(255, 255, 255, 0.5);
border: 1px solid rgba(204, 204, 204, 0.5);
}
HSL
HSL stands for hue, saturation, and lightness, and is another way to specify colors in CSS. The hue value represents the color itself, saturation represents the intensity of the color, and lightness represents the brightness of the color. HSL colors can be specified using the hsl()
function, which takes three values.
As an example:
div {
color: hsl(210, 100%, 50%);
background-color: hsl(0, 0%, 100%);
border: 1px solid hsl(0, 0%, 80%);
}
HSLA
HSLA colors are similar to HSL colors, but they also include an alpha channel that specifies the opacity of the color. The alpha channel is represented by a value between 0 and 1.
As an example:
div {
color: hsla(210, 100%, 50%, 0.5);
background-color: hsla(0, 0%, 100%, 0.5);
border: 1px solid hsla(0, 0%, 80%, 0.5);
}
Named Colors
CSS also supports a set of 147 named colors, such as red
, green
, blue
, etc. These named colors can be used to quickly and easily apply colors to your web page.
As an example:
div {
color: blue;
background-color: white;
border: 1px solid lightgray;
}
In this example:
- The
color
property is used to change the text color to blue. - The
background-color
property is used to set the background color to white with 50%
opacity, and the border property is used to set the border
color to light gray.