bootstrap box之间的填充/边距不起作用

时间:2022-06-17 22:26:44

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? bootstrap box之间的填充/边距不起作用

我正在提高我的引导技能,我根据那张图片完成了这些盒子,但是需要在盒子之间放置白色空格,就像你可以看到的图片一样,我尝试过填充和边距,但它不会影响每个盒子,不知何故,任何想法,如何在盒子之间放置空格?

<!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>

1 个解决方案

#1


1  

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.

在col- *的div中添加另一个div并在其上添加高度和颜色而不是col- * div。还要为所有col div添加p-1类。

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.

还有一件事,因为你的内容有行元素,它必须包装在容器流体div中。由于您尚未添加,因此可以看到水平滚动条。我已经解决了这个问题。

<!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>

#1


1  

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.

在col- *的div中添加另一个div并在其上添加高度和颜色而不是col- * div。还要为所有col div添加p-1类。

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.

还有一件事,因为你的内容有行元素,它必须包装在容器流体div中。由于您尚未添加,因此可以看到水平滚动条。我已经解决了这个问题。

<!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>