简体   繁体   English

如何在Bootstrap中的内容框之间添加空格

[英]How to add space between content boxes in Bootstrap

Right now I have a row of three boxes in Bootstrap as follows: 现在我在Bootstrap中有一排三个框,如下所示:

<div class="row">
<div class="col-lg-4 pathBoxMain">
<h2>Content</h2>
</div>

<div class="col-lg-4 pathBoxMain">
<h2>Content</h2>
</div>

<div class="col-lg-4 pathBoxMain">
<h2>Content</h2>
</div>
</div>

where my custom CSS changes the size of the content boxes and adds a border as follows: 我的自定义CSS更改内容框的大小并添加边框,如下所示:

.pathBoxMain {
border: 5px solid black;
padding: 10px 0px 0px 0px;
height: 175px;
}

All of the boxes appear right next to each other with the borders touching, and I am trying to add some space between the boxes so that does not happen. 所有的框都显示在彼此相邻的边框接触,我试图在框之间添加一些空间,这样就不会发生。 Everything I have tried (adjusting the width of the boxes, changing the margins, adding empty columns between the existing ones) all change the alignment of the boxes so they are no longer centerd throughout the row. 我尝试的一切(调整框的宽度,更改边距,在现有框之间添加空列)都会改变框的对齐方式,使它们不再在整个行中居中。

Is there a way to add space between the content boxes while preserving the centered spacing of the boxes? 有没有办法在内容框之间添加空格,同时保留框的居中间距?

.pathBoxMain {
  border: 5px solid black;
  margin: 10px 0px 10px 0px;
  height: 175px;
}

The margin property is probably what you're looking for 保证金属性可能是您正在寻找的

Use margin: top right bottom left . 使用margin: top right bottom left See documentation So you have to add something like. 请参阅文档所以你必须添加类似的东西。

margin: 10px 0 10px 0;

This will add 10 pixels on the top and 10 pixels on bottom. 这将在顶部添加10个像素,在底部添加10个像素。

.pathBoxMain {
 border: 5px solid black;
 padding: 10px 0px 0px 0px;
 margin: 10px 0 10px 0;
 height: 175px;
}

As bootstrap uses box-sizing: border-box , the margin isn't included in the col-lg-4 width. 由于bootstrap使用box-sizing: border-box ,因此col-lg-4宽度中不包含margin However, you can override that width and add some margin to every second element in the row, like: 但是,您可以覆盖该宽度并为行中的每个第二个元素添加一些边距,例如:

.col-lg-4{
    width: 32% !important;
}

div:nth-of-type(3n+2){
    margin-left: 2%;
    margin-right: 2%;
}

/* (32% width * 3 elements in a row) + 2% margin left and right = 100% width */

Bootply Bootply

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM