Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 how to add margin to element inside a col?

I have some span elements inside a col:

<div class="container-fluid">
  <div class="row">
    <div class="col">
        <span class="myClass">some text</span>
        <span class="myClass">some text</span>
    </div>
    <div class="col"></div>
  </div>
</div>

myClass elements have some padding:

.myClass{
  background: #ecedea;
  padding: 10px 10px;
  margin-right: 20px;
}

Because there is multiple span elements in the first col, a new line is being created. My problem is because of the padding I have some overlay between my spans in the first line and my spans in the second line:

enter image description here

I tried to add some margin-top to myClass but it didn't help

like image 317
woshitom Avatar asked Feb 09 '18 15:02

woshitom


People also ask

How do you add margins in Col?

You can use calculated width/margins., no change to your HTML necessary. E.g. the width of col-md-3 is 100/4=25%. Therefore you can reduce this, say to 20%, and allocate the remaing 5% to your margins. Perfect!

How do I set margins in Bootstrap 4?

r - sets margin-right or padding-right. x - sets both padding-left and padding-right or margin-left and margin-right. y - sets both padding-top and padding-bottom or margin-top and margin-bottom. blank - sets a margin or padding on all 4 sides of the element.

How do you put a space between Col in Bootstrap?

To add space between columns in Bootstrap use gutter classes. With gutters you can add horizontal or vertical space or even specify how big space should be on different screen size.

How do you add padding to a column?

You can adjust margins on the column contents using the margin utils such as ml-0 (margin-left:0), mr-0 (margin-right:0), mx-1 (. 25rem left & right margins), etc... Or, you can adjust padding on the columns (col-*) using the padding utils such as pl-0 (padding-left:0), pr-0 (padding-right:0), px-2 (.


1 Answers

span doesn't have margin because it's an inline element.

You can fix the CSS like this:

.myClass{
  background: #ecedea;
  padding: 10px 10px;
  display: inline-block;
  margin-right: 20px;
}

OR, you can use Bootstrap 4 utility classes and avoid the extra CSS:

<div class="container-fluid">
  <div class="row">
    <div class="col">
        <span class="bg-light d-inline-block p-2 mr-3">some text</span>
        <span class="bg-light d-inline-block p-2 mr-3">some text</span>
    </div>
  </div>
</div>

Demo: https://www.codeply.com/go/jKCFqBb92J

like image 117
Zim Avatar answered Sep 24 '22 17:09

Zim