我正在尝试使用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类具有绝对位置。

最佳答案

您在示例中使用的是.card-column布局,它将使用column CSS属性。 column-count布局中的默认.card-column似乎具有3的值。

即使.dropdown-menuposition值为absolute,也没有足够的空间显示在第一栏中,因为它不够高。
这就是为什么它出现在下一列的顶部。

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



@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>





更好的解决方案是使用将.dropdown-menu设置为.card-deckdisplay布局。这是一个例子。



@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;
}

<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-deck">
      <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>

关于css - 显示时绝对定位的div分为两部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33854158/

10-16 20:56