Open In App

HTML Background Images

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HTML background images are a powerful tool for enhancing the visual appeal and customization of web pages. They can be used in various ways to create visually engaging designs and improve user experience. This guide will provide an in-depth look at how to effectively use background images in HTML.

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:

imresizer-1701411009904

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: 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="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: 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.

imresizer-1701410711366

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:

Screenshot-2023-12-01-115731

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:

Screenshot-2023-12-01-124422

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:

Screenshot-2023-12-01-125712



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads