Aspect Ratio
Locks its content to a fixed width-to-height ratio.
Installation
Included in poetry-ui — available as
poetry_aspect_ratio
the moment you've installed Poetry,
with no per-component step. To own the source and edit it, copy it into your app:
bin/rails g poetry:add aspect-ratio
Default
Open Default standalone
16 / 9
<%= poetry_aspect_ratio(ratio: "16/9", class: "w-64 overflow-hidden rounded-lg bg-muted") do %>
<div class="flex size-full items-center justify-center text-sm text-muted-foreground">16 / 9</div>
<% end %>
Portrait
Open Portrait standalone
9 / 16
<%= poetry_aspect_ratio(ratio: "9/16", class: "w-24 overflow-hidden rounded-lg bg-muted") do %>
<div class="flex size-full items-center justify-center text-sm text-muted-foreground">9 / 16</div>
<% end %>
Square
Open Square standalone
1 / 1
<%= poetry_aspect_ratio(ratio: "1", class: "w-40 overflow-hidden rounded-lg bg-muted") do %>
<div class="flex size-full items-center justify-center text-sm text-muted-foreground">1 / 1</div>
<% end %>
With image
Open With image standalone
<%# The image fills the ratio box and is cropped to fit — size-full object-cover
keeps a 16/9 frame no matter the source photo's own dimensions.
(Inline gradient stands in for a real photo.) %>
<% photo = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NDAiIGhlaWdodD0iMzYwIj48ZGVmcz48bGluZWFyR3JhZGllbnQgaWQ9ImciIHgxPSIwIiB5MT0iMCIgeDI9IjEiIHkyPSIxIj48c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiM2MzY2ZjEiLz48c3RvcCBvZmZzZXQ9IjAuNTUiIHN0b3AtY29sb3I9IiM4YjVjZjYiLz48c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNlYzQ4OTkiLz48L2xpbmVhckdyYWRpZW50PjwvZGVmcz48cmVjdCB3aWR0aD0iNjQwIiBoZWlnaHQ9IjM2MCIgZmlsbD0idXJsKCNnKSIvPjxjaXJjbGUgY3g9IjUwMCIgY3k9IjkwIiByPSI0NiIgZmlsbD0iI2ZmZmZmZiIgb3BhY2l0eT0iMC44NSIvPjwvc3ZnPg==" %>
<%= poetry_aspect_ratio(ratio: "16/9", class: "w-80 overflow-hidden rounded-lg border") do %>
<%= image_tag photo, alt: "Sunrise over a coastal ridge", class: "size-full object-cover" %>
<% end %>
API
Poetry::Ui::AspectRatio::Component — options are
constructor keywords (the poetry_* helper forwards them);
slots are composed inside the block. Generated from the gem's source documentation.
| Option | Type | Details | Description |
|---|---|---|---|
| ratio: | String | required | A CSS <ratio>: \"16/9\", \"1\", \"1.5\" - kept a string so the fraction survives verbatim into the --ratio custom property. |
Styling
Every part carries a stable data-slot attribute — target
[data-slot=…] from your own CSS to restyle it. State rides
data attributes on the parts below. This contract is verified against rendered DOM in CI.
| Part | Description |
|---|---|
| [data-slot=aspect-ratio] | The ratio-locked box - the content block fills it |
CSS variables
| Part | Variable | Description |
|---|---|---|
| aspect-ratio | --ratio | always - the ratio: fraction verbatim ('16/9'), consumed by the theme's aspect-ratio rule |