html object-fit属性定义元素如何响应其内容框的高度和宽度。它适用于图像,视频和其他内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html object-fit属性定义元素如何响应其内容框的高度和宽度。它适用于图像,视频和其他内容相关的知识,希望对你有一定的参考价值。

.object-fit_fill { object-fit: fill }
.object-fit_contain { object-fit: contain }
.object-fit_cover { object-fit: cover }
.object-fit_none { object-fit: none }
.object-fit_scale-down { object-fit: scale-down }

html { 
  color: #eee; 
  padding: 30px;
  font-family: 'Source Code Pro', Monaco;
  background-color: #333;
}

p { font-weight: 200; font-size: 13px; margin-bottom: 10px; margin-top: 0;}

img { height: 120px; background-color: #444;}

img[class] { 
  width: 100%;
}

.original-image {
  margin-bottom: 50px;
}

.image {
  float: left;
  width: 40%;
  margin: 0 30px 20px 0;
  
  &:nth-child(2n) {
    clear: left;
  }
  
  &:nth-child(2n+1){
    margin-right: 0;
  }
}
<div class="original-image">
<p>original image</p>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png"> 
</div>

<div class="image">
  <p>object-fit: fill</p>
<img class="object-fit_fill" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png">
</div>

<div class="image">
  <p>object-fit: contain</p>
<img class="object-fit_contain" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png">
</div>

<div class="image">
  <p>object-fit: cover</p>
<img class="object-fit_cover" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png">
</div>

<div class="image">
  <p>object-fit: none</p>
<img class="object-fit_none" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png">
</div>

<div class="image">
  <p>object-fit: scale-down</p>
<img class="object-fit_scale-down" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14179/image.png">
 </div>

以上是关于html object-fit属性定义元素如何响应其内容框的高度和宽度。它适用于图像,视频和其他内容的主要内容,如果未能解决你的问题,请参考以下文章

理解 CSS3中 object-fit

object-fit,object-position 和 background-size 和 background-position

Html5的video标签自动填充满父div的大小

video自动填充满父级元素

HTML img和video object-fit 属性

HTML img和video object-fit 属性