aspect-ratio

Các tiện ích để kiểm soát tỷ lệ khung hình của một phần tử.

ClassStyles
aspect-<ratio>
aspect-ratio: <ratio>;
aspect-square
aspect-ratio: 1 / 1;
aspect-video
aspect-ratio: var(--aspect-video); /* 16 / 9 */
aspect-auto
aspect-ratio: auto;
aspect-(<custom-property>)
aspect-ratio: var(<custom-property>);
aspect-[<value>]
aspect-ratio: <value>;

Ví dụ

Ví dụ cơ bản

Sử dụng các tiện ích aspect-<ratio> như aspect-3/2 để đưa cho một phần tử một tỷ lệ khung hình cụ thể:

Thay đổi kích thước ví dụ để thấy hành vi mong đợi

<img class="aspect-3/2 object-cover ..." src="/img/villas.jpg" />

Sử dụng tỷ lệ khung hình video

Sử dụng tiện ích aspect-video để đưa cho phần tử video tỷ lệ khung hình 16 / 9:

Thay đổi kích thước ví dụ để thấy hành vi mong đợi

<iframe class="aspect-video ..." src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>

Sử dụng giá trị tùy chỉnh

Use the aspect-[<value>] syntax to set the aspect ratio based on a completely custom value:

<img class="aspect-[calc(4*3+1)/3] ..." src="/img/villas.jpg" />

For CSS variables, you can also use the aspect-(<custom-property>) syntax:

<img class="aspect-(--my-aspect-ratio) ..." src="/img/villas.jpg" />

This is just a shorthand for aspect-[var(<custom-property>)] that adds the var() function for you automatically.

Responsive design

Prefix an aspect-ratio utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<iframe class="aspect-video md:aspect-square ..." src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>

Learn more about using variants in the variants documentation.

Tùy chỉnh theme của bạn

Use the --aspect-* theme variables to customize the aspect ratio utilities in your project:

@theme {  --aspect-retro: 4 / 3; }

Now the aspect-retro utility can be used in your markup:

<iframe class="aspect-retro" src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>

Learn more about customizing your theme in the theme documentation.

Copyright © 2025 Tailwind Labs Inc.·Chính sách thương hiệu