为什么padding-top + object-fit在图像上不起作用?

时间:2019-03-04 16:55:28

标签: html css twitter-bootstrap bootstrap-4

我想使图像具有响应性。 因此,我们决定通过指定padding-top: 100%作为图像的父元素的伪元素来绝对排列图像。我们认为,如果将object-fit: cover应用于图像,则图像将是正方形。

但是,实际上,宽度和高度不足的图像不会被放大,并且看来object-fit无法正常工作。

如何使用CSS创建响应正方形并应用object-fit

.theme-color-text {
  position: relative;
}

.theme-color-text::before {
  margin-top: 10px;
  display: block;
  content: "";
  padding-top: 100%;
}

.image {
  width: 100%;
  border-radius: 10px;
  margin-top: 10px;
  object-fit: cover; /* If the image is small, it will not be enlarged to a square. */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/600/500/animals" class="image">
          <!-- correct -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/150/50/animals" class="image">
          <!-- incorrect -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/50/25/animals" class="image">
          <!-- incorrect -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:5)

根据CSS Image Values and Replaced Content Module Level 3 object-fit属性需要高度和宽度

  

5.5. Sizing Objects: the object-fit property

     

object-fit属性指定如何将替换元素的内容装配到其使用的高度和宽度建立的框中。

在这种情况下,您的代码缺少height属性的规范。请尝试此代码。

.theme-color-text {
  position: relative;
}

.theme-color-text::before {
  margin-top: 10px;
  display: block;
  content: "";
  padding-top: 100%;
}

.image {
  width: 100%;
  border-radius: 10px;
  margin-top: 10px;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  /* Add the specification of the `height` property. */
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/600/500/animals" class="image">
          <!-- correct -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/150/50/animals" class="image">
          <!-- incorrect -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
    <div class="col-6 col-md-4 col-lg-3">
      <div class="row post">
        <a href="#" class="theme-color-text w-100">
          <img src="https://placeimg.com/50/25/animals" class="image">
          <!-- incorrect -->
        </a>
      </div>
      <div class="row post-detail">
        <div class="col-auto">
          <i class="fas fa-heart"></i>
        </div>
        <div class="col-auto">
          <i class="fas fa-eye"></i>
        </div>
        <div class="col-12 comment-area">
          text
        </div>
      </div>
    </div>
  </div>
</div>