Integrating images into web pages is essential to create rich and attractive content. The HTML tag <img>
It is one of the basic components of the HTML language and an essential tool for any web designer. This article will explore in depth how to use the tag <img>
and associated best practices for effectively incorporating images into HTML.
Table of Contents
ToggleIntroduction to Label <img>
in HTML
The label <img>
, which comes from the word "image", is responsible for embedding images in HTML documents. It is important to highlight that <img>
is an empty tag, meaning it does not have a closing tag.
Basic Tag Syntax <img>
<img src="url-de-la-imagen" alt="image-description">
Essential attributes:
src
: Specifies the URL of the image.alt
: Provides an alternative description of the image displayed when the image cannot be loaded or for users using screen readers.
Secondary Attributes in <img>
Although src
y alt
are essential, there are other attributes that add functionality and accessibility to images:
title
: Provides additional information that usually appears as a tooltip when you mouse over the image.width
yheight
: Defines the dimensions of the image in pixels.loading
: Indicates whether the image should load lazily to optimize performance.srcset
: Allows you to specify different images for different resolutions, ideal for responsive design.
Advanced Implementation of <img>
in HTML
Let's see how we can use <img>
for different purposes and in more complex contexts.
Responsive Images with srcset
y sizes
<img src="imagen.jpg" srcset="imagen-320w.jpg 320w, imagen-480w.jpg 480w, imagen-800w.jpg 800w"
sizes="(max-width: 320px) 280px, (max-width: 480px) 440px, 800px"
alt="image-description">
This code allows the browser to choose the most suitable image based on the window size and the device's screen resolution.
Images in Retina Display and High Resolution
With the attribute srcset
, we can also cater to high pixel density screens (Retina Display):
<img src="imagen-standard.jpg" srcset="imagen-standard.jpg 1x, imagen-retina.jpg 2x" alt="image-description">
In this case, the browser will decide which version of the image to load based on the pixel density of the screen.
Load Images Lazily with loading="lazy"
To optimize page loading speed, it is possible to load images only when they are about to enter the visible area of the browser window:
<img src="imagen.jpg" loading="lazy" alt="image-description">
Important Attribute Details alt
The attribute alt
It is crucial not only for accessibility, but also for SEO. You should describe what the image expresses, not necessarily what it shows. For a decorative image that does not provide significant information, alt
can be empty (alt=""
).
Using Modern Image Formats such as webp
webp
is a modern format that offers better compression and quality than traditional formats such as jpeg
o png
. To use it, we can do the following:
<picture>
<source srcset="imagen.webp" type="image/webp">
<img src="imagen.jpg" alt="image-description">
</picture>
The browser will try to load the image .webp
and if it is not supported, it will load the image .jpg
.
Best Practices with <img>
In addition to knowing the syntax and attributes, it is important to follow certain best practices when working with the tag <img>
:
- Using relative paths instead of absolute paths to
src
: This makes image maintenance and site migration easier. - Image optimization: Make sure images are properly compressed to speed up loading.
- Use of
alt
with judgment: Accurately describes the content of each image to improve accessibility and SEO. - Definition of fixed dimensions: Helps prevent unexpected design readjustments.
- Testing on different browsers and devices: To ensure that images display correctly on all platforms.
Tools to Work with <img>
There are online tools that can help you optimize and convert images to different formats, as well as analyze the performance of images on your website:
- TinyPNG / TinyJPG: File size reduction
.png
y.jpg
. - ImageOptim: Image optimization for Mac.
- WebPageTest: Performance tests including image optimization.
Conclusion
The label <img>
It is much more than just a means of adding visual content to a web page. With the correct use and optimization of its attributes, it is a powerful resource for responsive and accessible web design. We hope this article helps you use images more effectively on your web pages, optimizing both performance and user experience.
Remember to consult the official documentation and always follow the best practices in your work as a web developer. Happy coding!