These properties can apply the colors to the elements.
There are 3 popular color models used in CSS. That are RGB, HSL, HEX.
- Color models
- Color Opacity
- Background color
- Background images
- Background positioning
- Background repetition
- Background attachment
- Gradient Text
-
RGB (Red Green Blue) Color Model:
- It represents colors by combining varying intensities of red, green, and blue colors.
- Syntax:
rgb(red, green, blue)
color: rgb(255, 0, 0); // Red
-
HSL (Hue Saturation Lightness) Color Model:
- It represents colors using hue (color), saturation (intensity), and lightness (brightness).
- Syntax:
hsl(hue, saturation%, lightness%)
color: hsl(0, 100%, 50%); // Red
-
Hexadecimal (HEX) Color Model:
- It represents colors using a six-character hexadecimal code (e.g., #RRGGBB) or in the 3 character short-code.
- Syntax:
#RRGGBB
or#RGB
color: #ff0000; // Red
-
RGBA (Red Green Blue Alpha) Color Model:
- It represents colors by combining varying intensities of red, green, and blue colors with alpha transparency for opacity of the color value.
- Alpha value ranges from
0
- no opacity to1
- full opacity. - Syntax:
rgb(red, green, blue, alpha)
color: rgba(255, 0, 0, 1); // Red
-
HSLA (Hue Saturation Lightness Alpha) Color Model:
- It represents colors using hue (color), saturation (intensity), and lightness (brightness) with alpha transparency for opacity of the color value.
- Alpha value ranges from
0
- no opacity to1
- full opacity.. - Syntax:
hsla(hue, saturation%, lightness%, alpha)
color: hsla(0, 100%, 50%, 1); // Red
-
Hexadecimal (HEX) Color Model:
- It represents colors using a six-character hexadecimal code (e.g., #RRGGBB) or in the 3 character short-code with alpha transparency for opacity of the color value.
- Alpha value ranges from
0
- no opacity tof
- full opacity for short-code or00
toff
for six-character hexadecimal code. - Syntax:
#RRGGBBAA
or#RGBA
color: #ff0000ff; // Red
It sets the background color of an element.
Syntax: background-color: value;
div {
background-color: #f0f0f0;
}
It adds an image to the background of an element.
Syntax: background-image: url(url);
body {
background-image: url("image.jpg");
}
It specifies the position of a background image within an element
Syntax: background-position: value;
or background-position: x-position y-position;
div {
background-position: center;
}
It controls how a background image is repeated
Syntax: background-repeat: value;
div {
background-repeat: no-repeat;
}
It determines whether a background image scrolls with the content or remains fixed.
Syntax: background-attachment: value;
body {
background-attachment: fixed;
}
The linear gradient property does not work with text. But it can be done with background-clip
property.
h1 {
background: linear-gradient(to right, red, blue);
background-clip: text;
color: transparent;
}