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.
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:
Where sides
is one of:
Where size
is one of:
Examples
<div class="p-4">
<div class="mt-3">
<div class="px-1">
More info at their page, under Utilities > Spacing: https://getbootstrap.com/docs/4.0/utilities/spacing/
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With