The Wayback Machine - https://web.archive.org/web/20240725234137/https://www.geeksforgeeks.org/how-to-set-background-image-in-html/
Open In App

How to add background image in HTML ?

Last Updated : 22 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Adding a background image to your HTML page can enhance the visual appeal and provide a more immersive experience for your website visitors. This process involves embedding an image behind your webpage content. You can do this by using the background attribute in HTML tags or the style tag, either inline or in an external stylesheet. This allows you to specify the image URL and control its placement and appearance.

Examples of adding background images in HTML

1. Using a style Attribute

To incorporate a background image in HTML using a style attribute, you can apply CSS directly to the HTML element. For instance, you can set the background image of the body using the style attribute, specify the image URL, and prevent repetition. This method provides a straightforward way to customize the look and feel of your webpage.

Syntax:

<div style = "background-image: url('Image_name.extension');">

Example: In this example we sets the background image of the body using the style attribute, specifying the image URL and preventing repetition.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>By using style attribute</title>
    </head>

    <body
        style="background-image: url(
'https://media.geeksforgeeks.org/wp-content/uploads/20240412161651/using-style-attribute.jpg'); 
               background-repeat: no-repeat;"
    >
    </body>
</html>

Output:


using-style-attribute

Using a style Attribute Example Output


2. Using The Background Attribute

Uisng background attribute to body or target tag we can easily set the background image in HTML.

Note: The background attribute in HTML is deprecated. Instead, use CSS properties like background-image applied inline or external css to set background images for elements.

Syntax:

<body background="image_name.image_extension"> 

Example: In this example we sets the background image of the body using the deprecated background attribute and specifies no repetition using inline CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Background Image</title>
</head>

<body background=
"https://media.geeksforgeeks.org/wp-content/uploads/20240412161651/using-style-attribute.jpg" 
      style="background-repeat: no-repeat;">
</body>

</html>

Output:


using-Background-Attribute

Using The Background Attribute Example Output

3. Using the background-image property

Using the background-image property to set an element’s background. Specify the URL of the image file

Synatx:

background-image: url('url')

Example: In this example we sets the body background image using CSS background-image property. The image URL is provided within the style block, with background-repeat set to no-repeat.

HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0"
        />
        <title>Background Image</title>
        <style>
            body {
                background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/20240415102919/
                  UsingBackgroundImageProperty.webp");
                background-repeat: no-repeat;
              
            }
        </style>
    </head>

    <body>
        <!-- Your content here -->
    </body>
</html>

Output:

Using-Background-ImageProperty

Using the “background-image” property Example Output

Understanding how to add a background image in HTML is a fundamental skill for web developers. Whether you’re using the style attribute, the background attribute, or the background-image property, each method offers unique advantages. Remember, while the background attribute is deprecated, you can achieve the same results with CSS properties like background-image.



Previous Article
Next Article

Similar Reads

How to add an image as background image of a web page ?
In this article, we will be adding an image as the background image of a web page. Background images are used to make a website more interactive and attractive. It can be applied in many stylings. Approach: In the body tag, specify a background image in the background attribute by passing the URL of the image or location path.Adding CSS styling pro
2 min read
How to Add Image in Text Background using HTML and CSS ?
In this article, we will use HTML and CSS to set the image in the text background. To set the image in the text background, some CSS property is used. To add an image in the text background using HTML and CSS, create a container element (e.g., a `&lt;div&gt;`), set its background to the desired image using CSS ('background-image property), and adju
2 min read
How to add background-image using ngStyle in Angular2 ?
The ngStyle Attribute is used to add some style to an HTML element. In this article, we will learn How to add background-image using ngStyle in Angular. To achieve this, we will follow the below approaches. Table of Content Steps for Installing &amp; Configuring the Angular ApplicationProject StructureUsing [ngStyle] DirectiveUsing the [style.backg
3 min read
How to add a Background Image using Tailwind CSS ?
To add a background image using Tailwind CSS, you can use the bg-[image-class] utility class, where [image-class] corresponds to a predefined set of background image classes. You also need to set the actual image URL using inline or external styles. Syntax&lt;div class="bg-cover bg-center h-screen" style="background-image: url('your-image-url.jpg')
1 min read
How to Add a Background Image using Bootstrap 5 ?
Bootstrap 5 is one of the alternatives for CSS styling of the elements. Rather than defining the separate properties for the elements, we can directly apply the Bootstrap 5 classes for more attractive styling of the elements. We can add a background image to the application in Bootstrap 5 using various approaches as listed below: Table of Content U
2 min read
How to Add Background Image in CSS ?
Adding a background image to a webpage can enhance its aesthetic appeal and make it more engaging for users. CSS (Cascading Style Sheets) provides several properties to easily set and customize background images. In this article, we will explore different approaches to add background images in CSS. Table of Content Using the background-image Proper
2 min read
How to Add a Background Image in Next.js?
Next.js is a powerful React framework that allows for server-side rendering and the generation of static websites. Adding a background image to your Next.js application can enhance the visual appeal of your web pages. This article will guide you through the process of adding a background image to a Next.js project. Prerequisites:Next.jsReactJSCSSNo
3 min read
How to Add filter to the background image using CSS?
Adding filters to background images using CSS allows you to apply visual effects like blur, grayscale, brightness adjustment, and more without modifying the actual image file. CSS provides a set of filter function that can be applied to elements with background images. This approach enhances design flexibility, enabling dynamic adjustments to backg
2 min read
How to Modify the Fill Color of an SVG Image when being Served as Background Image ?
In this article, we will see how to modify the fill color of an SVG image when being served as a background image. SVGs also known as scalable vector images, While being served as a background image we can change their background color easily. It has a number of approaches like using HTML only or using CSS or JavaScript. For starters let's look at
5 min read
Difference between background and background-color
Background Property The background property in CSS is very commonly used and contains many variants. The background property consists of all the following properties:background-colorbackground-imagebackground-positionbackground-sizebackground-repeatbackground-originbackground-clipbackground-attachmentExample: C/C++ Code &amp;lt;style&amp;gt; h1 { b
1 min read
three90RightbarBannerImg