CSS 属性 aspect-ratio 为盒子规定了首选纵横比,这个纵横比可以用于计算 auto 尺寸以及其他布局函数。

{{InteractiveExample("CSS Demo: aspect-ratio")}}

```css interactive-example-choice aspect-ratio: auto;


```css interactive-example-choice aspect-ratio: 1 / 1;

```css interactive-example-choice aspect-ratio: 16 / 9;


```css interactive-example-choice aspect-ratio: 0.5;

```html interactive-example


```css interactive-example #example-element { height: 100%; width: auto; }
aspect-ratio: 1 / 1;
aspect-ratio: 1;

/* 全局值 */
aspect-ratio: inherit;
aspect-ratio: initial;
aspect-ratio: revert;
aspect-ratio: revert-layer;
aspect-ratio: unset;

取值

  • auto
    • : 具有固有纵横比的{{glossary("Replaced elements", "可替换元素")}}将使用此纵横比,否则盒子无首选纵横比。涉及固有纵横比的尺寸计算始终使用内容盒的尺寸。
  • {{cssxref("<ratio>")}}
    • : 盒子的首选纵横比为指定的 width / height 比率。如果省略 height 和前面的斜杠字符,则 height 默认为 1。涉及首选纵横比的尺寸计算使用由 box-sizing 所指定的盒子的尺寸。

形式定义

{{cssinfo}}

形式语法

{{csssyntax}}

示例

aspect-ratio 的取值示例

aspect-ratio: 1 / 1;
aspect-ratio: 16 / 9;
aspect-ratio: 0.5;