HTML stands for Hyper Text Markup Language. It is used to design or create web pages using a markup language. It is a combination of Hypertext and Markup language. In HTML, background images allow developers to set images as backgrounds for HTML elements, enhancing visual appeal and customization within web pages. So, we can add background images in two different ways:
- Add a background image using the background image attribute inside the <body> tag.
- Add background image using the HTML style attribute.
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">
Example: In this example, we will see the use of background images on a webpage.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<h1 style="color: green; text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
|
Output:

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');">Example: In this example, we will see the use of background images on a webpage.
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=
<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: In the above 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
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: In this example we will see the implementation of above property with an example.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
style="background-repeat: no-repeat;">
<h1 style="color: green; text-align: center;">
Welcome to GeeksforGeeks
</h1>
</body>
</html>
|
Output:

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: In this example we will see the implementation of above property with an example.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
<body background=
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:

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: In this example we will see the implementation of above property with an example.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
</head>
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:

Output
Last Updated :
19 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...