目录

style objectFit 属性

示例

剪掉图片的侧面,保留纵横比,并填充空间:

document.getElementById("myImg").style.objectFit = "cover";
亲自试一试 »

描述

objectFit 属性用于指定如何调整 <img> 或 <video> 的大小以适合其容器。

该属性告诉内容以多种方式填充容器;例如"preserve that aspect ratio" 或"stretch up and take up as much space as possible"


浏览器支持

表中的数字指定完全支持该属性的第一个浏览器版本。

Property
objectFit 31.0 16.0 36.0 7.1 19.0

语法

返回 objectFit 属性:

object.style.objectFit

设置 objectFit 属性:

object.style.objectFit = "fill|contain|cover|scale-down|none|initial|inherit"

属性值

Value Description
fill This is default. The replaced content is sized to fill the element's content box. If necessary, the object will be stretched or squished to fit
contain The replaced content is scaled to maintain its aspect ratio while fitting within the element's content box
cover The replaced content is sized to maintain its aspect ratio while filling the element's entire content box. The object will be clipped to fit
none The replaced content is not resized
scale-down The content is sized as if none or contain were specified (would result in a smaller concrete object size)
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

技术细节

默认值: 充满
返回值: 一个字符串,表示元素的对象适合度
CSS版本 CSS3

相关页面

CSS教程:CSS 对象适配

CSS 参考:对象适合性