What is the attributes of the IMG tag used to point the location of image?

HTML img tag is used to display image on the web page. HTML img tag is an empty tag that contains attributes only, closing tags are not used in HTML image element.

Let's see an example of HTML image.

Test it Now

Output:

What is the attributes of the IMG tag used to point the location of image?

Attributes of HTML img tag

The src and alt are important attributes of HTML img tag. All attributes of HTML image tag are given below.

1) src

It is a necessary attribute that describes the source or path of the image. It instructs the browser where to look for the image on the server.

The location of image may be on the same directory or another server.

2) alt

The alt attribute defines an alternate text for the image, if it can't be displayed. The value of the alt attribute describe the image in words. The alt attribute is considered good for SEO prospective.

3) width

It is an optional attribute which is used to specify the width to display the image. It is not recommended now. You should apply CSS in place of width attribute.

4) height

It h3 the height of the image. The HTML height attribute also supports iframe, image and object elements. It is not recommended now. You should apply CSS in place of height attribute.


Use of height and width attribute with img tag

You have learnt about how to insert an image in your web page, now if we want to give some height and width to display image according to our requirement, then we can set it with height and width attributes of image.

Example:

Test it Now

Output:

What is the attributes of the IMG tag used to point the location of image?

Note: Always try to insert the image with height and width, else it may flicker while displaying on webpage.


Use of alt attribute

We can use alt attribute with

What is the attributes of the IMG tag used to point the location of image?


How to get image from another directory/folder?

To insert an image in your web, that image must be present in your same folder where you have put the HTML file. But if in some case image is available in some other directory then you can access the image like this:

In HTML, you use the

What is the attributes of the IMG tag used to point the location of image?
0 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 (
What is the attributes of the IMG tag used to point the location of image?
1) tag, for instance).

The

What is the attributes of the IMG tag used to point the location of image?
0 tag takes several attributes, of which
What is the attributes of the IMG tag used to point the location of image?
3,
What is the attributes of the IMG tag used to point the location of image?
4,
What is the attributes of the IMG tag used to point the location of image?
5, and
What is the attributes of the IMG tag used to point the location of image?
6 are the most important.

Knowing the ins and outs along with some best practices of the

What is the attributes of the IMG tag used to point the location of image?
0 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

What is the attributes of the IMG tag used to point the location of image?
0 tag, how to use its attributes, some best practices, and modern approaches to using
What is the attributes of the IMG tag used to point the location of image?
0.

Basic HTML
What is the attributes of the IMG tag used to point the location of image?
0 Tag Syntax

Here's the basic syntax for adding an

What is the attributes of the IMG tag used to point the location of image?
0 tag to your HTML:

What is the attributes of the IMG tag used to point the location of image?

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

HTML
What is the attributes of the IMG tag used to point the location of image?
0 Tag Attributes

The
What is the attributes of the IMG tag used to point the location of image?
3 Attribute

The

What is the attributes of the IMG tag used to point the location of image?
3 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
What is the attributes of the IMG tag used to point the location of image?
6 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

What is the attributes of the IMG tag used to point the location of image?
6 attribute plays:

What is the attributes of the IMG tag used to point the location of image?

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:

What is the attributes of the IMG tag used to point the location of image?

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
What is the attributes of the IMG tag used to point the location of image?
5 and
What is the attributes of the IMG tag used to point the location of image?
4 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
What is the attributes of the IMG tag used to point the location of image?
0 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:

What is the attributes of the IMG tag used to point the location of image?

The image looks like this:

What is the attributes of the IMG tag used to point the location of image?

Without using the width and height attributes, the image looks like this:

What is the attributes of the IMG tag used to point the location of image?

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

What is the attributes of the IMG tag used to point the location of image?
0 instead of
What is the attributes of the IMG tag used to point the location of image?
1. 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:

What is the attributes of the IMG tag used to point the location of image?

Insead of this:

What is the attributes of the IMG tag used to point the location of image?

Use the
What is the attributes of the IMG tag used to point the location of image?
2 Attribute to Show Tooltips

Just like the

What is the attributes of the IMG tag used to point the location of image?
6 attribute, you can use the
What is the attributes of the IMG tag used to point the location of image?
2 attribute to show additional information about the image. Browsers display this as a tooltip when the user hovers over the image.

What is the attributes of the IMG tag used to point the location of image?

What is the attributes of the IMG tag used to point the location of image?

What is the attributes of the IMG tag used to point the location of image?
0 Tag Modern Approaches

There are various ways you can use the

What is the attributes of the IMG tag used to point the location of image?
0 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

What is the attributes of the IMG tag used to point the location of image?
0 tag and set the value to “lazy”.

What is the attributes of the IMG tag used to point the location of image?

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
What is the attributes of the IMG tag used to point the location of image?
8 and
What is the attributes of the IMG tag used to point the location of image?
9 Tags

Often, you might need to specify to the user the caption of an image. A lot of developers do this by placing a

What is the attributes of the IMG tag used to point the location of image?
1 tag right after the
What is the attributes of the IMG tag used to point the location of image?
0.

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.

What is the attributes of the IMG tag used to point the location of image?

Ring-tailed lemurs are social animals

What is the attributes of the IMG tag used to point the location of image?

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

HTML5 introduced the

What is the attributes of the IMG tag used to point the location of image?
8 and
What is the attributes of the IMG tag used to point the location of image?
9 elements to help with this. You wrap the
What is the attributes of the IMG tag used to point the location of image?
0 tag inside a
What is the attributes of the IMG tag used to point the location of image?
8 element, and you specify a caption within the
What is the attributes of the IMG tag used to point the location of image?
9 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

What is the attributes of the IMG tag used to point the location of image?
8 and
What is the attributes of the IMG tag used to point the location of image?
9 elements:

What is the attributes of the IMG tag used to point the location of image?
Ring-tailed lemures are social animals

What is the attributes of the IMG tag used to point the location of image?

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

What is the attributes of the IMG tag used to point the location of image?
0 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.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


What is the attributes of the IMG tag used to point the location of image?
Kolade Chris

Web developer and technical writer focusing on frontend technologies.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

What attributes are used with the IMG tag?

The img element has two required attributes: src : The source location (URL) of the image file. alt : The alternate text. This is used to describe the image for someone who cannot see it because they are either using a screen reader or the image src is missing.

Which of the following attributes that specify the location of an image?

b src attribute is used to specify the location of an image file.