基于比例的流体响应图像网格

时间:2019-01-21 14:31:48

标签: javascript html css flexbox bootstrap-4

基本上,我基于Bootstrap 4+版本开发了此图像网格。添加多个media query后,它对于某些特定的设备和布局看起来不错。

参考代码

.cmd-three-img-container {
  position: relative;
  margin-bottom: 30px;
  height: 320px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

@media (max-width: 768px) {
  .cmd-three-img-container {
    height: 200px;
  }
}

.cmd-main-img {
  width: 50%;
  height: 100%;
  margin-left: 5px;
  cursor: pointer;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.cmd-main-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.cmd-img-stacked {
  width: 50%;
  height: 100%;
  cursor: pointer;
}

.cmd-img-overlay {
  position: absolute;
  top: 10px;
  right: 10px;
}

.cmd-img-overlay h4 {
  float: right;
  font-size: 22px;
  font-weight: 400;
  color: #fff;
  font-family: "Rubik", sans-serif;
}

.cmd-top-img,
.cmd-bottom-img {
  width: 100%;
  height: calc(50% - 2.5px);
  cursor: pointer;
}

.cmd-top-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-position: center;
  border-top-left-radius: 4px;
}

.cmd-bottom-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-position: center;
  border-bottom-left-radius: 4px;
}

.cmd-top-img {
  margin-bottom: 5px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />


<div class="cmd-three-imggrid-area">

  <div class="container-fluid px-5-percent">
    <div class="row">

      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>

所以我想保持与移动设备相同的在台式机中显示的三个图像比例,ipad也一样!基于设备布局的两个或三个图像网格无关紧要,但是可以减小包含图像宽度和高度的比例以及流体的总网格大小。

预先感谢

1 个答案:

答案 0 :(得分:1)

问题在于您在.cmd-three-img-container上设置的静态高度。因此,响应式将不起作用。我删除了它,然后将height中的.cmd-main-img更改为min-height: 100%。我更新了您的代码

.cmd-three-img-container {
  position: relative;
  margin-bottom: 30px;      
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}


.cmd-main-img {
  width: 50%;
  min-height: 100%;
  margin-left: 5px;
  cursor: pointer;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.cmd-main-img img {
  width: 100%;    
  height: 100%;
  object-fit: cover;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.cmd-img-stacked {
  width: 50%;
  height: 100%;
  cursor: pointer;
}

.cmd-img-overlay {
  position: absolute;
  top: 10px;
  right: 10px;
}

.cmd-img-overlay h4 {
  float: right;
  font-size: 22px;
  font-weight: 400;
  color: #fff;
  font-family: "Rubik", sans-serif;
}

.cmd-top-img,
.cmd-bottom-img {
  width: 100%;
  height: calc(50% - 2.5px);
  cursor: pointer;
}

.cmd-top-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-position: center;
  border-top-left-radius: 4px;
}

.cmd-bottom-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-position: center;
  border-bottom-left-radius: 4px;
}

.cmd-top-img {
  margin-bottom: 5px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />


<div class="cmd-three-imggrid-area">

  <div class="container-fluid px-5-percent">
    <div class="row">

      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
      <div class="col-sm-3">
        <div class="cmd-three-img-container">
          <!-- top level image container START-->
          <div class="cmd-img-stacked">
            <!--stacked img container -->
            <div class="cmd-top-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
            <div class="cmd-bottom-img">
              <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
            </div>
          </div>
          <div class="cmd-main-img">
            <!--Main image -->
            <img src="https://i.imgur.com/H4bbqpA.jpg" alt="">
          </div>
          <div class="cmd-img-overlay">
            <h4>Work</h4>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>