In this CSS Tutorial, you will learn how to apply a background image to the <body> element. You will also learn how to apply the background-repeat and background-position properties. The goal is to create a gradient image that repeats down the right edge of the page.
Setting up the HTML Code
The HTML code for this tutorial will be comprised of three paragraphs of text.
<p>Lorem I psum dolor sit css tutorial…</p>
<p>More CSS stuff goes here..</p>
<p>And some more great stuff about backgrounds.</p>
Creating Selectors to Style the Header
To style the <body> element and its content, you will only need a single type selector as shown below.
body {…}
Adding background-image
The background-image property in used to add a background image to the <body> element.
Values for the background-image property are either a url (to specify the image) or none (when no image is used).
For this lesson, you can use the url (http://csshowto.us/wp-content/uploads/2010/03/bg-tutorial.png). The image path can be written with or without quotation marks. The background-image code is shown below.
Setting background-repeat
The background image in this css tutorial is now repeating across the screen. This can be controlled using background-repeat.
Values for the background-repeat property include repeat, repeat-x and repeat-y. In this css tutorial, you will use repeat-y to force the image to repeat verticle down the page.
{
background-image: url(http://csshowto.us/wp-content/uploads/2010/03/bg-tutorial.png);
background-repeat: repeat-y;
}
Adding background-position
Now that the background image is repeating correctly, it must be positioned down the right edge of the <body> element. This is achieved using background-position.
Values for the background-position property include percentage (such as 0 100%), length (such as 2px 20px), and keywords (such as left top). In each case, the horizontal position is specified first, and then the vertical position. The values 0% 0% will position the image in the upper-left corner. Values of 0% 100% will position the image in the bottom-left corner. Values of 2px 20px will position the image in 2px from the left and 20px from the top.
If only one percentage or value is given, it sets the horizontal position only and the vertical position will be 50%. If two values are given, the horizontal position comes first. Combination of percent and and pixels are allowed as well as negative positions.
For this css tutorial, you will use percentage values of 100% 0, which will place the image in the right top corner of the element.
{
background-image: url(http://csshowto.us/wp-content/uploads/2010/03/bg-tutorial.png);
background-repeat: repeat-y;
background-position: 100% 0;
}
Using the Background Shorthand
Shorthand properties allow the values of several properties to be specified within a single property. The background property can be used to combine background-color, background-image, background-repeat, background-attachment, and background-position.
When sorting shorthand properties, browsers will first set all the individual properties to their initial values, and then override these with values specified by the programmer.
A default background rule would be set to background: transparent none repeat scroll 0 0;. If the declarations used in this css tutorial are combined into the shorthand rule, they will override the default values for background-image, background-repeat, and background-position. The result will be background: transparent url() repeat-y scroll 100% 0;.
However the rule can be shortened to only include the values that are needed, so the final declaration will be:
{
background: url((http://csshowto.us/wp-content/uploads/2010/03/bg-tutorial.png) repeat-y 100% 0;
}
Summary
In this css tutorial, you learned how to apply a background image to the <body> element. You also learned how to apply the background-repeat, background-position, and shorthand background properties.
Helpful Articles
Steve Present Weight Loss: The 2 Secrets Of Weight Loss Success
2 Unusual Facts About Rapid And Lasting Weight Loss Success: In order to achieve lasting results, you MUST change your inner weight controller: Your subconscious mind...
Diabetic Renal Diet for CKD Patients Diet
Diabetic Renal Diet Guidelines Made Easy! Being that one of the most common extrarenal diseases affecting the kidney is diabetes mellitus, diabetic renal diet has become a topic of interest nowadays...
The Lemonade Diet
There are many people who have the primary goal of weight loss and they turn to the lemonade diet to lose weight quickly...
Tags: background-attachment, background-color, background-image, background-image property, background-position, background-repeat, CSS, div, How To, selectors