What It Is & The right way to Use It Correctly in 2023


An HTML picture tag shows a picture on a webpage. It’s a self-closing tag (which means it’s structured in a means so you do not have so as to add a separate closing tag) that incorporates the picture supply and different attributes.

Right here’s an instance of a picture html tag:

<img src="https://instance.com/cat.jpg" alt="Furry white cat sitting on a wall">

Now, let’s go over the HTML tag for pictures (additionally referred to as an <img> tag) intimately.

The right way to Use the HTML Picture Tag

You’ll be able to insert the <img> tag in your HTML code anyplace you need to show a picture. Like throughout the hero part on a product web page.

And it at all times consists of two required attributes: 

  • src: Signifies the trail of the picture that’s proven
  • alt: Signifies various (alt) textual content that’s displayed if the picture fails to load and for people utilizing display readers or different text-to-speech instruments

The picture isn’t straight embedded into the web page however is loaded from the supply path within the tag. 

an example of an image loaded from the source path in the tag (shown in the screenshot below)

And the picture will be sourced from anyplace—i.e., on the identical server (known as a relative path) or a special one (known as an absolute path).

You may as well nest the picture tag inside different container tags—i.e., HTML tags with each opening and shutting tags.

Right here’s an instance of a picture tag nested inside an anchor tag that hyperlinks to a different web site:

<a href="https://instance.com"><img src="https://instance.com/cat.jpg" alt="furry white cat sitting on a wall"></a>

It creates a clickable picture hyperlink.

Kinds of HTML Picture Attributes

HTML picture attributes are used to specify details about the picture. You need to use them to regulate how the pictures are displayed, loaded, and extra.

Let’s check out every of those attributes one after the other.

The Src Attribute

That is crucial attribute. It specifies the trail to the picture.

Browsers can’t discover and show the picture with out its supply path.

And the supply will be both an absolute or a relative URL.

The Alt Attribute

This attribute is used to incorporate the choice textual content for the picture—a text-based clarification of the picture. 

Browsers show this textual content when the picture fails to load or can’t be discovered. Like when the consumer has a sluggish web connection.

And it seems to be like this on the webpage:

an example of an alt text for an image which failed to load

It additionally makes the picture accessible to visually impaired readers and people who want to take heed to content material reasonably than learn it. As a result of display readers and different text-to-speech instruments use alt textual content to explain the picture.

It’s additionally an essential attribute from an website positioning perspective. 

Why? 

As a result of alt textual content can assist search engines like google and yahoo like Google perceive your content material higher, which might result in larger rankings. And it’s particularly helpful for serving to your pictures seem in picture outcomes. Particularly in the event you embody related key phrases.

The Title Attribute

This attribute is used to set the picture’s title. To supply further context concerning the picture.

The title attribute is displayed as a tooltip—a component that shows details about the picture while you hover over it together with your mouse cursor.

That is what a tooltip seems to be like:

an example of title attribute displayed as a tooltip

The Longdesc Attribute

This attribute is used to hyperlink to a webpage with an in depth description of the picture. Which will be useful for complicated pictures that may’t be totally defined with alt textual content.

It may be the tackle of one other webpage or a component throughout the similar web page.

Nevertheless it’s not often used. As a result of most browsers don’t help it.

The Crossorigin Attribute

The crossorigin attribute is used when loading a picture from one other area.

It tells the browser to make one thing referred to as a cross-origin useful resource sharing (CORS) request. The CORS safety mechanism allows internet servers to regulate whether or not different domains can entry their sources. And protects delicate info from being accessed by way of unauthorized requests.

The crossorigin attribute can have the next values:

  • nameless: Sends a request with out credentials. It’s the identical as an empty crossorigin attribute. Any such request is used to entry publicly out there sources.
  • use-credentials: Sends a request with credentials for authentication—e.g., cookies, certificates, and many others. Any such request is used to entry non-public sources.

The Ismap Attribute

The ismap attribute represents a picture map (a picture with clickable areas) saved on the server. It’s solely used for clickable pictures (i.e., <img> tags contained in the <a> tags).

It’s a boolean attribute—which means it’s “true” when current and “false” when not. And it doesn’t comprise any values.

For instance:

<a href="https://instance.com/"><img src="https://instance.com/cat.jpg" alt="Furry white cat sitting on a wall" ismap></a>

When a consumer clicks the picture, the situation of the press can also be despatched with the request. It provides the cursor coordinates on the finish of the URL.

Within the above instance, clicking the picture at (x=33, y=45) coordinates will open the next URL:

