aspect-ratio
CSS 属性为box容器规定了一个期待的纵横比,这个纵横比可以用来计算自动尺寸以及为其他布局函数服务。
aspect-ratio: 1 / 1;
/* 全局值 */
aspect-ratio: inherit;
aspect-ratio: initial;
aspect-ratio: unset;
数值
正式定义
初始值 | auto |
---|---|
适用元素 | all elements except inline boxes and internal ruby or table boxes |
是否是继承属性 | 否 |
计算值 | as specified |
Animation type | discrete |
正式语法
auto | <ratio>
示例
纵横比数值例子
aspect-ratio: 1 / 1;
aspect-ratio: 16 / 9;
将宽度和高度关联到宽高比
浏览器内部已经为替换的元素和其他接受 width
和 height
的元素添加了 aspect-ratio
。这些可以在浏览器的UA样式表中看到。
在 Firefox 中,内部样式表看起来如下:
img, input[type="image"], video, embed, iframe, marquee, object, table {
aspect-ratio: attr(width) / attr(height);
}
您可以在 为图片设置宽高仍然重要gain 一文中了解更多与这个功能有关的内容。
规范
规范 | 状态 | 注释 |
---|---|---|
CSS Box Sizing Module Level 4 aspect-ratio |
Editor's Draft | 初始定义 |
浏览器兼容性
BCD tables only load in the browser
此页面上的兼容性表格是根据结构化数据生成的。如果您想贡献数据,请查阅 https://github.com/mdn/browser-compat-data 并向我们发送合并申请。