Vertical alignment of column rows in Bootstrap grid

前端 未结 2 749
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 07:20

Suppose you have a two-column layout using Twitter Bootstrap, in which you want to have specific rows vertically aligned with each other:

相关标签:
2条回答
  • 2021-01-28 07:54

    You can do something like this to get rows of equal heights:

    <style>
     .centered {
       text-align: center;
       font-size: 0;
      }
    
     .centered .myheight{
       float: none;
       display: inline-block;
       text-align: left;
       font-size: 13px;
       vertical-align: top;
       margin-bottom: 5px; /*add this if you want to give some gap between the rows. */
      }
    </style>
    
    
    <div class="container-fluid">
     <div class="row centered">
        <div class="col-sm-4 myheight">
          Some  content
        </div> 
        <div class="col-sm-4 myheight">
          Some  content ...<br>
          Some more content
        </div> 
        <div class="col-sm-4 myheight">
          Some  content
        </div>  
        <div class="col-sm-4 myheight">
          Some  content
        </div>                                                              
     </div>
    </div>
    
    0 讨论(0)
  • 2021-01-28 07:56

    For what I know, I would test these solutions.

    Solution 1 : Using Javascript (Jquery if you want) to detect the height of the left and the right div, and to tell them to have the same height.

    You can apply this solution on the second content div to make the espace above your bold text having the same height.

    Or to add.. for example as margin-top as needed in the smaller div (with bold text which need to be aligned) after comparing the heights of both.

    Anyway, if you have both of theirs heights you will have enought informations to find a way. Multiples solutions are possible here, I let you find the better one for your context and needs.

    <div class="container">
      <div class="row">
        <div class="col-sm-6 leftcolumn">
          <h2>Column 1</h2>
          <div class="astallas"><p>Optional content of variable height.</p></div>
          <p><strong>Align this vertically...</strong></p>
        </div>
        <div class="col-sm-6 rightcolumn">
          <h2>Column 2</h2>
          <div class="astallas"></div> // make this empty div have the same height that the left one with variable content
          <p><strong>...with this</strong></p>
        </div>
      </div>
    </div>
    

    Solution 2 (but with some browsers incompatibilties) : Use Flexbox <3 which is a native CSS3 fonctionnaly that give you a easy way to have your wanted divs' positions. http://flexboxfroggy.com/ https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    I think that both of these works with bootstrap and will respect responsive needs.

    0 讨论(0)
提交回复
热议问题