|
Search This Site
|
Styling Backgrounds With CSS
While backgrounds can be applied using HTML, CSS is the prefered choice as it is more efficient. In this lesson you will learn all about applying backgrounds
to your webpages using CSS. Background-Color PropertyThe background-color property is one of the most frequently used to style pages. Colored backgrounds are employed by using this property. Colors are specified in one of three ways; It can be specified by color name ( example white ), by a unique hexadecimal number ( example #FFFFFF) or by an rgb (red, green, blue) value [ example rgb( 255,255,255 ) ]. You should be familiar with applying all these values in a style sheet as they were disucssed earlier as a part of the color property.
background-color: #D8D8D8; <----- This hexadecimal values gives a shade of grey
style.css
body
{
background-color: #D8D8D8;
}
Background-Image PropertyThis property in CSS is used to apply a background image to your page. If that image is big enough it may cover the entire page, however if the image is small it may repeat until the entire background is covered.
background-image: url('background.jpg'); <----- url provides name and location of image
Providing a link to the same image assuming it is in a sub-folder titled images would look like this: style.css
body
{
background-image: url('images/background.jpg');
}
Background-Repeat PropertyThe background-repeat property is used closely with the background-image property. It specifies whether an image should be repeated only vertically, only horizantally or none at all. The values of this property are repeat-y and repeat-x . They can be considered to be similar to a graph where the y-axis represents vertical and the x-axis represents horizantal. If no background-repeat property is set, the image by default repeats both vertically and horizontally. To repeat the image vertically:
body
{
background-image: url('images/flower.jpg');
background-repeat: repeat-y;
}
body
{
background-image: url('images/flower.jpg');
background-repeat: repeat-x;
}
body
{
background-image: url('images/flower.jpg');
background-repeat: no-repeat;
}
Background-Position PropertyThe background-position property allows you to position the background. You can specify the positioning for your background a number of ways. Two of the most frequently used ways are by percentage value ( example 50 % 50% ) and stating the position ( example top right ). By default, any image you apply is positioned in the top left corner.
The background-position property is employed like this:
background-position: top right; <----- Image displayed in top right corner
background-position: 80% 40%; <----- First percentage value represents horizantal
position. Second percentage value represents
vertical position
style.css
body
{
background-image: url('images/flower.jpg');
background-repeat: no-repeat;
background-position: top right;
}
Background-Attachment PropertyHave you ever visited certain websites and wondered how a background moved with you as you scrolled down ?
It is accomplished by using the background-attachment property.
The background-attachment property is used to determine whether a background should scroll or remain in a fixed position.
The value of the background-attachment property can be fixed or static. It is represented like this:
background-attachment: fixed; <----- The background image remains in a fixed position
style.css
body
{
background-image: url('background.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
}
That's about it for what you need to know on applying backgrounds. Try experimenting with the properties on your own and have fun. When you are
ready, move on to the next lesson.
Previous Lesson - Fonts
Next Lesson - Links
|
|
© Copyright 2010 | All Rights Reserved
|