Bootstrap 3 applies a 15px left and right padding on columns.
This is causing me trouble because my layout has a lot of nested columns:
<div class="col-md-6"></div>
<div class="col-md-6"></div>
<div class="col-md-12">
<div class="col-md-6">
<div class="col-md-12"></div>
<div class="col-md-12"></div>
</div>
<div class="col-md-6"></div>
</div>
See Fiddle.
I don't need to just remove the padding because I need the separation between the elements.
The result I'm after, visually, is this: http://jsfiddle.net/Aeup8/8/
My first approach was to set:
[class^='col-'] {
padding:0;
}
[class^='col-'] + [class^='col-'] {
padding-left: 15px;
}
However, this will not fix columns that wrap onto a second row.
See Fiddle
My second approach was using JavaScript:
(function($) {
var $els = $('[class^="col-"');
//console.log($els);
var cols = {};
$els.each(function(i, col) {
var classes = $(col).attr('class').split(' ');
classes.forEach(function(str) {
var match = str.match(/col-(\w+)-(\d+)/);
if ( match ) {
//console.log($els.eq(i));
cols[match[1]] = cols[match[1]] || {};
var current = cols[match[1]];
if ( match[2] == 12 ) {
current.ids = [];
current.sum = 0;
$els.eq(i).css({ padding: 0 });
return
}
current.ids = current.ids || [];
current.sum = current.sum || 0;
current.sum += ( +match[2] );
current.ids.push(i);
if (current.sum == 12) {
//console.log(current);
current.ids.forEach(function(id) {
$els.eq(id).css({ 'padding': 0, 'padding-right': '15px' });
if (id == i) $els.eq(id).css({ 'padding': 0, 'padding-left': '15px' });
});
current.ids = [];
current.sum = 0;
}
}
});
});
})(jQuery);
See Fiddle
But it has problems:
What do I do?
To add space between columns in Bootstrap use gutter classes. With gutters you can add horizontal or vertical space or even specify how big space should be on different screen size.
The gutters between columns in our predefined grid classes can be removed with .g-0 . This removes the negative margin s from .row and the horizontal padding from all immediate children columns.
Using “div” tag: Simply adding a “div” with padding in between two “div” tags gives spacing between the “div”.
Bootstrap 5 has new gutter classes that are specifically designed to adjust the gutter for the entire row. The gutters classes can be used responsively for each breakpoint (ie: gx-sm-4 ) use g-0 for no gutters. use g-(1-5) to adjust horizontal & vertical gutters via spacing units. use gy-* to adjust vertical gutters.
You can counter the duplicated padding when nesting columns by wrapping each layer in its own .row
:
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-md-12"></div>
<div class="col-md-12"></div>
</div>
</div>
<div class="col-md-6"></div>
</div>
</div>
</div>
The gap between the 2 primary columns will remain, but the nesting won't continue to indent further: http://jsfiddle.net/5uqmE/.
They currently accomplish this by apply a negative margin
that opposes the outer padding
:
// Row
//
// Rows contain and clear the floats of your columns.
.row {
.make-row();
}
// Creates a wrapper for a series of columns
.make-row(@gutter: @grid-gutter-width) {
margin-left: (@gutter / -2);
margin-right: (@gutter / -2);
&:extend(.clearfix all);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With