Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix Bootstrap Padding On Nested Columns

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:

  • It does not traverse the DOM in the desired order, so it won't do nested ones well.
  • I don't even want to fix that because it seems like this is a very bad solution (it would happen every time the viewport size changes)

What do I do?

like image 711
Alain Jacomet Forte Avatar asked Mar 30 '14 19:03

Alain Jacomet Forte


People also ask

How do you put a space between two columns in Bootstrap?

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.

How do I reduce the gutter space in Bootstrap?

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.

How do I add a space between columns in Bootstrap 5?

Using “div” tag: Simply adding a “div” with padding in between two “div” tags gives spacing between the “div”.

What is Bootstrap Gy?

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.


1 Answers

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);
}
like image 81
Jonathan Lonowski Avatar answered Sep 19 '22 17:09

Jonathan Lonowski