<img> HTML – Image Tag Tutorial (2024)

In HTML, you use the <img> tag to add images to websites. It is an inline and empty element, which means that it doesn't start on a new line and doesn't take a closing tag (unlike the paragraph (<p>) tag, for instance).

The <img> tag takes several attributes, of which src, height, width, and alt are the most important.

Knowing the ins and outs along with some best practices of the <img> tag is crucial because images can negatively affect your site's load time and SEO.

So in this tutorial, we will take a look at how to add images to websites using the <img> tag, how to use its attributes, some best practices, and modern approaches to using <img>.

Here's the basic syntax for adding an <img> tag to your HTML:

<img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs" />

Now let's talk about its attributes and how they work.

HTML <img> Tag Attributes

The src Attribute

The src attribute signifies the image source. Without it, the tag itself wouldn't be functional in the real world.

It indicates to the browser where to find the image. So it takes a relative path if the image is hosted locally, or an absolute URL if the image is hosted online.

The alt Attribute

The alt attribute specifies an alternative text for the image. This could be the text that shows during a network failure, for example. Or it could display something when the image source is wrongly specified, so users know what the image is about.

In the code snippet below, the image source is wrongly specified, showing you the role that the alt attribute plays:

<img src="assets/images/ring-tailed-lemur.webp" alt="A Group of Ring-tailed Lemurs"/>

This is the CSS that centers the image horizontally and vertically:

body { display: flex; align-items: center; justify-content: center; flex-direction: column; height: 100vh; }

And it looks like this:
<img> HTML – Image Tag Tutorial (1)

The alt attribute is very important for 2 other reasons:

  • SEO: it indicates to web crawlers what the image is about
  • Accessibility: it helps screen readers know what the image is about so they can report that to visually impaired people. In addition, it lets users with low bandwidth know what the image is about.

The width and height Attributes

You can use these attributes to specify a certain width and height for your images. With these attributes, you can resize the image down or up.

Ideally, though, you shouldn't resize an image with these attributes. We'll touch on this more under best practices.

HTML <img> Tag Best Practices

Do not resize an image with the width and height attributes.

This is a bad practice because it can make the image appear distorted and can affect the quality.

Instead, you can optimize the image to your desired dimensions with photo editing software such as Photoshop.

In the code snippet below, I specify a width and height for the image – a bad practice:

<img src="assets/images/ring-tailed-lemurs.webp" height="440px" width="440px" alt="A Group of Ring-tailed Lemurs"/>

The image looks like this:
<img> HTML – Image Tag Tutorial (2)

Without using the width and height attributes, the image looks like this:
<img> HTML – Image Tag Tutorial (3)

Looks better? Yes!

Name Your Images Appropriately

Naming images appropriately can help search engines understand what the image is about. For example, name an image ring-tailed-lemurs.webp instead of photo-1580855733764-084b90737008.webp. The latter is not enough for search engine optimization (SEO).

Reduce Image File Size

The image's file size is crucial when it comes to page speed. A smaller image size (that preserves the image's quality) reduces load time while larger images take forever to load.

There are several tools and various software that can help you do this. Some examples are imageOptim, jStrip, and PNGGauntet. And if you're concerned about SEO, you'll want to look into these – as page speed is an important ranking factor.

Host Images with a CDN

Imagine if a website is hosted in the United States but a user in Africa wants to accessed it. Assets such as images and icons would have to travel from The States to Africa, which in turn slows download time.

Using a CDN (Content Delivery Network) will allow the website's images to be cached across several locations around the world. The CDN can then serve them from locations closest to the user, improving load time and providing a better user experience.

Cloudflare is a popular CDN that a lot of developers use to host their images.

Use Descriptive Alternative Text

Using descriptive alternative text helps search engines understand what the image is about. But it doesn't end there – the alt text must also be relevant to the image.

For example, use this:

<img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs"/>

Insead of this:

<img src="assets/images/ring-tailed-lemurs.webp" alt="Lemurs" />

Use the title Attribute to Show Tooltips

Just like the alt attribute, you can use the title attribute to show additional information about the image. Browsers display this as a tooltip when the user hovers over the image.

<img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs" title="Ring-tailed lemurs are led by females"/>

