Tuesday, 22 August 2017

How to Create Background CSS with Example

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:


  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
1) CSS background-color

You can set the background color like this:

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

2) CSS background-image
The background-image property is used to set an image as a background of an element. By default the image covers the entire element. You can set the background image for a page like this.

<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper1.gif");
margin-left:100px;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>

3) CSS background-repeat

background-repeat: repeat-x;

<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>

background-repeat: repeat-y;

<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>

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.

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

5) CSS background-position
The background-position property is used to define the initial position of the background image. By default, the background image is placed on the top-left of the webpage.

You can set the following positions:
  1. center
  2. top
  3. bottom
  4. left
  5. right

background: white url('good-morning.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search