Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put margin between div in bootstrap?

I want to put some 5px margin between box I created, and this margin should be constant, if I resize the browser window.

My html is like this:

<div class="container">
        <div class="row">
            <div class="col-md-3  col-sm-6 col-xs-12">
                <div style="height: 121px; background-color: orange; width:100%;">
                    <label>Box 1</label>
                </div>
            </div>
            <div class="col-md-2 col-sm-6 col-xs-12">
                <div style="height: 121px;background-color: wheat; width:100%;">
                    <label>Box 2</label>
                </div>
            </div>
            <div class="col-md-2 col-sm-6 col-xs-12">
                <div style="height: 121px;background-color: beige ;width:100%;">
                    <label>Box 3</label>
                </div>
            </div>
            <div class="col-md-2 col-sm-6 col-xs-12">
                <div style="height: 121px;background-color: chocolate; width:100%;">
                    <label>Box 4</label>
                </div>
            </div>
        </div>
                </div>

So in above html I used div with background colors. And I want to put some margin between them.

like image 801
patel Avatar asked Jul 29 '14 17:07

patel


2 Answers

Following the cannonical way to add margin and padding in Bootstrap, you can do this.

Classes format:

  • {property}{sides}-{size} for xs

  • {property}{sides}-{breakpoint}-{size} for sm, md, lg, and xl.

Where property is one of:

  • m - for classes that set margin
  • p - for classes that set padding

Where sides is one of:

  • t - for classes that set margin-top or padding-top
  • b - for classes that set margin-bottom or padding-bottom
  • l - for classes that set margin-left or padding-left
  • r - for classes that set margin-right or padding-right
  • x - for classes that set both *-left and *-right
  • y - for classes that set both *-top and *-bottom
  • blank - for classes that set a margin or padding on all 4 sides of the element

Where size is one of:

  • 0 - for classes that eliminate the margin or padding by setting it to 0
  • 1 - (by default) for classes that set the margin or padding to $spacer * .25
  • 2 - (by default) for classes that set the margin or padding to $spacer * .5
  • 3 - (by default) for classes that set the margin or padding to $spacer
  • 4 - (by default) for classes that set the margin or padding to $spacer * 1.5
  • 5 - (by default) for classes that set the margin or padding to $spacer * 3

Examples

  1. Padding * 1.5 in all sides: <div class="p-4">
  2. Margin default on top: <div class="mt-3">
  3. Padding * 0.25 on left and right: <div class="px-1">

More info at their page, under Utilities > Spacing: https://getbootstrap.com/docs/4.0/utilities/spacing/

like image 153
Natacha Avatar answered Sep 28 '22 06:09

Natacha


The problem with your code in bootstrap is that columns (<div class="col-*"></div>) have custom padding. There will always be equal width between your divs, but not always equal height (unless you start adding bootstrap rows which is handled the same way vertically and columns are horizontally with padding).

To solve this problem in your case you just need to add padding to your divs. DEMO

 <div class="container">
    <div class="row">
        <div class="col-md-2  col-sm-6 col-xs-12" style="padding-top: 5px;">
            <div style="background-color: orange;">
                <label>Box 1</label>
            </div>
        </div>
        <div class="col-md-2 col-sm-6 col-xs-12" style="padding-top: 5px;">
            <div style="background-color: wheat; ">
                <label>Box 2</label>
            </div>
        </div>
        <div class="col-md-2 col-sm-6 col-xs-12" style="padding-top: 5px;">
            <div style="background-color: beige; ">
                <label>Box 3</label>
            </div>
        </div>
        <div class="col-md-2 col-sm-6 col-xs-12" style="padding-top: 5px;">
            <div style="background-color: chocolate;">
                <label>Box 4</label>
            </div>
        </div>
    </div>
</div>
like image 23
Jordan.J.D Avatar answered Sep 28 '22 07:09

Jordan.J.D