Jack The Baker
Jack The Baker

Reputation: 1883

Bootstrap 4 add margin to col

I want to add some margin to col with mr-md-3 but it cause break to that div

.content {
  height: 200px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container">
  <div class="row no-gutters">
    <div class="col-md-4 col-12 content mr-md-3 mb-3"></div>
    <div class="col-md-8 col-12 content mb-3"></div>
  </div>
</div>

<footer>
<div class="container">
  <div class="row no-gutters">
    <div class="col-md-4 col-12 content mb-3"></div>
    <div class="col-md-8 col-12 content mb-3"></div>
  </div>
</div>
</footer>

I need to add some margin right to element without break and want to fit to global container, how can I do this?

Note: see example in large resolution.

Upvotes: 2

Views: 336

Answers (2)

Carol Skelly
Carol Skelly

Reputation: 362340

Since Bootstrap column gutters use padding, place the content inside the columns and adjust the padding.

<div class="container">
    <div class="row no-gutters">
        <div class="col-md-4 col-12 pr-md-3 mb-3">
            <div class="content"></div>
        </div>
        <div class="col-md-8 col-12 mb-3">
            <div class="content"></div>
        </div>
    </div>
</div>

https://www.codeply.com/go/3wsSnnzCsp

Upvotes: 2

Serg Chernata
Serg Chernata

Reputation: 12400

All you have to do is give flex: 1; to the neighboring div.

Now you can make the margin even bigger and the problem column will shrink accordingly.

.container .problem{flex: 1;}

.content {
  height: 200px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container">
  <div class="row no-gutters">
    <div class="col-md-4 col-12 content mr-md-3 mb-3"></div>
    <div class="col-md-8 col-12 content problem mb-3"></div>
  </div>
</div>

<footer>
<div class="container">
  <div class="row no-gutters">
    <div class="col-md-4 col-12 content mb-3"></div>
    <div class="col-md-8 col-12 content mb-3"></div>
  </div>
</div>
</footer>

Upvotes: 3

Related Questions