https://instance.com/?33,45

This system or script on the server processes the request and performs a selected motion. Or takes the consumer to a selected web page.

It’s not often used lately. As a result of it’s not useful for customers with display readers. And utilizing JS/CSS as an alternative affords extra environment friendly performance, with out relying on the server for processing.

The Usemap Attribute

The usemap attribute represents a picture with clickable areas (referred to as a picture map).

Every space hyperlinks to a selected tackle–i.e., a webpage or useful resource.

Details about the places of those areas and addresses is saved in a <map> tag. The worth of the usemap attribute factors to the identify of the related map.

Due to this fact, the usemap attribute creates picture maps which might be processed inside browsers (referred to as client-side picture maps.)

Right here’s an instance utilizing the usemap attribute:

<img src="https://instance.com/cat.jpg" alt="Furry white cat sitting on a wall" usemap="#cat">
<map identify="cat">
<space form="rect" coords="0,0,200,400" href="left_part.html" alt="Left Half">
<space form="rect" coords="200,0,400,400" href="right_part.html" alt="Proper Half">
</map>

The usemap tag can’t be used for clickable pictures.

The Loading Attribute

This specifies how browsers ought to load the picture.

That’s decided by which of those values the attribute incorporates:

  • keen: The picture is loaded instantly (that is the default worth)
  • lazy: The picture isn’t loaded till it is required (i.e., when the consumer reaches that a part of the web page)

Picture lazy loading can enhance the load time. It will possibly enhance the web page efficiency rating in Google’s PageSpeed Insights—a instrument that measures web page efficiency throughout desktop and cell gadgets. 

an infographic showing Google PageSpeed insights score

Web page velocity is a confirmed Google rating issue. So, utilizing lazy loading for pictures generally is a good observe to enhance your web site’s website positioning.

The Referrerpolicy Attribute

This defines what referrer info to ship together with the picture request.

What’s a referrer?

A referrer is the web page the request is shipped from. That means the web page the place the picture goes to be embedded.

This attribute can comprise any of those values:

  • no-referrer: No referrer info is shipped with the request
  • no-referrer-when-downgrade: No referrer info is shipped with requests from HTTPS to HTTP
  • same-origin: The entire URL is shipped with same-origin requests. No referrer info is shipped for cross-origin requests.
  • origin: The origin (scheme, host, and port) is shipped
  • strict-origin: The origin (scheme, host, and port) is shipped with HTTPS to HTTPS and HTTP to any origin requests. No referrer info is shipped with HTTPS to HTTP requests.
  • origin-when-cross-origin: The origin (scheme, host, and path) is shipped with cross-origin requests. The entire URL, together with the trail, is shipped with same-origin requests.
  • strict-origin-when-cross-origin: It’s the identical because the “origin-when-cross-origin,” however no referrer info is shipped with HTTPS to HTTP requests. That is the default worth.
  • unsafe-url: Theorigin, path, and question string are despatched. (It’s not beneficial because it’s unsafe to share the referrer’s full info.)

The Srcset Attribute

This attribute is used for displaying responsive pictures.

It incorporates hyperlinks to the identical picture in a number of sizes. And every picture’s dimension is specified both by width or density.

This helps the browser select essentially the most applicable picture from the set in line with the display dimension and backbone.

The Sizes Attribute

This attribute is used together with the srcset attribute. 

It tells the browser which picture dimension to make use of underneath totally different situations.

The Fashion Attribute

This attribute is used to use inline type to the picture tag. And can override every other international type utilized to the picture.

The type attribute can management the looks of the picture tag. It will possibly comprise parts corresponding to picture border, shadow, and alignment.

The Top Attribute

This attribute is used to specify the peak of a picture in pixels. Like within the picture beneath. 

an example of a height attribute

Not specifying the peak will load the picture in its authentic peak. Which could now work together with your web page’s structure.

The Width Attribute

This attribute is used to specify the width of a picture in pixels. 

And a picture with no width attribute will seem in its authentic width.

Tip: It’s beneficial that you just outline each the peak and width for pictures. To stop the structure from altering after the picture has loaded.

Some previous HTML picture attributes had been changed in HTML5. 

Browsers could help these attributes for compatibility, however it isn’t beneficial to make use of them. They’re deprecated (which means not beneficial).

Right here’s the checklist of deprecated attributes:

  • align
  • border
  • hspace
  • longdesc
  • identify
  • vspace

Browsers That Assist the HTML Picture Tag

The HTML <img> tag is a widely-accepted aspect. 

