code

CSS Background

CSS background property is used to define the background effects on element. There are 5 CSS background properties that affects the HTML elements:

  1. background-color
  2. background-image
  3. background-repeat
  4. background-attachment
  5. background-position
body {
  background-color: lightblue;
}

1) CSS background-color

The background-color property is used to specify the background color of the element

<!DOCTYPE html>  
<html>  
<head>  
<style>  
h2,p{  
    background-color: #b0d4de;  
}  
</style>  
</head>  
<body>  
<h2>My first CSS page.</h2>  
<p>Hello . This is an example of CSS background-color.</p>  
</body>  
</html>   

2) CSS background-image

The background-image property specifies an image to use as the background of an element.

By default, the image is repeated so it covers the entire element.

The background image for a page can be set like this:

body {
  background-image: url("paper.gif");
}

Background Image – Repeat Horizontally or Vertically

By default, the background-image property repeats an image both horizontally and vertically.

Some images should be repeated only horizontally or vertically, or they will look strange, like this:

body {
  background-image: url("gradient_bg.png");
}

If the image above is repeated only horizontally (background-repeat: repeat-x;), the background will look better:

body {
  background-image: url("gradient_bg.png");
  background-repeat: repeat-x;
}

Tip: To repeat an image vertically, set background-repeat: repeat-y;

3)Background Image – Set position and no-repeat

Showing the background image only once is also specified by the background-repeat property:

body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
}

In the example above, the background image is shown in the same place as the text.

We want to change the position of the image, so that it does not disturb the text too much.

The position of the image is specified by the background-position property:

body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}

4) CSS background-attachment

The background-attachment property is used to specify if the background image is fixed or scroll with the rest of the page in browser window. If you set fixed the background image then the image will not move during scrolling in the browser. Let?s take an example with fixed background image.

body{
   background: white url('bbb.gif');  
   background-repeat: no-repeat;  
   background-attachment: fixed;
} 

5)Background Image – Fixed position

To specify that the background image should be fixed (will not scroll with the rest of the page), use the background-attachment property:

body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
  background-attachment: fixed;
}

Background – Shorthand property

To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property.

The shorthand property for background is background:

body {
  background: #ffffff url("img_tree.png") no-repeat right top;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)

RSS20
Follow by Email
Facebook20
Pinterest9