object-fit CSS property sets how the content of a replaced element , such as an <img> or <video> , should be resized to fit its container.

You can alter the alignment of the replaced element's content object within the element's box using the object-position 特性。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

object-fit property is specified as a single keyword chosen from the list of values below.

contain
The replaced content is scaled to maintain its aspect ratio while fitting within the element’s content box. The entire object is made to fill the box, while preserving its aspect ratio, so the object will be "letterboxed" if its aspect ratio does not match the aspect ratio of the box.
cover

The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.

fill

The replaced content is sized to fill the element’s content box. The entire object will completely fill the box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be stretched to fit.

none

The replaced content is not resized.

scale-down
The content is sized as if none or contain were specified, whichever would result in a smaller concrete object size.

形式定义

初始值 fill
适用于 replaced elements
继承 no
计算值 如指定
动画类型 discrete

形式句法

fill | contain | cover | none | scale-down
					

范例

Setting object-fit for an image

HTML

<section>
  <h2>object-fit: fill</h2>
  <img class="fill" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <img class="fill narrow" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <h2>object-fit: contain</h2>
  <img class="contain" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <img class="contain narrow" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <h2>object-fit: cover</h2>
  <img class="cover" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <img class="cover narrow" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <h2>object-fit: none</h2>
  <img class="none" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <img class="none narrow" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <h2>object-fit: scale-down</h2>
  <img class="scale-down" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
  <img class="scale-down narrow" src="https://yari-demos.prod.mdn.mozit.cloud/files/6457/mdn_logo_only_color.png" alt="MDN Logo">
</section>
					

CSS

h2 {
  font-family: Courier New, monospace;
  font-size: 1em;
  margin: 1em 0 0.3em;
}
div {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: flex-start;
  height: 940px;
}
img {
  width: 150px;
  height: 100px;
  border: 1px solid #000;
}
.narrow {
  width: 100px;
  height: 150px;
  margin-top: 10px;
}
.fill {
  object-fit: fill;
}
.contain {
  object-fit: contain;
}
.cover {
  object-fit: cover;
}
.none {
  object-fit: none;
}
.scale-down {
  object-fit: scale-down;
}
					

结果

规范

规范 状态 注释
CSS Images Module Level 4
The definition of 'object-fit' in that specification.
工作草案
CSS Images Module Level 3
The definition of 'object-fit' in that specification.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
object-fit Chrome 完整支持 31 Edge 完整支持 16 注意事项
完整支持 16 注意事项
注意事项 Edge supports object-fit on img elements only. See Edge issue 13603873 for details .
Firefox 完整支持 36 IE 不支持 No Opera 完整支持 19
完整支持 19
完整支持 11.6 Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari 完整支持 10 WebView Android 完整支持 4.4.3 Chrome Android 完整支持 31 Firefox Android 完整支持 36 Opera Android 完整支持 19
完整支持 19
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari iOS 完整支持 10 Samsung Internet Android 完整支持 2.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Images
  4. 指南
    1. Implementing image sprites in CSS
    2. Using CSS gradients
  5. 特性
    1. <gradient>
    2. <image>
    3. image-orientation
    4. image-rendering
    5. object-fit
    6. object-position

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1