Bootstrap 如何在引导卡中根据宽度和高度拟合图像?

oxf4rvwz  于 5个月前  发布在  Bootstrap
关注(0)|答案(3)|浏览(64)

我想给予图像的宽度和高度(300像素; 300像素;)在卡:

<!-- Card Start From Here -->
  <div class="card" style="width: 35rem;">
   <img class="card-img-top" src="/demo/img/1.jpeg" alt="Card image cap" style="width:35rem;height:20rem;"/>
   <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary">Go somewhere</a>
  </div>
  </div>
  <!-- Card Start From Here -->

字符串
产量缩水:
x1c 0d1x的数据
如果我删除下面的代码行,那么图像将很长,但不会缩小:

style="width:35rem;height:20rem;"


我试过:

object-fit:cover;(Check all properties like fill, scale-down)


我也试过CSS:

<style>
 .clsAutofit{
   height:300px;
   width:300px;
  }
 .clsImg{
  max-width:100%;
  max-height:100%;
  }
 </style>

 <div class="clsAutofit">
   <img src="/demo/img/1.jpeg" class="clsImg" />
 </div> 
 <!-- This also not working in card -->


我也尝试了一些属性:

<style>
 img.one {
 display: block;
 object-fit: contain;
 max-width:500px;
 max-height:280px;
 width: auto;
 height: auto;
 }

 img.two {
  object-fit: contain;
  width: 535px;;
  height: 535px;
  }
 .box {
   width: 30%;
   height: 200px;
   border: 5px dashed #f7a239;
 }
 .containerTY {
    width: 60%;
    height: 500px;
 }
 .containerTYZ {
    max-width: 100%;
 }
 .thumbnail {
   width: 500px;
   height: 400px;
  }
 .img4R {
    width: 100%;
    height: auto;
    max-width: 50vw;
  }
 </style>


我试过了,但还没能做到。欢迎任何想法或建议。

lvmkulzt

lvmkulzt1#

你的方法是好的.问题是,引导类属性往往有!important .
所以把你自定义的style.css放在你的bootstrap.css下面,这样它就比bs有更高的优先级,
则用途:

.my-image{
  height : 300px !important;
  width: 300px !important
}

字符串
.my-image类绑定图片,清除inline css styles,检查这个。

y53ybaqx

y53ybaqx2#

.clsImg{
  max-width:100%;

  width: 100%;
  height: auto;
  object-fit:cover;
  }

字符串

qaxu7uf2

qaxu7uf23#

使用img-fluid的图像,你也可以使用w-25 w-50 w-100lgmdsm等设备

@media (min-width: 768px) {
  .w-md-50{width:50% !important;}
}
@media (min-width: 992px) {
  .w-lg-75{width:75% !important;}
}

个字符

相关问题