Make two column responsive images of same size

蹲街弑〆低调 提交于 2019-12-07 08:28:26
Frank Spin

To gain more control over your images it's best to switch to background images. You can then use background-size: cover to fill out the div.

HTML

<div class="col-lg-6">
  <div class="image-holder" style="background-image: url('assets/img/101.jpg');">
    &nbsp;
  </div>
</div>
<div class="col-lg-6">
  <div class="image-holder" style="background-image: url('assets/img/101.jpg');">
    &nbsp;
  </div>
</div>

CSS

.image-holder {
  background-size: cover;
  background-position: center center;
  height: 400px;
  width: 100%;
}

If your images are just static assets it's better to include the image reference in css.

You can't set both height and width at the same time for an image- it will stretch them out. Have made it a 2-column flexbox along with col-lg-6 as per requirement.

See demo below:

.box {
  border: 1px solid #ddd;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 1 50%;
  min-width: 200px; 
}
.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}

.col-lg-6.box {
   padding: 15px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css" />

<div class="container">
  <div class=" col-lg-6 box">
    <img src="http://placehold.it/150x100" alt="Null" />
  </div>
  <div class=" col-lg-6 box">
    <img src="http://placehold.it/75x75" alt="Null" />
  </div>
  <div class=" col-lg-6 box">
    <img src="http://placehold.it/150x50" alt="Null" />
  </div>
  <div class=" col-lg-6 box">
    <img src="http://placehold.it/150x150" alt="Null" />
  </div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!