<img> HTML – Image Tag Tutorial (4)

<img> Tag Modern Approaches

There are various ways you can use the <img> tag that are a bit more up to date and modern. Let's look at some of them now.

Lazy Load Images

Lazy loading is a newish "load what is needed" concept. With lazy loading, the image is loaded only when the user scrolls to its viewport.

This is in contrast to eager loading, which loads every image immediately after the page is rendered by the browser.

To apply lazy loading, add the loading attribute to the <img> tag and set the value to “lazy”.

<img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs" title="Ring-tailed lemurs are led by females" loading="lazy"/>

Images are often quite high quality and large these days, but this can negatively impact user experience and SEO – hence the introduction of lazy loading.

Use the <figure> and <figcaption> Tags

Often, you might need to specify to the user the caption of an image. A lot of developers do this by placing a <p> tag right after the <img>.

This might not be wrong, but it defies best practices and does not associate the caption with the image, so search engines won’t understand what it is.

<img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs" title="Ring-tailed lemurs are led by females" loading="lazy"/><p>Ring-tailed lemurs are social animals</p>

<img> HTML – Image Tag Tutorial (5)

Its is clear that there is no association between the image and the caption in the above example.

HTML5 introduced the <figure> and <figcaption> elements to help with this. You wrap the <img> tag inside a <figure> element, and you specify a caption within the <figcaption> element.

This helps search engines associate the caption with the image, leading to better performance and SEO.

The snippets of code below and the screenshots show you an image with and without the <figure> and <figcaption> elements:

<figure> <img src="assets/images/ring-tailed-lemurs.webp" alt="A Group of Ring-tailed Lemurs" title="Ring-tailed lemurs are led by females" loading="lazy" /><figcaption>Ring-tailed lemures are social animals</figcaption></figure>

<img> HTML – Image Tag Tutorial (6)

You can see now that the image and the caption are beautifully associated.

Use the .webP Image Format

.webP is an image format created by Google. According to the creator, it's an image format lower in size than its counterparts - JPG, JPEG, PNG, but with the same quality.

This format has been getting more and more widely accepted and is considered the nextgen image format for the web.

Conclusion

I hope this article helps you understand how the <img> tag works in HTML so you can use it properly in your projects. If you do so, it'll help improve your user experience and SEO.

Thanks a lot for reading, and keep coding.

<img> HTML – Image Tag Tutorial (2024)

FAQs

How to insert image using img tag in HTML? ›

First, the title attribute is title =”(your title)”. Next, set your alt attribute, which explains the image in detail. Finally, set the height and width of your image. Use the code <img src=”(your title)” alt=”Image” height=”(your image height)” width=”(your image width)”>.

What is IMG tag HTML? ›

The <IMG> tag is used to incorporate in-line graphics (typically icons or small graphics) into an HTML document. This element is NOT intended for embedding other HTML text. For large figures with captions and text flow see FIG element. Example: <IMG SRC="tajmahal.gif" ALT="The Taj Mahal">

How to add local image in img tag 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.

What is the difference between IMG and picture tag in HTML? ›

Difference between <picture> and <img>

Generally speaking you will use <img> if you use different sizes of the same image and <picture> if you use different images like a portrait of a house on small screens and a landscape image of the same house on bigger screens.

How do I upload an image from an IMG tag? ›

How to Add an Image in HTML. The <img> tag in HTML allows you to link an image from the internet to a page. Add <img> to a new line, then put your image's URL into a src (source) attribute. For example, <img src="http://www.website.com/my-dog.jpg">.

How to display an image in HTML? ›

HTML Images Syntax

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.

Why is my image not showing up in HTML? ›

Make sure that the file type is supported by the browser (e.g. supported types include PNG, JPEG, and GIF). Additionally, check if the filename is spelled correctly and its case-sensitivity is correct. Finally, try adding a '/' at the beginning of the src path.

Is IMG tag closed in HTML? ›

To add an image to your website you will need to use the <img> tag. This tag is different than other tags, in that it has no closing tag. It is called a self-closing tag, which means that there is just a slash at the end of the opening tag (ex. <img />).

How to write an img tag? ›

