object-fit - CSS: Cascading Style Sheets 编辑

The 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 property.

Syntax

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

Values

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.

Formal definition

Initial valuefill
Applies toreplaced elements
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

fill | contain | cover | none | scale-down

Examples

Setting object-fit for an image

HTML

<section>
  <h2>object-fit: fill</h2>
  <img class="fill" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <img class="fill narrow" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <h2>object-fit: contain</h2>
  <img class="contain" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <img class="contain narrow" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <h2>object-fit: cover</h2>
  <img class="cover" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <img class="cover narrow" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <h2>object-fit: none</h2>
  <img class="none" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <img class="none narrow" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <h2>object-fit: scale-down</h2>
  <img class="scale-down" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn_logo_only_color.png" alt="MDN Logo">

  <img class="scale-down narrow" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/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;
  margin: 10px 0;
}

.narrow {
  width: 100px;
  height: 150px;
}

.fill {
  object-fit: fill;
}

.contain {
  object-fit: contain;
}

.cover {
  object-fit: cover;
}

.none {
  object-fit: none;
}

.scale-down {
  object-fit: scale-down;
}

Result

Specifications

SpecificationStatusComment
CSS Images Module Level 4
The definition of 'object-fit' in that specification.
Working Draft
CSS Images Module Level 3
The definition of 'object-fit' in that specification.
Candidate RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:63 次

字数:8889

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文