简体   繁体   English

如何在Bootstrap中保留三列之间的空间?

[英]How to keep space between three columns in Bootstrap?

I've done quite a bit of searching here on Stackoverflow on how to solve this problem efficiently, but I still haven't seemed to find exactly what I'm looking for. 我已经在Stackoverflow上做了很多关于如何有效地解决这个问题的搜索,但我似乎还没有找到我正在寻找的确切内容。

Basically, I have three columns that I want evenly spaced and centered across my page. 基本上,我有三列我想要均匀分布并在我的页面中居中。 However, when I set col-md-4 for all three columns, the end result is they are all three bunched up to each other. 但是,当我为所有三列设置col-md-4时,最终结果是它们都是三个相互聚集的。 How can I make it so that there is space between the columns? 如何才能使列之间有空间? Like 10-15px or so without forcing them onto another row. 像10-15px左右,没有强迫他们到另一行。

Here is some example code: 这是一些示例代码:

<div class="row-fluid">
     <div class="col-md-4">
          <p>Stuff that fills this column</p>
     </div>
     <div class="col-md-4">
          <p>Stuff that fills this column</p>
     </div>
     <div class="col-md-4">
          <p>Stuff that fills this column</p>
     </div>
</div>

Maybe I'm just doing something wrong but I cannot seem to figure out how to make this work. 也许我只是做错了什么,但我似乎无法弄清楚如何使这项工作。 I've seen several people suggest to just put them into another div with some padding but that doesn't seem to work for me. 我已经看到有几个人建议将它们放入另一个带有填充物的div中,但这对我来说似乎不起作用。

Thanks for any help! 谢谢你的帮助! I'm open to all suggestions! 我对所有建议持开放态度!

Actually, your code already creates spaces between columns, because in bootstrap the column has 15px padding from each side. 实际上,您的代码已经在列之间创建了空格,因为在引导程序中,列的每边都有15px填充。

Your code is working normally, check here: http://www.bootply.com/H6DQGdZxGy 您的代码正常运行,请在此处查看: http//www.bootply.com/H6DQGdZxGy

It's a late answer but I guess that some people can be interessed by another explanation. 这是一个迟到的答案,但我想有些人可以通过另一种解释来解释。 Bootstrap "cols" system is not made to decorate but to place elements in pages. Bootstrap“cols”系统不是用来装饰,而是在页面中放置元素。 If you need to space column contents, you need to wrap your content: 如果需要空间列内容,则需要包装内容:

<div class="row-fluid">
 <div class="col-md-4">
   <div class="col-spaced">
      <p>Stuff that fills this column</p>
   </div>
 </div>
 <div class="col-md-4">
   <div class="col-spaced">
      <p>Stuff that fills this column</p>
   </div>
 </div>
 <div class="col-md-4">
    <div class="col-spaced">
      <p>Stuff that fills this column</p>
   </div>     
</div>

Then you can add spacing on ".col-spaced" by using padding 然后,您可以使用填充在“.col-spaced”上添加间距

.col-spaced {
     margin-left: 15px
}

Note that: 注意:

  • margin will change you column size because the col-* should be placed to respect column layout margin会改变列大小,因为col- *应该放在尊重列布局上
  • you may need to change col-* first and last child to fix some problem 您可能需要更改col- *第一个和最后一个孩子来解决一些问题

做你想做的'hacky'方法是给列一个与背景颜色相同的边框。

You can do something like: 你可以这样做:

[class*="col-"] {
  padding-right: 15px;
  padding-left: 15px;
}

[class*="col-"]:first-child {
  padding-left: 0px;
}

[class*="col-"]:last-child {
  padding-right: 0px;
}

You might add a content to wrap it, otherwise you'll have those rules applied to all columns in your layout! 您可以添加一个内容来包装它,否则您将这些规则应用于布局中的所有列!

.spaced-columns [class*="col-"] {
  padding-right: 15px;
  padding-left: 15px;
}

So then you can use: 那么你可以使用:

<div class="spaced-columns">
  <div class="col-md-4"> your content here</div>
  <div class="col-md-4"> your content here</div>
  <div class="col-md-4"> your content here</div>
</div>

or you can create just a class like: spaced-col and then add a padding on it: 或者你可以只创建一个类: spaced-col然后在其上添加一个填充:

.spaced-col {
  padding: 0px 10px;
}

and then you apply this class on your cols 然后你在cols上应用这个类

<div class="col-md-4 spaced-col"> your content here</div>
<div class="col-md-4 spaced-col"> your content here</div>
<div class="col-md-4 spaced-col"> your content here</div>

So you'll have your spacing as you want :) 所以你会有你想要的间距:)

Cheers 干杯

If you use padding and change the background color, you may notice that the colors of the columns don't have much spacing between them. 如果使用填充并更改背景颜色,您可能会注意到列的颜色之间没有太多间距。 Perhaps a better option would be 也许更好的选择

    .col-md-4 {
        margin-left: 5px;
    }

You have the option to use column offsetting col-md-offset-*. 您可以选择使用列偏移col-md-offset- *。 You wouldn't be able to maintain the spacing of your columns (reduce 4 to 3), but I believe it should somewhat do the job for responsive spacing. 你将无法保持列的间距(减少4到3),但我相信它应该在一定程度上完成响应间距的工作。

Check this out: twitter bootstrap grid system. 看看这个: twitter bootstrap网格系统。 Spacing between columns 列之间的间距

You can put another div to place your content inside the .col div, as the below example: 您可以放置​​另一个div来将您的内容放在.col div中,如下例所示:

<div class="container">
  <div class="row">
    <div class="col-md-4">
      <div class="content-wrapper" style="border:1px solid #000;">
        <p>Some content here</p>
      </div>
    </div>
    <div class="col-md-4">
      <div class="content-wrapper" style="border:1px solid #000;">
        <p>Some content here</p>
      </div>
     </div>
    <div class="col-md-4">
      <div class="content-wrapper" style="border:1px solid #000;">
        <p>Some content here</p>
      </div>
    </div>
  </div>
</div>

See it working here: https://codepen.io/pmfeo/pen/RVxPXg 看到它在这里工作: https//codepen.io/pmfeo/pen/RVxPXg

That div will adjust to it's parent padding. 该div将调整为它的父级填充。

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

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