Bootstrap: add margin/padding space between columns

前端 未结 14 2146
梦毁少年i
梦毁少年i 2020-12-01 01:45

I\'m trying to put some extra margin/padding space between columns on my Bootstrap grid layout. I\'ve tried this but I don\'t like the result. Here is my code:



        
相关标签:
14条回答
  • 2020-12-01 02:27

    Simply add a div within col-md-6 that has the extra padding that you need. The col-md-6 is the 'backbone' to keep the column integrity, but you can add additional padding within it.

    <div class="row">
        <div class="text-center col-md-6">
            <div class="classWithPad">Widget 1</div>
        </div>
        <div class="text-center col-md-6">
            <div class="classWithPad">Widget 2</div>
        </div>
    </div>
    

    CSS

    .classWithPad { margin:10px; padding:10px; }
    
    0 讨论(0)
  • 2020-12-01 02:30

    You may use the padding and margin shorthand Bootstrap 4 classes as follows:

    For extra small devices i.e. xs

    {property}{sides}-{size}
    

    For other devices/viewports (small, medium, large and extra large)

    {property}{sides}-{breakpoint}-{size}
    

    Where:

    property = m for margin and p for padding
    

    Following are sides shorthand meanings:

    l = defines the left-margin or left-padding
    r = defines the right-margin or right-padding
    t = defines the top-margin or top-padding
    b = defines the bottom-margin or right-padding
    x = For setting left and right padding and margins by the single call
    y = For setting top and bottom margins
    blank = margin and padding for all sides
    

    The breakpoint = sm, md, lg, and xl.

    Combining all the above, the left padding complete code can be (for example):

    For left padding in extra small devices

    pl-2

    or for medium to extra large

    pl-md-2

    0 讨论(0)
  • 2020-12-01 02:30

    A solution for someone like me when cells got background color

    HTML

    <div class="row">
            <div class="col-6 cssBox">
                a<br />ba<br />ba<br />b
            </div>
            <div class="col-6 cssBox">
                a<br />b
            </div>
    </div>
    

    CSS

    .cssBox {
      background-color: red;
      margin: 0 10px;
      flex-basis: calc(50% - 20px);
    }
    
    0 讨论(0)
  • 2020-12-01 02:31

    For those looking to control the space between a dynamic number of columns, try:

    <div class="row no-gutters">
        <div class="col">
            <div class="inner">
                <!-- content here -->
            </div>
        </div>
        <div class="col">
            <div class="inner">
                <!-- content here -->
            </div>
        </div>
        <!-- etc. -->
    </div>
    

    CSS:

    .col:not(:last-child) .inner {
      margin: 2px; // Or whatever you want your spacing to be
    }
    
    0 讨论(0)
  • 2020-12-01 02:32

    I was facing the same issue; and the following worked well for me. Hope this helps someone landing here:

    <div class="row">
      <div class="col-md-6">
        <div class="col-md-12">
         Set room heater temperature
       </div>
     </div>
     <div class="col-md-6">
       <div class="col-md-12">
         Set room heater temperature
       </div>
     </div>
    </div>
    

    This will automatically render some space between the 2 divs.

    0 讨论(0)
  • 2020-12-01 02:34

    Just add 'justify-content-around' class. that would automatically add gap between 2 divs.

    Documentation: https://getbootstrap.com/docs/4.1/layout/grid/#horizontal-alignment

    Sample:

    <div class="row justify-content-around">
        <div class="col-4">
          One of two columns
        </div>
        <div class="col-4">
          One of two columns
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题