HTML images are used to add visual content to web pages. They enhance user engagement, convey information visually, and make a website more interactive and appealing.
Images in HTML are embedded using the <img>
tag, which does not have a closing tag.
For instance, you can add an image to your webpage with the following syntax:
<img src="image.jpg" alt="Description of Image">
<img>
TagThe <img>
tag consists of several attributes that control how the image appears and behaves:
Example:
<img src="logo.png" alt="Company Logo" width="200" height="100" title="Our Logo">
Adding images in HTML is straightforward. Below are the steps:
The src
attribute specifies the path to the image file. The path can be:
https://example.com/image.jpg
)./images/photo.jpg
).
The alt
attribute is crucial for accessibility and SEO. It provides a text description of the image, which screen readers can interpret.
In modern web development, responsive images ensure that images adapt to different screen sizes and resolutions. Use the srcset
and sizes
attributes for better control:
<img src="small.jpg" srcset="medium.jpg 768w, large.jpg 1200w" sizes="(max-width: 768px) 100vw, 50vw" alt="Responsive Image">
This example loads different image versions based on the user's device width.
alt
attribute for accessibility and SEO.Common image formats used in web development include:
Here are the most common image file types, which are supported in all browsers (Chrome, Edge, Firefox, Safari, Opera):
Abbreviation | File Format | File Extension |
---|---|---|
APNG | Animated Portable Network Graphics | .apng |
GIF | Graphics Interchange Format | .gif |
ICO | Microsoft Icon | .ico, .cur |
JPEG | Joint Photographic Expert Group image | .jpg, .jpeg, .jfif, .pjpeg, .pjp |
PNG | Portable Network Graphics | .png |
SVG | Scalable Vector Graphics | .svg |
HTML allows you to use images as clickable links. Wrap the <img>
tag inside an <a>
tag:
<a href="https://example.com"> <img src="thumbnail.jpg" alt="Clickable Image"> </a>
Accessibility is essential when using images. Follow these tips:
alt=""
).<img>
element to define an imagesrc
attribute to define the URL of the imagealt
attribute to define an alternate text for an image, if it cannot be displayedwidth
and height
attributes or the CSS width
and height
properties to define the size of the imagefloat
property to let the image float to the left or to the rightAn absolute path includes the full URL of the image, while a relative path points to the image location relative to the HTML file.
Use CSS properties like max-width: 100%;
or the srcset
attribute for adaptive images.
The alt
attribute improves accessibility for visually impaired users and helps with SEO by describing the image content.
<img src="example.jpg" alt="Example Image">
<img src="default.jpg" srcset="small.jpg 600w, large.jpg 1200w" sizes="(max-width: 600px) 100vw, 50vw" alt="Responsive Example">
<a href="https://example.com"> <img src="logo.jpg" alt="Clickable Logo"> </a>