waleedd32
waleedd32

Reputation: 533

padding/margin between bootstrap boxes not working

I'm improving my bootstrap skills, i have done these boxes according to that picture, but need to put white spaces between boxes like on the picture you can see, i have tried padding and margin but its not affecting every box somehow, any idea, how to put spaces between boxes? enter image description here

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
 <link rel="stylesheet" href="css/bootstrap.css">

  <style>

  </style>

</head>

<body>
  <div class="row grid " >
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-3 " style="background-color:red; height:200px;"></div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 " style="background-color:blue; height:200px;"></div>
	

  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-6 " style="background-color:yellow; height:200px;"> </div>
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-3 " style="background-color:burlywood; height:200px;"></div>
  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-0 " style="background-color:black; height:200px;"></div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-6 " style="background-color:orange; height:200px;"></div>
  </div>

  <script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>

Upvotes: 3

Views: 861

Answers (1)

Nandita Sharma
Nandita Sharma

Reputation: 13417

Add another div in the col-* 's div and add the height and color on it instead of the col-* divs. Also add p-1 class to all the col divs.

One sample is as shown below

    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-3 p-1" >
         <div style="background-color:red; height:200px;"></div>
    </div>

Also one more thing, as your content is having row elements it must be wrapped in a container-fluid div. As you have not added it, a horizontal scroll bar can be seen. I have fixed that too in the answer.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
 <link rel="stylesheet" href="css/bootstrap.css">

  <style>

  </style>

</head>

<body>
<div class="container-fluid">
  <div class="row grid " >
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-3 p-1" ><div style="background-color:red; height:200px;"></div></div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 p-1" ><div style="background-color:blue; height:200px;"></div></div>
	

  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-6 p-1"> <div style="background-color:yellow;height:200px; "></div></div>
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-3 p-1 " ><div style="height:200px;background-color:burlywood; "></div></div>
  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-0 p-1" ><div style="background-color:black;height:200px; "></div></div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-6 p-1"><div style="background-color:orange; height:200px;"></div></div>
  </div>
</div>
  <script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>

Upvotes: 1

Related Questions