Like other elements, you can format background of any element with the help of style sheet. CSS has enhanced features than HTML to set background of an element.
You can set background of an element in 3 different ways of your choice or page requirement.
An element can have following three attributes:-
- Background Color
- Background Image
- Background Color + Image (both)
When you use 2nd and 3rd attribute, further these have 3 common attributes to set background position, attachment, and background repeat:-
- Background Attachment
- Background Position
- Background Repeat
How to set Background property with css coding?
Now let us discuss all the attributes of Background with an example of each:-
Background-Color
If you want to set color for your HTML page or any other element created on the page, you can use this attribute to set the color.
Example for setting Background Color:-
/*Syntax of Background Color in CSS*/
height:200px;
width:200px;
background-color:red;
height:200px;
width:200px;
background-color:red;
Above example will create a square box of 200px height and 200px width with Background color as RED.
Background color has 4 values:-
- Color Name (red, green blue etc)
- Hexadecimal Number (#ff33ea, #123456,...etc)
- RGB color code - rgb(123,0,255)
- Transparent
Background-Image
If
you want to set an image for your HTML page or any other element created
on the page, you can use this attribute to set the image.
The css code is quite similar to the background color attribute but the attribute and values are different.
Example for setting Background Image:-
/*Syntax of Background Image in CSS*/
height:200px;
width:200px;
background-image:url('myimage.jpg');
height:200px;
width:200px;
background-image:url('myimage.jpg');
Above example will create a square box of 200px height and 200px width with Background image.
Background Image has 2 values:-
- url - (Path where your image is stored)
- none
Background Color + Image
Sometime it happens that your image load time increased and it shows an empty image area. This is not good for a website or blog.
To handle this situation, we suggest you to use this property of background image with color.
Example, setting Background Image with color:-
/*Syntax of Background Image with color in CSS*/
height:200px;
width:200px;
background:#ff2354 url('myimage.jpg');
height:200px;
width:200px;
background:#ff2354 url('myimage.jpg');
Above example will create a square box of 200px height and 200px width with Background image.
But if your image is not loading then it will show you the color you set with the url of your image.
Background Attachment
You can control the attachment of background image in style sheet. This is quite easy and a good practice to handle an image.
This property has 2 values to use with the image. If you want to scroll the image with the page or if you want to fix the image to some point on the page.
- Fixed - It will fix the image and will not allow image to scroll with the page.
- Scroll - It will scroll the image with page scroll
Example for setting Background Attachment:-
/*Syntax of Background Attachment in CSS*/
background-attachment:value;
background-attachment:value;
Background Position
You can control the background position of your HTML element with the help of CSS (stylesheet). If you want to position an element, then you can do this by using background-position property of style sheet.
It has following 11 values to use for setting up the background position of an element.
It has following 11 values to use for setting up the background position of an element.
- top left
- top center
- top right
- center left
- center center
- center right
- bottom left
- bottom center
- bottom right
- x-% y-%
- x-pos y-pos
Example for setting Background Position:-
/*Syntax of Background Position in CSS*/
background-position:value;
background-position:value;
Background Repeat
You can set an to repeat itself or not within your HTML page. It has 4 values to set.
- Repeat
- No Repeat
- Repeat X
- Repeat Y
Example for setting Background Repeat:-
/*Syntax of Background Repeat in CSS*/
background-repeat:value;
background-repeat:value;
NOTE:-
To use properties of background and its relevant values,
read posts on each topic in the next section of this blog.
0 comments:
Post a Comment