×

CSS Background

CSS Background

The properties of CSS background are applied to describe the effects of background on the element. Here are five properties of background that affects various elements of HTML:

  • Background-color
  • Background-image
  • Background-repeat
  • Background-attachment
  • Background-position

Background-color

The color of the background element is specified by using this property. An example for setting background-color is given below:

Example:

<!DOCTYPE html> 
  <html> 
  <head>  
 <style>   body {   background-color: orange;   }   </style>  
 </head> 
  <body>  
 <h1> Hello! </h1>  
 <p> This page has an orange background color!   </p>  
 </body>  
 </html>    
CSS Background

Background-image

When we want to insert an image as the background of HTML element, we can use background-image property. The image will be repeated and will cover all the elements.  An example of adding image in background is as follows:

Example:

 <!DOCTYPE html>  
 <html>  
 <head>  
 <style>   body {   background-image: url("paper.gif");   }   </style>  
 </head> 
  <body> 
  <h1> Hello! </h1>  
 <p> This page has an image as the background!   </p>  
 </body>  
 </html>    
CSS Background

Background-repeat

As discussed in the background-image property, background-image repeats the image vertically and horizontally. A few images in the background are repeated both vertically and horizontally. But, the background appears better when the image has horizontal repetition only.

Example: repeat-x;

<!DOCTYPE html>  
 <html>  
 <head>  
 <style>   body {   background-image: url("gradient_bg1.png");   background-repeat: repeat-x;   }   </style>  
 </head>  
 <body>  
 <h1> Hello Users! </h1>  
 <p> This image will be repeated only   horizontally! </p>   
</body> 
</html>    
CSS Background

Example: repeat-y;

 <!DOCTYPE html> 
  <html>  
 <head>   
<style>   body {   background-image: url("gradient_bg1.png");   background-repeat: repeat-y;   }   </style>   
</head>  
 <body>  
 <h1> Hello Users! </h1>  
 <p> This image will be repeated only vertically!   </p>  
 </body>  
 </html>    
CSS Background

Background-attachment

It is applied to define the background image, which is scroll or fixed with the page rest in the browser window. When we fixed the image in the background, the image can’t move at the time of scrolling inside the browser. The following example will clear this concept.

Example: fixed

 <!DOCTYPE html>   
<html>  
 <head> 
  <style>   body {   background-image: url("tree.png");   background-repeat: no-repeat;   background-attachment: fixed;   margin-left: 550px;   }   </style>  
 </head>  
 <body>  
 <h1> The background-attachment property   </h1>  
<p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 </body>  
 </html>    
CSS Background

Example: scroll

<!DOCTYPE html>  
 <html>  
 <head>  
 <style>   body {   background-image: url("tree.png");   background-repeat: no-repeat;   background-attachment: scroll;   margin-left: 550px;   }   </style>  
 </head>  
 <body>   <h1> The background-attachment property   </h1>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>   
<p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p>  
 <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  <p> This property will specify that the image in   the background should scroll or be fixed. </p> 
  </body> 
  </html>     
CSS Background

Background-position

This property of CSS is applied to describe the basic position of an image in the background. A background picture is positioned on the top-left side of the webpage. We can arrange some positions by using the above property as follows:

  1. top
  2. bottom
  3. center
  4. left
  5. right

Example: center

    <!DOCTYPE html>  
 <html>  
 <head>  
 <style>   body {   background-image: url("flower.gif");   background-repeat: no-repeat;   background-attachment: fixed;   background-position: top;   }   </style>  
 </head> 
  <body> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
  <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p>  
 <p> This is an example for setting the position   of background image. This image is fixed. Please scroll the page. </p> 
</body>  
</html>     
CSS Background

Related Topics

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Table

CSS table provides better look and feel. There are various properties of CSS table that are given below. border border-collapse padding width height text-align color background-color CSS Table Border You can set...

4 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

Css Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

5 minutes read.

Css Text Orientation

Introduction: The text-orientation property of CSS is used to specify the orientation of the text characters in the line of content. This property applies only with the vertical writing-mode and not...

4 minutes read.