It’s supported by all the favored web browsers, together with:

  • Chrome
  • Safari
  • Edge
  • Firefox
  • Opera

Most cell browsers additionally help the picture tag.

Right here’s the checklist of the favored picture codecs supported by Chrome, Edge, Firefox, Safari, and Opera:

  • .apng
  • .bmp
  • .gif
  • .jpeg
  • .jpg
  • .png
  • .webp
  • .svg

Greatest Practices for Making certain Accessibility

Photos make webpages visually interesting to readers.

However not everybody can see them effectively or in any respect. So, it’s additionally essential to make them accessible to visually impaired individuals. 

Bear in mind, display readers use pictures’ various textual content to explain them. That means it is a good observe to write down descriptive alt textual content for all non-decorative pictures. 

However what are these? 

Non-decorative pictures are the a part of the web page’s foremost content material. And comprise info it is advisable perceive the web page.

Then again, ornamental picture parts don’t comprise any info. They improve the webpage visually. So, hold the choice textual content clean (alt=“”) for these parts so display readers can skip them.

Right here’s a superb instance of descriptive alt textual content: “Furry white cat sitting in entrance of a wall” is healthier than “Picture of cat.”

As a result of the previous textual content is extra informative. It offers context concerning the picture for individuals utilizing display readers.

Listed here are some greatest practices to make pictures extra accessible:

  • Use contextual and descriptive alt textual content
  • Don’t add various textual content for ornamental pictures just like the one beneath
an example of a decorative image on "Semrush Features" page
  • For clickable pictures, describe the goal hyperlinks within the various texts of the pictures
  • Keep away from embedding essential textual content that doesn’t seem anyplace else on the web page inside pictures. If the picture incorporates textual content, attempt to describe it within the various textual content.
  • Write the choice textual content in the identical language as the remainder of the web page’s content material

If the choice textual content is left clean, some display readers could default to the picture’s file identify. So, it’s essential to make use of descriptive file names as effectively.

For instance, the file identify “furry-cat.png” is healthier than “image-202305.png.”

Google additionally typically makes use of file names to know the pictures. Which means a contextual picture file identify can be helpful for picture website positioning.

HTML Picture Tag Examples

Now, let’s go over some particular examples utilizing the picture tag in HTML:

Picture with Particular Dimensions

You need to use the peak and width attributes to specify a picture’s dimensions. 

Right here’s an instance of a picture that’s 150×150 pixels:

<img src="https://instance.com/cat.jpg" alt="Furry white cat sitting on a wall" peak="150" width="150">

Picture with Inline Fashion

You’ll be able to apply inline type to the picture through the use of the type aspect. 

Right here’s an instance of including a black border to a picture utilizing inline type:

<img src="https://instance.com/cat.jpg" alt="Furry white cat sitting on a wall" type="border:3px stable black">

Animated Picture

The <img> tag in HTML can show animated pictures like GIFs. 

For instance, the beneath tag shows a GIF:

<img src="https://instance.com/rabbit.gif" alt="Rabbit leaping throughout the grass">

Picture as a Button

To make use of a picture as a button, nest the <img> tag contained in the <button> tag.

Like this:

<button kind="submit"><img src="https://instance.com/submit.jpg" alt="Submit the type"></button>

Lazy Loading Picture

You’ll be able to allow lazy loading for a picture by setting its loading attribute to “lazy.” 

For instance:

<img src="https://instance.com/rabbit.gif" alt="Rabbit leaping throughout the grass" loading="lazy">

HTML picture tags allow you to embed pictures that make your pages seem visually wealthy. And picture attributes can supply a greater, extra accessible consumer expertise. And increase your rankings in search engines like google and yahoo.

However it is advisable guarantee they’re used appropriately.

Invalid HTML tags and attributes could cause technical points. They will negatively have an effect on the consumer expertise and diminish your search engine rankings. Due to this fact, it is best to repeatedly audit your web site to trace such points.

You’ll be able to verify for a lot of these points utilizing Website Audit

Open the instrument and comply with our configuration directions. Then, click on “Begin Website Audit.”

"Site Audit Settings" page

Click on the “Points” tab. Then, enter “pictures” into the search bar to search out points like lacking alt attributes.

how to find missing alt attributes in Site Audit "Issues” tab

And you’ll find different technical points together with your web site. Probably the most pressing ones are referred to as “Errors.”

You may as well set a web site audit schedule by clicking the gear icon and scrolling all the way down to “Website Audit settings.”

site audit schedule set to "Schedule: Weekly, Every Tuesday" in “Site Audit settings”

Strive Website Audit now without cost.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles