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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read