Links and images are fundamental components of the web. This guide covers how to create hyperlinks and add images to your HTML documents, as well as other basic media elements.
The <a>
element (anchor) creates a hyperlink to web pages, files, email addresses, locations on the same page, or anything else a URL can address.
Attribute | Value | Description |
---|---|---|
href |
URL or path | Specifies the destination of the link (required) |
target |
_blank, _self, _parent, _top | Specifies where to open the linked document |
download |
filename (optional) | Specifies that the target will be downloaded when clicking the link |
rel |
nofollow, noopener, noreferrer, etc. | Specifies the relationship between the current document and the linked document |
title |
text | Provides additional information about the link (shown as a tooltip) |
target="_blank"
to open links in a new tab, always include rel="noopener noreferrer"
. This prevents the new page from accessing the window.opener property, which can be a security risk.
You can create anchor points (bookmarks) within a page to link to specific sections:
You can also link to specific sections on other pages:
The <img>
element is used to embed images in an HTML document.
Attribute | Value | Description |
---|---|---|
src |
URL or path | Specifies the path to the image (required) |
alt |
text | Provides alternative text for the image (required for accessibility) |
width |
pixels or percentage | Specifies the width of the image |
height |
pixels or percentage | Specifies the height of the image |
loading |
eager, lazy | Specifies if the image should be loaded immediately or deferred |
title |
text | Shows additional information as a tooltip when hovering |
Example of an image with width 500px and height 300px
alt
attribute with descriptive text. It helps users with screen readers and displays if the image fails to load. If an image is purely decorative, use an empty alt attribute (alt=""
) but don't omit it.
You can make an image function as a link by wrapping the <img>
element with an <a>
element:
Image maps allow you to define clickable areas within an image:
The <map>
element defines an image map with clickable areas. The <area>
element specifies the clickable areas and their associated links.
Shape Value | Coords Format | Description |
---|---|---|
rect |
x1,y1,x2,y2 | Defines a rectangular region (top-left and bottom-right coordinates) |
circle |
x,y,r | Defines a circular region (center coordinates and radius) |
poly |
x1,y1,x2,y2,...,xn,yn | Defines a polygon (all coordinates of the polygon) |
default |
N/A | Defines the entire region |
The simplest way to make an image responsive is to set its maximum width to 100% of its container and let the height adjust automatically to maintain the aspect ratio.
The <picture>
element gives more control by allowing you to specify different image sources for different screen sizes or device capabilities.
The srcset
and sizes
attributes allow the browser to choose the most appropriate image based on the viewport size and device resolution.
srcset
, the w
unit represents the intrinsic width of the image in pixels. The browser uses this information along with the sizes
attribute to determine which image to load.
The <figure>
and <figcaption>
elements provide semantic way to associate captions with images:
<figure>
element represents self-contained content, referenced as a single unit from the main content. The <figcaption>
element provides a caption or legend for the figure.
Attribute | Value | Description |
---|---|---|
controls |
N/A | Displays playback controls |
autoplay |
N/A | Starts playing automatically (often blocked by browsers) |
loop |
N/A | Plays the media again when finished |
muted |
N/A | Mutes the audio output |
preload |
auto, metadata, none | Specifies if/how the media should be loaded when the page loads |
poster |
URL | Specifies an image to be shown while the video is downloading (video only) |
<audio>
and <video>
elements for browsers that don't support these elements. Consider adding captions or transcripts for videos to make them accessible to everyone.
A favicon is a small icon associated with a website, displayed in the browser tab, bookmarks, and other areas.
favicon.ico
file should be placed in the root directory of your website for older browsers that look for it there automatically.
rel="noopener noreferrer"
with target="_blank"
alt
text for imagesloading="lazy"
for images below the foldautoplay
for videos with sound (consider user preferences)