具有bootstrap-4不同框高度的响应框网格

时间:2018-02-15 16:41:33

标签: html css html5 twitter-bootstrap bootstrap-4

我尝试使用box类和bootstrap制作一个包含6个文本框的响应网格。

我的问题是,盒子中的文字数量不同,它们的宽度/高度不同,但我希望它们的最大高度/最大宽度与文字中包含最多文字的框相同。 / p>

如果不对所有盒子的宽度/高度进行硬编码,是否有机会完成这项工作?

以下是代码段:

/***** Box *****/

.flex-box {
  position: relative;
  text-align: center;
  margin-top: 10px;
  margin-right: auto;
  margin-bottom: ;
  margin-left: auto;
  padding: 40px;
  transition: all 0.3s ease;
  background-color: rgb(170, 230, 255);
}

.flex-box:hover {
  margin-top: 5px;
  margin-bottom: 5px;
  -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
  -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
}
<!-- Textbox-Sections -->
<section id="about_me" class="section-padding title">
  <div class="container-fluid">
    <div class="container pt-4">
      <h1 class="title">About me</h1>
      <div class="row">
        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti fuga iusto aspernatur magni rem quas deserunt repellendus hic fugit amet tempora labore perferendis, voluptate possimus itaque cum molestias dolores. Omnis?</div>
            </p>
          </div>
        </div>
        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Esse voluptatum soluta cum provident explicabo fugiat dolores odit blanditiis dolorem magni. Quaerat temporibus doloremque beatae voluptatum dignissimos? Eligendi voluptatum similique
                incidunt!
              </div>
            </p>
          </div>
        </div>
        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Voluptate ipsam neque recusandae similique. Inventore repellendus aliquid delectus explicabo dolore sapiente voluptas nihil dolorem tempore voluptates, nemo ipsum cumque animi quidem.</div>
            </p>
          </div>
        </div>

        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod illo incidunt nihil praesentium minus itaque hic, soluta dicta cupiditate quos dolorum esse, harum placeat, qui veritatis animi sunt? Ab, nobis.</div>
            </p>
          </div>
        </div>
        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni, fuga vel. Maxime hic tempore quo animi ex vero doloribus dolores quos, sapiente delectus dolor et laborum amet quam aspernatur earum.</div>
            </p>
          </div>
        </div>
        <div class="col col-12 col-sm-6 col-lg-4">
          <div class="flex-box">
            <h1 class="boxtitle">My way</h1>
            <p class="lead">
              <div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dicta, repellendus distinctio ratione autem consequatur rerum libero nesciunt ullam eaque molestias odit architecto nisi incidunt quasi omnis ut. Natus, exercitationem alias.</div>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

Here's a link to the CodePen site

由于

3 个答案:

答案 0 :(得分:1)

只需将.flex-box高度:100%设为填充col-* ...

https://codepen.io/anon/pen/GQORQV?editors=1100

.flex-box {
  position: relative;
  text-align: center;
  margin-top: 10px;
  margin-right: auto;
  margin-bottom:;
  margin-left: auto;
  padding: 40px;
  height: 100%;
  transition: all 0.3s ease;
  background-color: rgb(170, 230, 255);
}

答案 1 :(得分:0)

正如@Abhijeet所说,只有盒子水平对齐才能使用css。否则,您可以使用此jQuery代码段:

var max_height = 0;
var max_width = 0;
$('.equal-size').each(function(e) {
  h = $(this).height();
  if(typeof(h) != "undefined") {
   if(h > max_height) {
     max_height = h;
   }
  }
  w = $(this).width();
  if(typeof(w) != "undefined") {
   if(w > max_width) {
     max_width = w;
   }
  }
});
if(max_height > 0) {
  $('.equal-size').height(max_height);
}
if(max_width > 0) {
  $('.equal-size').width(max_width);
}

您需要为元素equal-size提供。

正在寻找最大高度/宽度并将其应用于每个元素。

仅当您只有一组元素应具有相同的高度/宽度时才会起作用。如果您有更多内容,则需要提供组号并相应地更改类(equal-size-1equal-size-2,...)并使用此代码段:

var numberOfGroups = 2;
for(var i=1; i<numberOfGroups+1; i++) {
    var max_height = 0;
    var max_width = 0;
    $('.equal-size-'+String(i)).each(function(e) {
      h = $(this).height();
      if(typeof(h) != "undefined") {
       if(h > max_height) {
         max_height = h;
       }
      }
      w = $(this).width();
      if(typeof(w) != "undefined") {
       if(w > max_width) {
         max_width = w;
       }
      }
    });
    if(max_height > 0) {
      $('.equal-size-'+String(i)).height(max_height);
    }
    if(max_width > 0) {
      $('.equal-size-'+String(i)).width(max_width);
    }
}

答案 2 :(得分:0)

我认为只有css是可能的,但是如果你让这些盒子垂直,我的意思是彼此一个然后绝对可能但不是水平的。如果部分彼此对齐,例如 - enter image description here

,则网格中的高度可能相同