Components

AspectRatio

Container that enforces a fixed aspect ratio for its child content.

Category: Layout / Composite

Import

import { AspectRatio } from "@hareru/ui"

Exports

NameTypeDescription
AspectRatioComponentMain aspect ratio container
AspectRatioPropsTypeProps interface

Types

export interface AspectRatioProps extends React.HTMLAttributes<HTMLDivElement> {
  ratio?: number
}

Key Props

PropTypeDefaultDescription
rationumber1Width-to-height ratio. Use 16/9, 4/3, 1, etc.

Common Ratios

RatioExpressionUse Case
1:11Avatar, thumbnail
16:916/9Video, banner
4:34/3Classic photo
3:23/2Photography
2:12/1Wide banner

Usage

import { AspectRatio } from "@hareru/ui"

// Video embed (16:9)
<AspectRatio ratio={16 / 9}>
  <img
    src="photo.jpg"
    alt="Photo"
    style={{ objectFit: "cover", width: "100%", height: "100%" }}
  />
</AspectRatio>

// Square (1:1)
<AspectRatio ratio={1}>
  <img src="avatar.jpg" alt="Avatar" style={{ objectFit: "cover", width: "100%", height: "100%" }} />
</AspectRatio>

// YouTube embed
<AspectRatio ratio={16 / 9}>
  <iframe
    src="https://www.youtube.com/embed/dQw4w9WgXcQ"
    title="Video"
    style={{ width: "100%", height: "100%", border: "none" }}
    allowFullScreen
  />
</AspectRatio>

Notes

  • Implemented with CSS aspect-ratio property (no padding-top hack).
  • The child is positioned absolutely inside the container to fill all available space.
  • Works with any content: images, videos, iframes, maps, or custom components.
  • The outer container takes the full width of its parent (width: 100%).

On this page