Skip to content

Media

Introduction

The Media component provides a flexible way to display images with built-in support for different aspect ratios and loading strategies. It uses semantic HTML and follows best practices for performance and accessibility.

When to use

The Media component is perfect for situations where you need responsive images with controlled aspect ratios:

  • Hero images
  • Product galleries
  • Blog post featured images
  • Profile pictures
  • Background images
  • Above-the-fold content
  • Image-heavy layouts

Usage

Learn how to implement the Media component in your project, from basic usage to advanced configurations.

Basic

---
import { Media } from 'accessible-astro-components'
---
<Media
src="/path/to/image.jpg"
alt="Descriptive alt text"
ratio="16:9"
/>

With custom loading

<Media
src="/hero-image.jpg"
alt="Hero section image"
ratio="21:9"
loading="eager"
fetchpriority="high"
/>

Props

Configure the Media component using these available props to customize its behavior and appearance.

PropTypeDefaultDescription
srcstring'https://fakeimg.pl/640x360'URL or path to the image
altstring''Alternative text for the image
ratio'1:1' | '4:3' | '16:9' | '21:9' | 'auto''auto'Aspect ratio of the image
loading'lazy' | 'eager''lazy'Image loading strategy
decoding'async' | 'sync' | 'auto''async'Image decoding behavior
fetchpriority'high' | 'low' | 'auto''auto'Resource loading priority
classstring''Additional CSS classes to apply

Accessibility

Accessibility isn’t an afterthought - it’s built into the core of this component through semantic HTML elements and proper image handling. The Media component is built with accessibility in mind:

  • Required alt text support
  • Maintains aspect ratio without layout shift
  • Proper loading states
  • Respects user’s reduced data preferences
  • Semantic HTML structure

Styling

Make the Media component your own with custom styling while maintaining its accessibility features.

/* Option 1: Using :global() in your style block */
<style>
:global(.media) {
border-radius: 0.5rem;
overflow: hidden;
background: light-dark(hsl(204 20% 94%), hsl(215 25% 15%));
}
:global(.media img) {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.2s ease;
}
:global(.media:hover img) {
transform: scale(1.05);
}
:global(.media[data-loading="true"])::before {
content: '';
position: absolute;
inset: 0;
background: light-dark(hsl(204 10% 90% / 0.5), hsl(215 15% 15% / 0.5));
}
</style>
/* Option 2: Using is:global on the style tag */
<style is:global>
.media {
border-radius: 0.5rem;
overflow: hidden;
background: light-dark(hsl(204 20% 94%), hsl(215 25% 15%));
}
.media img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.2s ease;
}
.media:hover img {
transform: scale(1.05);
}
.media[data-loading="true"]::before {
content: '';
position: absolute;
inset: 0;
background: light-dark(hsl(204 10% 90% / 0.5), hsl(215 15% 15% / 0.5));
}
</style>

Interactive examples

See the Media component in action with these practical examples demonstrating different aspect ratios and use cases.

Default style

Example image with default settings

Square aspect ratio

Example image with 1:1 aspect ratio

Widescreen format

Example image in 16:9 format