Images · Astro Tech Blog

Images in HTML

Images make web pages visually engaging. HTML provides elements for embedding images, providing fallbacks, and adapting to different screen sizes.

img tag

The <img> element embeds an image in the page. It is a void element (self-closing).

HTML
<img src="photo.jpg" alt="A scenic mountain landscape">
Live Output Window

Required attributes

AttributePurpose
srcImage URL (path to the image file)
altAlternative text — describes the image for screen readers and when the image fails to load

Optional attributes

HTML
<img
src="photo.jpg"
alt="Sunset over the ocean"
width="800"
height="600"
loading="lazy"
decoding="async"
>
Live Output Window
AttributeValuesPurpose
widthpixelsDisplay width (maintains aspect ratio with height)
heightpixelsDisplay height
loadingeager (default), lazyLazy loading — defers loading off-screen images
decodingsync, async, autoImage decoding hint for the browser
fetchpriorityhigh, low, autoPriority hint for loading
referrerpolicyno-referrer, origin, etc.Referrer information
sizesmedia query + widthUsed with srcset for responsive images
srcsetcomma-separated URLs + widthsMultiple resolution options
crossoriginanonymous, use-credentialsCORS settings

Image formats

FormatUse CasePros
JPEG (.jpg)Photographs, complex scenesSmall file size, 16M colours
PNG (.png)Logos, icons, screenshotsLossless, transparency support
GIF (.gif)Simple animationsAnimations
WebP (.webp)Modern replacement for JPEG/PNGSmaller size, transparency + animation
AVIF (.avif)Next-gen formatEven smaller than WebP, HDR support
SVG (.svg)Icons, illustrations, logosScalable (vector), tiny file size

figure tag

<figure> groups an image with a caption (<figcaption>):

HTML
<figure>
<img src="chart.png" alt="Bar chart showing quarterly sales">
<figcaption>Figure 1: Quarterly sales for 2026</figcaption>
</figure>

<figure>
<figcaption>Team photo at the annual conference</figcaption>
<img src="team.jpg" alt="Team photo">
</figure>
Live Output Window

<figcaption> can go before or after the image. One <figure> can contain multiple images.

Responsive Images with srcset

The srcset attribute lets the browser choose the best image resolution based on the device’s screen size and pixel density:

Width-based selection

HTML
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w,
photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw,
(max-width: 1200px) 50vw,
33vw"
alt="Landscape photo"
>
Live Output Window
  • srcset: lists images with their actual width in pixels (400w, 800w, etc.).
  • sizes: tells the browser how wide the image will be displayed at different breakpoints (100vw = full viewport width, 50vw = half, 33vw = one-third).

The browser selects the smallest image that fits the layout at the device’s pixel density.

Pixel density selection

HTML
<img
src="icon-1x.png"
srcset="icon-1x.png 1x,
icon-2x.png 2x,
icon-3x.png 3x"
alt="Icon"
>
Live Output Window

Use 1x, 2x, 3x for standard, retina (2×), and super-retina (3×) displays.

picture tag

The <picture> element provides art direction — different images for different screen sizes or formats. It contains multiple <source> elements and one <img> fallback.

Format fallbacks (WebP with JPEG/PNG fallback)

HTML
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="Landscape">
</picture>
Live Output Window

The browser uses the first <source> with a format it supports. If none match, it falls back to the <img> tag.

Art direction (crop for mobile)

HTML
<picture>
<source
media="(max-width: 600px)"
srcset="photo-cropped.jpg"
>
<source
media="(min-width: 601px)"
srcset="photo-full.jpg"
>
<img src="photo-full.jpg" alt="Full landscape photo">
</picture>
Live Output Window

On narrow screens, a cropped version is shown. On wider screens, the full photo is displayed.

Combined: art direction + resolution switching

HTML
<picture>
<source
media="(max-width: 600px)"
srcset="photo-mobile-1x.jpg 1x,
photo-mobile-2x.jpg 2x"
>
<source
media="(min-width: 601px)"
srcset="photo-desktop-1x.jpg 1x,
photo-desktop-2x.jpg 2x"
>
<img src="photo-desktop-1x.jpg" alt="Landscape">
</picture>
Live Output Window

Detailed Table with Tag, attributes and attribute values

TagAttributeValuesDescription
<img>srcURLImage source (required)
<img>alttextAlternative text (required for accessibility)
<img>widthpixelsDisplay width
<img>heightpixelsDisplay height
<img>loadingeager, lazyLazy loading behaviour
<img>decodingsync, async, autoDecoding hint
<img>srcsetcomma-separated listResponsive image sources
<img>sizesmedia query + sizeDisplay size conditions
<img>fetchpriorityhigh, low, autoLoading priority
<img>crossoriginanonymous, use-credentialsCORS
<figure>Container for media + caption
<figcaption>textCaption for the figure
<picture>Container for multiple sources
<source>srcsetURL(s)Image source for picture
<source>mediamedia queryWhen this source applies
<source>typeMIME typeImage format hint
<source>sizesmedia query + sizeDisplay size conditions