Aspect Ratio
The AspectRatio component renders content in a responsive container constrained to a specific aspect ratio.
Quick Start
Usage Scenarios
Video Embed
AspectRatio(
Iframe(src="https://www.youtube.com/embed/dQw4w9WgXcQ", cls="border-0"),
ratio="16/9",
)
Square Image
Ultra-Wide Banner
Classic 4:3
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
children |
Any |
Required | Content to display (images, videos, iframes, etc.) |
ratio |
str |
"16/9" |
Aspect ratio as width/height string (e.g. "16/9", "4/3", "1/1", "21/9") |
**kwargs |
Any |
- | Additional HTML attributes (cls, id, hx-, data-, etc.) |
Accessibility
- Ensure child media elements (images, iframes) have appropriate
alttext or titles. - Use
object-fit: coveron images for best visual results within the constrained container.
API Reference
faststrap.components.layout.aspect_ratio.AspectRatio(children, *, ratio='16/9', **kwargs)
Render content in a responsive aspect ratio container.
Uses the CSS aspect-ratio property for consistent sizing across viewports. Children fill the container; use object-fit on child media for best results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
Any
|
Content to display (images, videos, iframes, etc.) |
required |
ratio
|
str
|
Aspect ratio as width/height string (e.g. "16/9", "4/3", "1/1", "21/9") |
'16/9'
|
**kwargs
|
Any
|
Additional HTML attributes (cls, id, hx-, data-, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element constrained to the specified aspect ratio |