HTML background images are graphics applied to the background of HTML elements, often used in webpage design for aesthetic or branding purposes. They're set using CSS background-image property, allowing for single or repeating images, gradients, or patterns to enhance visual appeal.
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.
How to Add Background Image in HTML
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.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210203170945/HTML-Tutorials.png">
<h1 style="color: green;
text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
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.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body>
<h3 style="color: green; text-align: center;">
Welcome to GeeksforGeeks
</h3>
<div style="background-image: url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210203170945/HTML-Tutorials.png');">
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
<p>A Computer Science portal for geeks.</p>
</div>
</body>
</html>
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.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210203170945/HTML-Tutorials.png"
style="background-repeat: no-repeat;">
<h1 style="color: green;
text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
Output:
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.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210203170945/HTML-Tutorials.png"
style="background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;">
<h1 style="color: green;
text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
Output:
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.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210203170945/HTML-Tutorials.png"
style="background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;">
<h1 style="color: green;
text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
Output:
HTML Background Images Example Output
Similar Reads
How to Add Background Image in HTML?
Adding a background image to an HTML page can enhance the visual appeal and provide a more immersive experience for your website visitors. The <body> background attribute is used to add a background image in HTML, but this attribute is deprecated in HTML5 and is not in use. In place of the bac
3 min read
How to Set Background Image in HTML Table ?
Setting a background image in an HTML table can enhance the visual appeal of your webpage. In this article, we will discuss two methods to set a background image in an HTML table i.e. using inline CSS and external CSS. Set Background Image in HTML Table using Inline CSSIn this approach, we directly
2 min read
HTML Canvas Images
In this article, we will see HTML Canvas Images. We generally use images on our web page through <img> element. In this article, we will see HTML Canvas images in HTML and JavaScript. The drawImage() Method is used to embed images or video on Canvas. With this method, we can also change the h
3 min read
How to Set Background Image in CSS?
CSS (Cascading Style Sheets) can allow developers to set the background image for the web pages or specific HTML elements. This feature can help enhance the visual aesthetics of the website. The background-image property in CSS can be used to specific one or multiple background images to be applied
3 min read
HTML Images
The HTML <img> tag is used to embed an image in web pages by linking them. It creates a placeholder for the image, defined by attributes like src, width, height, and alt, and does not require a closing tag.There are two ways to insert the images into a webpage:By providing a full path or addre
6 min read
CSS background-image Property
The background-image property allows you to set one or more background images for an element. It allows you to specify the image URL and combine it with other background properties to control its position, size, repeat behavior, and more, enhancing the visual design of web pages.Syntaxelement_select
4 min read
How to Add Background Image in CSS?
The CSS background-image property is used to add an image as a background to an element.Syntaxbackground-image: url()1. Setting background Image of an ElementA background image is added to an h1 tag using the URL.HTML<h1 style="background-image: url( 'https://media.geeksforgeeks.org/wp-content/up
1 min read
How to Fix broken Images in HTML?
Broken images in HTML occur when the browser cannot load the referenced image file, resulting in an empty placeholder with an icon (usually a small image symbol or an "x"). This issue negatively affects user experience and can leave the webpage looking incomplete or unprofessional. These are the fol
3 min read
HTML Image Maps
HTML Image Map is used to create interactive and clickable areas within an image on a web page. It enhance the user experience by providing a visually appealing way to navigate or interact with content directly through images.What is an HTML Image Map?An HTML Image Map is a type of image that contai
3 min read
HTML | DOM Style backgroundImage Property
The DOM Style backgroundImage Property is used to set or return the background image of an element. Syntax: To get the backgroundImage propertyobject.style.backgroundImageTo set the backgroundImage propertyobject.style.backgroundImage = "image | none | initial | inherit" Return Values: It returns a
3 min read