×

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

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

Cascading Style Sheet

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

7 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

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

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 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 Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes 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 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 Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.

CSS Letter-spacing

CSS Letter-spacing: Letter-spacing attribute is applied to manage the space among all letters within the block or element of the text. This property sets the behavior of the spacing between...

2 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.