CSS
In this tutorial we will learn about CSS background property.
We use background-color
property to set the background color of the element. The value of this property can be name of the color, color in hex form, color in rgb and rgba value.
Click here for CSS Color tutorial.
In the following example we have set the background color of the div to lightyellow.
div {
background-color : lightyellow;
}
This is a sample paragraph.
We use the background-image
property to set background image of elements. By default, the image will be repeated to fill up the entire container.
The repeating of the background image is also known as tiling.
In the following example we are setting the background image of div.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
}
We can control the repeating (tiling) of background image using the background-repeat
property.
Different values for this property is listed below.
In the following example we are using repeat
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : repeat;
}
In the following example we are using repeat-x
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : repeat-x;
}
In the following example we are using repeat-y
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : repeat-y;
}
In the following example we are using no-repeat
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : no-repeat;
}
To control the position of the background image we use the background-position
property.
List of values this property can take is given below.
We can also specify the exact position using x% y%
or xpos ypos
.
In the following example we are using center center
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : no-repeat;
background-position : center center;
}
In the following example we are using 10% 20%
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : no-repeat;
background-position : 10% 20%;
}
In the following example we are using 10px 50px
.
div {
background-image : url(/image/dyclassroom-logo-black-311x48.png);
background-repeat : no-repeat;
background-position : 10px 50px;
}
We use background-attachment
property to make the background image either fixed or scrollable.
It takes two values - scroll (the default value) and fixed.
If the value is set to fixed then the image will not scroll with the rest of the content of the page.
We can combine the above background properties in the following manner.
div {
background: lightyellow url(/image/dyclassroom-logo-black-311x48.png) scroll center center no-repeat;
}
So, in the above rule we have set:
ADVERTISEMENT