How to Add an Image & Background Image in HTML (2024)

Did you know that people remember only 20% of what they read but 80% of what they see? People learn and process information better visually — which is why using images on your website is critical. That's where learning how to use HTML background img code comes into the picture. (Pun intended.)

How to Add an Image & Background Image in HTML (1)

Today, I'm going to walk you through everything you need to know about adding an image (or a background image) in HTML. You'll learn how to use HTML background img code, as well as why imagery is a must-have.

Why use imagery on your site?

65% of the population are visual learners. If you're not making use of images on your site, you're not offering content that speaks to a majority of folks.

Pictures help make your content more informative, engaging, and memorable. There's also the digestibility aspect: If you add imagery such as an infographic, dense content becomes easier to understand. In addition to improving the user experience, they can also help boost your organic search traffic.

How to Add an Image & Background Image in HTML (3)

HubSpot's Free Website Builder

Create and customize your own business website with an easy drag-and-drop website builder.

  • Build a website without any coding skills.
  • Pre-built themes and templates.
  • Built-in marketing tools and features.
  • And more!

Get Started for Free

How to Add Imagery with a Site Building Platform

If you use a website-building platform like Content Hub or WordPress, just click the image icon in your toolbar, select an image from your file manager, and insert it. This process is straightforward and shouldn't take more than a few minutes. Because a tool like Content Hub offers drag-and-drop building, you do not need to know HTML.

That being said, if you aren't using a builder, adding an image to your site is still possible. However, you'll have to make use of HTML to do so. I'll walk you through that process now.

How to Insert an Image in HTML

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you’ll add images to the body section of your HTML file.

The syntax looks like this: <img src=“URL” alt=“descriptive text”>

The HTML image element is an “empty element,” meaning it does not have a closing tag. Unlike elements like paragraphs that consist of an opening and a closing tag with content in between, an image specifies its content with attributes in the opening tag.

Here, take a look at these lines of code to see the difference between a paragraph and image.

<p>This is a paragraph.</p><img src=“https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg” alt=“an artist's rendition of a black hole in space”>

Did you notice the two attributes in the image element above, src and alt? I will walk you through both of these important attributes next. This video dives deeper into the steps we just explained, so if you prefer to learn that way, you're welcome to follow along in the video.

The img src Attribute

An image element must always have a src (source) attribute containing the image URL or file path. If this is missing, the browser won't know what to render.

The types of permitted image files depend on the browser. However, allbrowsers allow you to place standard formats like .jpeg, .png, and .gif, as well as .svg.

In the code example above, the source is a full hyperlink because the image is being fetched from another website. If you want to place an image stored on your server, you can use the image file path without the website name or protocol.

For example, if the image is located in a subfolder stored in the same place as your HTML file, your image element can look more like this.

<p>This is a paragraph.</p><img src=“/csz/news/800/2017/theoreticala.jpg” alt=“an artist's rendition of a black hole in space”>

In this case, “csz” would be a folder located in the same directory as your HTML file.

The img alt Attribute

While a browser can render an image without the alt attribute, including this attribute is a must, in my opinion. That’s because this attribute contains image alt text.

Image alt text is important for a few reasons. First, it will appear in place of an image if the image fails to load on a user’s screen. Second, it helps screen-reading tools describe images to readers with visual impairments who might have trouble understanding the image without it. Creating a site that's accessible to all — including folks with blindness and low vision — is the right thing to do. Plus, it's good for your organization.

Third, image alt text allows search engines to better crawl and rank your website. Google Images — not Google Search, Google Image Search — is a major search engine on its own, and another way people can find your website.

Providing images with descriptive alt text can help you rank for your target keywords and drive traffic to your site. In 2019, HubSpot did exactly that, leading to a 25% year-over-year growth in organic traffic that came from web and image searches.

The img style Attribute

You might also see a style attribute inside an <img> tag containing the width and height of the image. Specifying the width and height can help prevent the web page from flickering while the image loads. Here’s how the code might look with these additional attributes.

<img src="https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg" alt="an artist's rendition of a black hole in space" style="width:300px; height:300px;">

Output

How to Add an Image & Background Image in HTML (4)

It’s important to note that you can also specify the size of an image using internal or external CSS, over inline CSS. To learn the difference between these three types of CSS, see our guide on adding CSS to HTML.

