align-items-center while keeping the background fu

2019-08-30 21:34发布

问题:

I have two columns inside a row in Bootstrap 4. If I try to align vertically the content using align-items-center on the row, or align-self-center on the col, the h4 is centered but the background is not full height. Here is a pen https://codepen.io/geo555/pen/PyaLbE

<div class="container">
  <div class="row no-gutters justify-content-center">
    <div class="col-sm-4">
       <img src="https://via.placeholder.com/400X400" class="img-fluid">
    </div>
    <div class="col-sm-4 text-center bg-dark text-white align-self-center">
       <h4>Awesome desk</h4>
    </div>
   </div>
</div>

回答1:

Make the col d-flex flex-column and use vertical auto margin (my-auto) on the heading...

<div class="container">
    <div class="row no-gutters justify-content-center">
        <div class="col-sm-4">
            <img src="https://via.placeholder.com/400X400" class="img-fluid">
        </div>
        <div class="col-sm-4 border text-center bg-dark text-white d-flex flex-column">
            <h4 class="my-auto">Awesome desk</h4>
        </div>
    </div>
</div>

https://www.codeply.com/go/7nPpNbXHF4