HTML Image Tag and Attributes
The HTML <img>
tag is used to embed images in a webpage. It is a self-closing tag, meaning it does not have a closing </img>
tag. The <img>
tag has several attributes that define the image source, appearance, and behavior.
Basic Syntax
Attributes of <img>
Tag
src
(Source)- Specifies the path to the image file.
- Can be a relative path (
images/photo.jpg
) or an absolute URL (https://example.com/photo.jpg
). - Required attribute.
alt
(Alternative Text)- Provides a textual description of the image for accessibility.
- Shown when the image cannot be loaded or viewed.
- Helps with SEO (search engine optimization).
- Recommended attribute.
width
andheight
- Define the dimensions of the image.
- Can use pixels (
px
) or percentages (%
). - Helps browsers reserve space for the image during page load.
title
- Provides additional information about the image as a tooltip when hovered over.
loading
- Defines how and when the image should be loaded.
lazy
: Delays loading the image until it’s near the viewport.eager
: Loads the image immediately.
- Defines how and when the image should be loaded.
crossorigin
- Specifies if the image should be loaded with CORS (Cross-Origin Resource Sharing).
anonymous
: Loads the image without credentials.use-credentials
: Loads the image with credentials like cookies.
- Specifies if the image should be loaded with CORS (Cross-Origin Resource Sharing).
referrerpolicy
- Controls the referrer information sent when loading the image.
- Common values:
no-referrer
,origin
,same-origin
.
- Common values:
- Controls the referrer information sent when loading the image.
Comments
Post a Comment