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.
background-color
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.
background-image
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.
background-repeat
Different values for this property is listed below.
In the following example we are using repeat.
repeat
div { background-image : url(/image/dyclassroom-logo-black-311x48.png); background-repeat : repeat; }
In the following example we are using repeat-x.
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.
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.
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.
background-position
List of values this property can take is given below.
We can also specify the exact position using x% y% or xpos ypos.
x% y%
xpos ypos
In the following example we are using center center.
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%.
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.
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.
background-attachment
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: