简体   繁体   中英

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.

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. How can I make it so that there is space between the columns? Like 10-15px or so without forcing them onto another row.

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.

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.

Your code is working normally, check here: 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. 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 {
     margin-left: 15px
}

Note that:

  • margin will change you column size because the col-* should be placed to respect column layout
  • you may need to change col-* first and last child to fix some problem

做你想做的'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 {
  padding: 0px 10px;
}

and then you apply this class on your 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-*. 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.

Check this out: twitter bootstrap grid system. Spacing between columns

You can put another div to place your content inside the .col div, as the below example:

<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

That div will adjust to it's parent padding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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