The img width and height Attributes

The width and height of an image can also be specified in pixels with separate width and height attributes, like so this.

<img src=“https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg” alt=“an artist's rendition of a black hole in space” width=“400” height=“200”>

This will produce the same result as the image above, where the style attribute was used.

The main difference between these methods is that using separate width and style attributes will tell the browser how much space to save for the image, which may result in smoother loading (though this will depend on your page layout, ultimately).

How to Add an Image & Background Image in HTML (5)

HubSpot's Free Website Builder

Create and customize your own business website with an easy drag-and-drop website builder.

  • Build a website without any coding skills.
  • Pre-built themes and templates.
  • Built-in marketing tools and features.
  • And more!

Get Started for Free

How to Insert a Background Image in HTML

If you’d like to set an image as the background of a web page or an HTML element, rather than simply inserting the image onto the page, you’ll need to use the CSS background-image property. By using this property, you are able to specify what background image you want to appear on your website.

This CSS property replaced the background-image attribute in previous versions of HTML. It’s much more flexible and predictable than the HTML attribute — and still easy to use. Therefore, instead of the traditional HTML background img code, I'd encourage you to use CSS.

To set the value of background-image, you have to use the following syntax:

url(‘ ’);

Between the single quotation marks, you’ll put the image URL or file path.

How to Insert a Background Image on a Page

Say you want to set an image as the entire page's background. In this case, you would apply CSS to the body element. Using a CSS selector, you can define the background-image property in the head section of your HTML file or an external stylesheet.

For this demo, I'll show you how to do so using the same image as above. I'll also change the text color to white so we can see it.

Here’s the HTML and CSS.

