×

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

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

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.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

Is CSS a programming language?

 A programming language is a set of grammatical rules and a vocabulary for commanding a machine to accomplish specified tasks. High-level languages like BASIC, C, C++, COBOL, Java, FORTRAN, Ada...

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 Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 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 Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 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.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS Align

CSS Center Align Elements CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space....

2 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.