CSS backgrounds

The background property of CSS allows us to set background effects for html elements.

 

The following are various CSS background properties:

background-color

background-image

background-repeat

background-attachment

background-position

background

 

background-color

used to set color for the background of any html element

 

<!DOCTYPE html>

<html>

<head>

<style>

body {

  background-color: lightblue;

}

</style>

</head>

<body>

 

<h1>Hello World!</h1>

 

<p>This page has a light blue background color!</p>

 

</body>

</html>

 

background-image

used to set an image as background of a html element

 

<!DOCTYPE html>

<html>

<head>

<style>

body {

  background-image: url(“paper.gif”);

}

</style>

</head>

<body>

 

<h1>Hello World!</h1>

 

<p>This page has an image as the background!</p>

 

</body>

</html>

 

 

background-repeat

used to repeatedly apply a background image across the web page when it is smaller in size.  The values for this property are: no-repeat, repeat-x, repeat-y

 

<!DOCTYPE html>

<html>

<head>

<style>

body {

  background-image: url(“img_tree.png”);

  background-repeat: no-repeat;

}

</style>

</head>

<body>

 

<h1>Hello World!</h1>

<p>W3Schools background image example.

<br/>

W3Schools background image example.

<br/>

 

W3Schools background image example.

<br/>

W3Schools background image example.

<br/>

W3Schools background image example.

<br/>

W3Schools background image example.

<br/>

 

</p>

<p>The background image only shows once, but it is disturbing the reader!</p>

 

</body>

</html>

 

background-position

used to specify the position of the background image in the web page

 

background-attachment

used to specify whether to display the background image fixed or scrolled along with the text while user scrolling.

 

<!DOCTYPE html>

<html>

<head>

<style>

body {

  background-image: url(“img_tree.png”);

  background-repeat: no-repeat;

  background-position: right top;

  margin-right: 200px;

  background-attachment: fixed;

}

</style>

</head>

<body>

 

<h1>The background-attachment Property</h1>

 

<p>The background-attachment property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page).</p>

 

<p><strong>Tip:</strong> If you do not see any scrollbars, try to resize the browser window.</p>

 

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

<p>The background-image is fixed. Try to scroll down the page.</p>

 

</body>

</html>

You cannot copy content of this page