绝对定位的div在显示时分成两部分

时间:2015-11-22 11:18:59

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

我正在试验Bootstrap 4,并在卡片列中有一张卡片:

<div class="card-columns">
   <div class="card">
      <div class="card-block">
         <h4 class="card-title">iPhone 5S</h4>
         <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6>
      </div>
      <img data-src="holder.js/100%x200" alt="100%x200" data-holder-rendered="true" style="height: 200px; width: 100%; display: block;">
      <div class="card-block">
         <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan
            dengan harga khas dari Shopoza! Nikmati cicilan 0%
         </p>
         <div class="btn-group open">
            <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Add to cart</a>
            <div class="dropdown-menu" style="
               /* min-height: 100%; */
               /* overflow: auto; */
               /* max-height: 100%; */
               /* height: 100%; */
               ">
               <a class="dropdown-item" href="#">Action</a>
               <a class="dropdown-item" href="#">Another action</a>
            </div>
         </div>
         <a href="#" class="btn btn-secondary card-link">Quick checkout</a>
      </div>
      <!-- card block -->
   </div>
   <!-- card -->
</div>

卡片列本身位于容器内,行中有一行col-sm-12列。

这是显示的内容:

Button menu displayed into half

这里有什么问题? dropdown-menu div类具有绝对定位。

1 个答案:

答案 0 :(得分:1)

您正在使用示例中的.card-column布局,该布局将使用column CSS属性。 column-count布局中的默认.card-column似乎的值为3

即使.dropdown-menu的{​​{1}}值为position,也没有足够的空间让absolute显示在第一列,因为它不高够了。
这就是为什么它出现在下一栏的顶部。

解决此问题的一种简单方法是增加.dropdown-menu元素的margin-bottom,但如果您希望减少.card,则会导致列之间出现间隙在较小的屏幕尺寸上,当您向column-count添加更多项目时,您将不得不进一步增加列高,因此这不是一个好的解决方案。

.dropdown-menu
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css');

.card-columns {
  -webkit-column-count: 1;
  -moz-column-count: 1;
  column-count: 1;
  -webkit-column-gap: 0;
  -moz-column-gap: 0;
  column-gap: 0;
  text-align: center;
}

.card {
  margin-bottom: 3.75rem;
  max-width: 22.5rem; 
}

@media screen and (min-width: 34em) { 
  .card-columns {
    -webkit-column-count: 1;
    -moz-column-count: 1;
    column-count: 1;
    -webkit-column-gap: 0;
    -moz-column-gap: 0;
    column-gap: 0;
  }

  .card {
    max-width: 22.5rem;    
  }
}

@media screen and (min-width: 48em) { 
  .card-columns {
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
    -webkit-column-gap: .55rem;
    -moz-column-gap: .55rem;
    column-gap: .55rem;
    text-align: left;
  }
}

@media screen and (min-width: 62em) { 
  .card-columns {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;
  }
}

更好的解决方案是使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script> <div class="container"> <div class="row"> <div class="card-columns"> <div class="card"> <div class="card-block"> <h4 class="card-title">iPhone 5S</h4> <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6> </div> <img src="http://placehold.it/380x200" class="img-responsive"/> <div class="card-block"> <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan dengan harga khas dari Shopoza! Nikmati cicilan 0% </p> <div class="btn-group"> <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> <a href="#" class="btn btn-secondary card-link">Quick checkout</a> </div> <!-- card block --> <!-- card --> </div> <div class="card"> <div class="card-block"> <h4 class="card-title">iPhone 5S</h4> <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6> </div> <img src="http://placehold.it/380x200" class="img-responsive"/> <div class="card-block"> <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan dengan harga khas dari Shopoza! Nikmati cicilan 0% </p> <div class="btn-group"> <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> <a href="#" class="btn btn-secondary card-link">Quick checkout</a> </div> <!-- card block --> <!-- card --> </div> <div class="card"> <div class="card-block"> <h4 class="card-title">iPhone 5S</h4> <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6> </div> <img src="http://placehold.it/380x200" class="img-responsive"/> <div class="card-block"> <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan dengan harga khas dari Shopoza! Nikmati cicilan 0% </p> <div class="btn-group"> <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> <a href="#" class="btn btn-secondary card-link">Quick checkout</a> </div> <!-- card block --> <!-- card --> </div> </div> </div> </div>布局,.card-deck设置为display。这是一个例子。

flex
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css');

.card-deck {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
}

.card-deck .card {
  flex: 0 1 22.5rem;
  margin: 0 .25rem .50rem;
}