It is possible to include images in HTML documents by using <img>
element.
The browsers support different image formats like:
The <img>
tag is used to put an image in an HTML document.
<img src="images/tus.jpg" alt="dog image">
The src
attribute tells the browser where to find the image (URL to the image). It can be absolute, or relative, as the above example demonstrates. It is usually relative. This attribute is mandatory for the <img> element.
The alt
attribute is the alternative text describing the image. It provides meaningful information for users who are unable to see the image. Users will see this displayed if the image URL is wrong, the image is not in one of the supported formats, or if the image is not yet downloaded. If alt is omitted. validator will give an error.
Note that, like the <br> tag, because the <img> element does not enclose any content, no closing tag is required.
We can style images using CSS.
In addition to src, alt attributes, <img> supports:
width
and height
which defines image display size on the page. The units of these attributes are pixels (not specified in the attribute value).sizes
, srcset
- A list of one or more strings separated by commas indicating a set of source sizes, and a list of possible image sources to use.