How to add background image in HTML?

How to add background image in HTML?

4 mins read1.5K Views 2 Comments
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Dec 4, 2024 11:23 IST

Images can make a page look more appealing and captivating. One such way to make the page more attractive is by adding a background image to it. You can add a background image in HTML using the Background HTML attribute (HTML Tag) or the Internal Style Sheet (CSS). In case you don't know how HTML and CSS differ, first read the Difference Between HTML and CSS article.

2022_12_MicrosoftTeams-image-171.jpg

In this article, we will explore how to add background image in HTML. But, before we begin exploring different methods to add background image in HTML, let’s go through the key highlights to get a quick understanding:

Recommended online courses

Best-suited HTML & CSS courses for you

Learn HTML & CSS with these high-rated online courses

– / –
25 days
4.24 K
6 weeks
– / –
– / –
2.95 K
190 hours
7.5 K
4 weeks
– / –
1 month
3.2 K
28 hours
449
11 hours
Free
2 hours
– / –
17 hours

Key Highlights: Methods & Steps to Add Background Image in HTML

Method Steps
Background Attribute 1. Open HTML file in a text editor.
2. Add "body background=image-path" with the image URL or path.
3. Save and run the HTML file.
Internal Style Sheet (CSS) 1. Open HTML file in a text editor.
2. Add "style" tag in the "head" section.
3. Use background-image: url('image-path').
4. Save and run the HTML file.

You can also explore: What is HTML?

Table of content (TOC)

How to Add Background Image in HTML

You can add background image in HTML using two methods:

  1. Background attribute (Html Tag)
  2. Internal Style Sheet (CSS)

Let’s explore each of these methods in detail in the next section.

You can also explore: HTML Online Courses & Certifications

Background Attribute

If you want to add background image in HTML using background attrubute, perform the following steps:

  1. Open the HTML file in text editor.
  2. Within the starting <body> tag in your Html file, type <Body background=” “>
  3. Give the path of the image we want to add. (Example, <Body background=”C:Usersanshuman.singhDownloadsinfoedge.jpg “>
  4. Save the Html file in the text editor and run the file.

Let’s go through an example to understand the steps in a better way. Here’s the HTML file:

2022_12_HTML-code.jpg

When you will execute this code, the output will be something like this:

2022_12_output.jpg

Note:C:Usersanshuman.singhDownloadsinfoedge.jpg“, is the image path where the image was saved in my device. Replace the path as per the location of the file stored on your device. And, if you want to add image from internet, use the image URL to do so.

Internal Style Sheet (CSS)

If you want to add background image using internal style sheet (CSS), perform the following steps:

  • Open the HTML file in text editor.
  • Following the closing of the title tag in the HTML document, define the starting and closing tags of the <style> tag.
  • Type the element body in the style tag and, then type the backgroud image property.
  • Save the Html file in the text editor and run the file.

Let’s go through an example to understand the steps in a better way. Here’s the HTML file, as per the above steps:

2022_12_2nd34.jpg

When you will execute this code, the output will be something like this:

2022_12_6879.jpg

Note: In the above HTML code, we have used an image from internet, by using the image URL.

You can also explore these articles:

HTML Lists: Ordered and Unordered Lists Explained with Examples
HTML Lists: Ordered and Unordered Lists Explained with Examples
This tutorial explains how to create different types of lists: unordered, ordered, and description lists in HTML. In this blog, you will learn what are HTML lists and understand how...read more

HTML Formatting: HTML Tags for Text Formatting (With Examples)
HTML Formatting: HTML Tags for Text Formatting (With Examples)
Have you ever wondered how is text formatting done in HTML using HTML formatting tags? Let us understand it in detail with the help of various examples.Have you ever wondered...read more

Creating HTML Tables (Tutorial With Examples)
Creating HTML Tables (Tutorial With Examples)
This guide offers an introduction to HTML tables. You will learn how to create tables in HTML to present tabular data. Tables are a great way to present data visually....read more

HTML Heading and Paragraph Tags Explained With Examples
HTML Heading and Paragraph Tags Explained With Examples
This tutorial explains how to create headings and paragraphs in HTML documents using heading and paragraph tags.

How to Repeat Background Image in HTML?

If the background image is relatively small than the page or the element, it will repeat vertically and horizontally until it reaches the bottom of the page.

Here’s an example:

2022_12_repeat.jpg

Note: If you don’t want to background image to repet itself, set the background repeat property to no-repeat.

How to Cover the Entire Element with the Background Image in HTML?

In order to cover the entire element with the background image, you can set the background-size property to cover. And, if you don’t want that your image should stretch, you can set the background-attachment property to fixed. This will cover the entire element or page, with no stretching.

Let’s go through an example to understand it in a better way:

2022_12_entire-element-2.jpg

The output will be something like this:

2022_12_ENTIRE-ELEMNT.jpg
Must Explore:

All About CSS Syntax
All About CSS Syntax
Have you ever wondered how websites get their unique styles? It’s all because of CSS syntax, the rules web designers follow to style web pages. Let’s understand more! CSS syntax...read more

How to Link CSS to HTML
How to Link CSS to HTML
Have you ever wondered how to make your website visually appealing and well-structured? Linking CSS to HTML is the key to achieving this. Let’s understand more! Learning how to Link...read more

How to Create HTML Forms – Tutorial with Examples
How to Create HTML Forms – Tutorial with Examples
HTML forms or web forms are a powerful medium for interacting with users. They have interactive controls for submitting information. Web forms enable us to collect data or personal information...read more

HTML Tags: Essential and Basic Tags for Creating an HTML Document
HTML Tags: Essential and Basic Tags for Creating an HTML Document
HTML tags form the basic structure of any webpage or HTML document. In this blog, we will discuss some essential and basic HTML tags with examples. HTML tags form the...read more

FAQs

How do I add a background picture in HTML?

You can add backgrond picture in HTML by using either Internal Style Sheet (CSS) or Background attribute (Html Tag).

Can I use multiple background images in HTML?

Yes. You can use multiple background images in HTML by separating image URLs with commas in the CSS background-image property. Each image can have its own positioning and repeat settings.

How can I adjust the opacity of a background image in HTML?

To adjust background image opacity, create a pseudo-element with the image and set its opacity. Then, place your content in a separate element on top of the background.

How do you fit an image background in HTML?

To fit an image background in HTML, set the background-size property to "100% 100%".  This will stretch the background image to cover the entire element, such as the body element.

How can I place a background image behind the text in HTML?

To place a background image behind text in HTML, you can follow these steps:

  1. Create a container element, such as a `<div>.`
  2. Set its background image using the CSS `background-image` property.
  3. Adjust the text properties, such as colour and size, to ensure the text remains readable against the background.
About the Author
author-image
Anshuman Singh
Senior Executive - Content

Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr... Read Full Bio

Comments

(2)