Colors are specified by the color
property, which is defined using various techniques like predefined color names, RGB, HEX, HSL, RGBA, HSLA values.
Format | Syntax | Example |
---|---|---|
Predefined Color | red, green, blue etc. | h1 { color: red } |
RGB | rgb(rrr, ggg, bbb) | rgb(255, 91, 77) |
RGBA | rgba(rrr, ggg, bbb, a) | rgba(255, 91, 77, 0.7) |
HEX | #rrggbb | #00ff00; |
HSL | hsl(h, s, l); | hsl(9, 100%, 65%) |
HSLA | hsla(h, s, l, a); | hsla(9, 100%, 65%, 0.7) |
h1 {
color: green;
}
RGB stands for 'red, green, and blue'. RGB is used for defining the color of any HTML element by specifying the values of RGB, that are in the range of 0 to 255.
h1 {
color: rgb(255, 91, 77);
}
h1 {
color: green;
}
h1 {
color: green;
}
h1 {
color: green;
}
h1 {
color: green;
}
Background properties in CSS are used to define background styles of the HTML elements. There are various background properties available for styling in CSS, which are given bewlow-
It is used to specify or set the background color by the CSS background-color
property.
h1 {
background-color: green;
}
It is used to specify or set the background image as a background of an element by the CSS background-image
property.
body {
background-image: url("image.gif");
}
By default the background-image
property in CSS repeats an image in horizontal and vertical direction, but by using background-repeat
property we can control this default behaviour. We can easily set a background image repeat vertically (i.e. y-axis), and horizontally (i.e. x-axis), in both directions, or in neither direction with background-repeat
property.
body {
background-image: url("image.png");
background-repeat: repeat-x;
}
The background-attachment
property defines whether the background image should scroll or should be fixed with the web page.
body {
background-image: url("imgage.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
By default the background-image
property in CSS placed the image at the top-left position of the element i.e. at (0,0)
, but we can control the position of the background image by using background-position
property.
body {
background-image: url("imgage.png");
background-repeat: no-repeat;
background-position: right top;
}
We can specify all the background properties in one single property, known as shorthand property (i.e. background
). The order of the background property values are:- background-color
, background-image
, background-repeat
, background-attachment
, background-position
.
body {
background: #ffffff url("imgage.png") no-repeat right top;
}