<h2>Background Image</h2><p>The background image is specified in the body element.</p>body { background-image: url(‘https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg’); color: #FFFFFF;}

Here’s the result.

Output

How to Add an Image & Background Image in HTML (6)

You're doing a great job — give yourself a pat on the back!

I also want to share with you what happens when the image is smaller than the browser. In that case, it displays on multiple tiles. This is what you can expect to see.

Output

How to Add an Image & Background Image in HTML (7)

To prevent this from happening, you can use the background-repeat property and set it to no-repeat.

Here’s the CSS.

body { background-image: url(‘https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg’); background-repeat: no-repeat; color: #FFFFFF;}

The HTML stays the same. Here’s how it would look on the front end now.

You’ve solved the repeating problem, but now you have a lot of extra whitespace below and to the right of the image. To ensure the background image covers the entire body element — or, in other words, takes up the entire browser window — you can use the background-size property and set it to cover.

Then, to prevent the image from warping its dimensions, use the background-attachment property and set it to fixed. That way, the image will keep its original proportions.

Here’s the CSS.

body { background-image: url(‘https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg’); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; color: #FFFFFF;}

The HTML stays the same. Here's how it would look on the front end now.

Output

How to Add an Image & Background Image in HTML (8)

How to Insert a Background Image on an HTML Element

You can also set an image as the background of an HTML element rather than the entire web page.

For example, you could place HTML elements inside a div, then target the div with the CSS properties we used above. One difference is that instead of setting the background-size property to cover, we’re going to set it to 100% 100%.

That means the image will stretch horizontally and vertically as needed to fit the entire div element, without retaining its original dimensions.

Here’s the CSS and HTML.

<div> <h2>Background Image</h2> <p>In this example, the background image is specified for the div element.</p> <p>But you can specify the background image for any HTML element.</p> <p>Try it for a paragraph, heading, and more.</p></div><p>This paragraph is not contained in the div. Therefore it does not have an image background.</p>div { background-image: url(‘https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg’); background-repeat: no-repeat; background-attachment: fixed; background-size: 100% 100%; color: #FFFFFF;}

Here’s the result.

Output

How to Add an Image & Background Image in HTML (9)

When to Use CSS vs HTML to Add a Background Image

You might be wondering:So, if I can use HTML or CSS to add a background image, how do I decide when to use which? I'm here to help walk you through exactly why you'd use each of these properties.

For starters, using CSS when adding a background image to your website is smart because it makes your site code easier to maintain. The content of your site itself is presented in HTML, so you can use CSS to add stylistic changes. This means when you go into your site to maintain it, you'll have an easier time differentiating the content from the design aspects. Because of this, I advise you use CSS to add your background image.

Another reason is because when you use CSS, you have more control over the way that your image displays.CSS offers properties including background-size, background-position, and even background-repeat. Thanks to this, adjusting your image’s size and position is simpler than it would be otherwise.

Next, there’s also the performance update. When you add a CSS background image, it will load after your HTML content. This improves your page performance because content loads first.

As you can already tell, I highly suggest you stick with CSS for your background image needs.

How to Make an Image Link in HTML

Images are also effective links — you can link an icon or a high-res image. Either way, the process is the same: Enclose your <img> element in an <a> (anchor) tag, like so.

<a href=“URL”> <img src=“URL” alt=“descriptive text”/></a>

Here’s an interactive example — click the HubSpot logo to be taken to the HubSpot homepage.

See the Pen Create an Image Link by HubSpot (@hubspot) on CodePen.

Making Your Website Visual

Adding images to your website is important to your visitor experience and search engines. Whether you‘re building your website with a content management system or from scratch, it’s easy. You just need to know some HTML and CSS. Add images to your website today!

Editor's note: This post was originally published in September 2020 and has been updated for comprehensiveness.

Topics: HTML

How to Add an Image & Background Image in HTML (2024)

FAQs

How do you insert an image into HTML answer? ›

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file. The HTML image element is an “empty element,” meaning it does not have a closing tag.

How do I add a picture in HTML? ›

In order to put a simple image on a web page, we use the <img> element. This is a void element (meaning, it cannot have any child content and cannot have an end tag) that requires two attributes to be useful: src and alt . The src attribute contains a URL pointing to the image you want to embed in the page.

How to add background image in HTML without repeat? ›

To avoid the background image from repeating itself, set the background-repeat property to no-repeat .

How to make background image fit screen in HTML? ›

Set the background-size property to "100% 100%" and the background image will be stretched to cover the entire element, in this case the body element.

How do you display an image from a file in HTML? ›

In order to insert an image in HTML from a folder you will need to use the <img> tag. The src attribute is used to specify the location of the image. You can link to an image using either an absolute or relative file path.

How to add image button HTML? ›

Inside the div, we will add an email <input> field to get the email of the user. To make an image button in HTML, we will use the <img> tag inside the <button> tag. So in this way, the image will behave like a button. In the src attribute of the image tag, we write the address of the image we want to use.

How do I add an image icon in HTML? ›

How To Add Icons. To insert an icon, add the name of the icon class to any inline HTML element. The <i> and <span> elements are widely used to add icons. All the icons in the icon libraries below, are scalable vector icons that can be customized with CSS (size, color, shadow, etc.)

What is the correct HTML for inserting an image? ›

The HTML <img> tag is used to embed an image in a web page. Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image. The <img> tag is empty, it contains attributes only, and does not have a closing tag.

How do I add only one background image in HTML? ›

You have to set the background-repeat property to no-repeat . This will ensure that the background-image is not repeated. The image will only be shown once. To fit the image into each cell you can use background-size: cover and background-position: center .

How do I add a background image to a row in HTML? ›

Hover over your selected row and click on the green pencil icon to edit that row. Next, copy and paste the code below within the CSS tab of the row's design edit window. background-image: url("URL_OF_IMAGE_HERE"); As you can see in the image below, your image will now appear as the row's background.

How do I show an image behind text in HTML? ›

To add an image in the text background using HTML and CSS, create a container element (e.g., a `<div>`), set its background to the desired image using CSS ('background-image property), and adjust the text properties (e.g., color, size) to ensure readability.

How to make a background image fit to the screen in HTML? ›

The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover. Take a look at this example of it in action. Here's the HTML in the image below.

What is the correct HTML tag for adding a background color? ›

The correct answer to this question “What is the correct HTML for adding a background color” is option (b). <body bgcolor = “green”>. This is the correct HTML command that is used for adding a background color of choice.

How do I make the background of an image stay in place in HTML? ›

The background-attachment property determines if a background image will scroll with the rest of the page or stay fixed. The property accepts three values: scroll, fixed, and local. In this example, the background image will not scroll with the rest of the page. It will stay fixed at its position.

Top Articles
lapis armor or diamond armor | Fandom
What to Watch in 2022
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5717

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.