htmlcsstwitter-bootstrapbuttonwhitespace

Keeping a bit of vertical space between elements when they wrap?


Using Bootstrap, when I'm putting a bunch of elements (e.g. buttons) in a row and shrink the window, the elements wrap around.

But while there is some horizontal space between the elements when the window is wide enough to contain everything next to each other, vertically everything gets stuck together, with zero pixels of empty space between.

Live example (narrow or widen the output window to see the effect)

Screenshot example:

  1. Wide enough, notice space between buttons:
  2. Wrapping around, notice NO space between the buttons stacked on top of each other:

I guess I could mess around with some custom CSS, adding vertical margins or whatnot, but in order to keep things as compatible and standard as possible, I wonder if there is a better or more native way to fix this in a Bootstrap layout?


Solution

  • What happens here is the buttons are displayed as an inline block and by default such elements have no white space or margin to begin with. You can use below method to avoid this.

    What I have done is added a class to the parent element of the buttons and inherit style to class "btn".

    html

    <div class='container'>
        <div class='row'>
            <div class='col-xs-12 button-wrapper'>
                <button class='btn btn-primary'>Lorem ipsum dolor sit amet</button>
                <button class='btn btn-info'>consectetur adipiscing elit</button>
                <button class='btn btn-success'>sed do eiusmod tempor incididunt</button>
            </div>
        </div>
    </div>
    

    css

    .button-wrapper .btn {
        margin-bottom:5px;
    }
    

    Demo