VOOZH about

URL: https://www.geeksforgeeks.org/html/html-background-images/

⇱ HTML Background Images - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML Background Images

Last Updated : 23 Jul, 2025

HTML background images are pictures or graphics placed behind elements on a webpage. They are commonly used in web design to improve the look and feel of a site or to support branding. These images are added using theCSS background-image property, which lets you use single images, repeating patterns, or even gradients. About 80% of modern websites use background images to enhance visual design and user experience.

👁 bgimg_compressed
HTML Background Images


Understanding HTML Background Images

HTML Background Images allows the developers to set images as backgrounds for HTML elements, enhancing visual appeal and customization within web pages.

The Background images can be included in two ways:

Using Background-Image Attribute Inside Body Tag

In this method, we will add a background image using the background image attribute inside the <body> tag.

Syntax:

<body background="image.png"></body>

Example: Adding the Background on a webpage.

Output:

👁 HTML Background Images Example Output

Using the HTML style attribute

In this method, we will add a background image using the HTML style attribute for a particular element in the webpage.

Syntax:

<div style="background-image: url('img.jpg');"></div>

Example: In this example we can see that there is no background image for the heading "Welcome to GeeksforGeeks", but we have a background image for the div containing paragraphs.

Output:

👁 HTML Background Images Example Output

Background Repeat

In HTML, if the image is small in size as compared to the element then the image repeats itself horizontally and vertically. It repeats until it reaches the end of the element. If you don't want to repeat the image then set the value of background-repeat property to no-repeat. Let us discuss this concept with the help of an example:

Syntax:

background-repeat: no-repeat;

Example: Implementation of Background Repeat property.

Output:


👁 HTML-Background-Images-(1)
HTML Background Images Example Output


Background Cover

Background cover is a design that ensures an image spans the entire background of a webpage or element, adapting to different screen sizes for a visually consistent look. Also, to make sure the entire element is always covered, set the background-attachment property to fixed:

Syntax:

background-size: cover;
background-attachment:fixed;

Example: Implementation of background-size property.

Output:


👁 HTML-Background-2-(1)
HTML Background Images Example Output


Background Stretch

Background stretch is a concept where an image is expanded or stretched to fill the entire background, maintaining proportions to fit different screen dimensions. It can be done using :

Syntax:

background-size : 100% 100%;

Example: Implementation of background-size property.

Output:


👁 HTML-Background-3-(1)
HTML Background Images Example Output


Comment