1. 布局
  2. 纵横比

Quick reference

Class
Properties
aspect-autoaspect-ratio: auto;
aspect-squareaspect-ratio: 1 / 1;
aspect-videoaspect-ratio: 16 / 9;

基本用法

¥Basic usage

设置纵横比

¥Setting the aspect ratio

使用 aspect-* 工具设置元素的所需纵横比。

¥Use the aspect-* utilities to set the desired aspect ratio of an element.

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

Tailwind 不包括开箱即用的大量纵横比值,因为使用任意值更容易。有关详细信息,请参阅 任意值 部分。

¥Tailwind doesn’t include a large set of aspect ratio values out of the box since it’s easier to just use arbitrary values. See the arbitrary values section for more information.

浏览器支持

¥Browser support

aspect-* 工具使用原生 aspect-ratio CSS 属性,Safari 直到版本 15 才支持该属性。在 Safari 15 普及之前,Tailwind 的 aspect-ratio 插件是一个不错的选择。

¥The aspect-* utilities use the native aspect-ratio CSS property, which was not supported in Safari until version 15. Until Safari 15 is popularized, Tailwind’s aspect-ratio plugin is a good alternative.


有条件地应用

悬停、聚焦和其他状态

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:aspect-square to only apply the aspect-square utility on hover.

<iframe class="w-full aspect-video hover:aspect-square" src="https://www.youtube.com/..."></iframe>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

断点和媒体查询

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:aspect-square to apply the aspect-square utility at only medium screen sizes and above.

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

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 提供了一组最小的 aspect-ratio 工具。你可以通过编辑 tailwind.config.js 文件中的 theme.aspectRatiotheme.extend.aspectRatio 来自定义这些值。

¥By default, Tailwind provides a minimal set of aspect-ratio utilities. You can customize these values by editing theme.aspectRatio or theme.extend.aspectRatio in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      aspectRatio: {
        '4/3': '4 / 3',
      },
    }
  }
}

主题定制 文档中了解有关自定义默认主题的更多信息。

¥Learn more about customizing the default theme in the theme customization documentation.

任意值

¥Arbitrary values

If you need to use a one-off aspect-ratio value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<iframe class="w-full aspect-[4/3]" src="https://www.youtube.com/..."></iframe>

Learn more about arbitrary value support in the arbitrary values documentation.