Syntax. The <img> tag is written as <img src="" alt=""> (no end tag) with the image URL inserted between the double quotes of the src attribute. The srcset attribute can also be used in order to specify different images to use in different situations (e.g. high-resolution displays, small monitors, etc).

How do I link an image in IMG? ›

Image as a link

By adding the <img> tag inside an <a> tag the browser can see that the image should be clickable. You have turned the image into a link! If you are using WordPress then you can add this HTML code to your page using the text view in the page editor.

How to set image size in img tag in HTML? ›

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How to use HTML img? ›

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.

What image type is the IMG tag? ›

The <img> tag displays an image in a web page. Accepted image formats include gif, jpg and png. Most browsers also support newer formats: apng, avif, and webp.

How do I use a picture tag? ›

The <picture> element contains two tags: one or more <source> tags and one <img> tag. The browser will look for the first <source> element where the media query matches the current viewport width, and then it will display the proper image (specified in the srcset attribute).

How do you put an image inside an input tag in HTML? ›

HTML Demo: <input type="image">
  1. <p>Sign in to your account:</p>
  2. <div>
  3. <label for="userId">User ID</label>
  4. <input type="text" id="userId" name="userId" />
  5. </div>
  6. <input type="image" id="image" alt="Login" src="/media/examples/login-button.png" />
Apr 29, 2024

How to use PNG in IMG tag? ›

Here are the steps:
  1. Locate the PNG or SVG file on your computer.
  2. Upload the file to your website server or hosting service.
  3. In your HTML code, use the <img> tag with the src attribute to specify the file location.

Why is my image not showing in HTML? ›

Make sure that the file type is supported by the browser (e.g. supported types include PNG, JPEG, and GIF). Additionally, check if the filename is spelled correctly and its case-sensitivity is correct. Finally, try adding a '/' at the beginning of the src path.

Top Articles
9 Best Movies Like Shutter Island And Where To Watch Them?
ciąża owiec - i jak określić, jaka długość: zrozum wszystkie szczegóły
The Tribes and Castes of the Central Provinces of India, Volume 3
Time in Baltimore, Maryland, United States now
Garrison Blacksmith Bench
Ingles Weekly Ad Lilburn Ga
Crocodile Tears - Quest
Graveguard Set Bloodborne
Violent Night Showtimes Near Amc Fashion Valley 18
Simple Steamed Purple Sweet Potatoes
Thayer Rasmussen Cause Of Death
Brutál jó vegán torta! – Kókusz-málna-csoki trió
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Magicseaweed Capitola
Belle Delphine Boobs
Dc Gas Login
Dit is hoe de 130 nieuwe dubbele -deckers -treinen voor het land eruit zien
Les Schwab Product Code Lookup
5 high school volleyball stars of the week: Sept. 17 edition
Truth Of God Schedule 2023
Bend Pets Craigslist
Sam's Club La Habra Gas Prices
Best Forensic Pathology Careers + Salary Outlook | HealthGrad
Sni 35 Wiring Diagram
Craigslist Pinellas County Rentals
Azur Lane High Efficiency Combat Logistics Plan
Boston Dynamics’ new humanoid moves like no robot you’ve ever seen
Craigslistodessa
25 Best Things to Do in Palermo, Sicily (Italy)
Turbo Tenant Renter Login
Die 8 Rollen einer Führungskraft
Imagetrend Elite Delaware
R/Orangetheory
Martin Village Stm 16 & Imax
Roch Hodech Nissan 2023
Ourhotwifes
#1 | Rottweiler Puppies For Sale In New York | Uptown
20+ Best Things To Do In Oceanside California
Soulstone Survivors Igg
Enjoy4Fun Uno
Google Chrome-webbrowser
Michael Jordan: A timeline of the NBA legend
Ashoke K Maitra. Adviser to CMD&#39;s. Received Lifetime Achievement Award in HRD on LinkedIn: #hr #hrd #coaching #mentoring #career #jobs #mba #mbafreshers #sales…
The Banshees Of Inisherin Showtimes Near Reading Cinemas Town Square
Gym Assistant Manager Salary
Mychart Mercy Health Paducah
Theater X Orange Heights Florida
Xre 00251
Union Supply Direct Wisconsin
Barber Gym Quantico Hours
Buildapc Deals
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5686

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.