Components › Components

On this page

Aspect ratio

A box that keeps its shape as its width changes.

The clarusjs logo, in a 16:9 box

Usage #

tscl
@import uiAspectRatio from './aspect-ratio.tscl'

<div class="demo">
  <uiAspectRatio ratio="16/9">
    <img src="/logo.svg" alt="The clarusjs logo, in a 16:9 box" />
  </uiAspectRatio>
</div>

<style scoped>
  :scope {
    display: grid;
    gap: 12px;
    padding: 24px;
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
    background: var(--ui-bg);
    color: var(--ui-fg);
  }
</style>

Source #

Copy it next to your components as aspect-ratio.tscl.

tscl
{{-- Aspect ratio: a box that keeps its shape (ratio, '16/9' by default) as its width changes. --}}
@input ratio: string = '16/9'

<div data-part="aspect-ratio" [style]="'aspect-ratio: ' + ratio"><slot /></div>

<style scoped>
  @layer theme, base, components, utilities;
  @layer components {
    :where(:scope) {
      position: relative;
      width: 100%;
      overflow: hidden;
      border-radius: var(--ui-radius);
    }
    :where(:scope > img),
    :where(:scope > video),
    :where(:scope > iframe) {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
</style>

Styling #

Needs #