Add vertical whitespace using Twitter Bootstrap?
Asked Answered
P

13

168

What's the best way to add vertical whitespace using Twitter's Bootstrap?

For example, let's say that I am creating a landing page and would like a bit (100px) of blank whitespace above and below a certain button. Obviously, I could create a certain class for that particular button. But, I would think that Bootstrap should have a DRY way of adding in vertical blank spaces.

Pearlinepearlman answered 5/9, 2012 at 2:3 Comment(0)
L
44

In Bootstrap 4/5 there are spacing utilites (BS4, BS5).

Excerpt from the documentation:

Spacing utilities that apply to all breakpoints, from xs to xl, have no breakpoint abbreviation in them. This is because those classes are applied from min-width: 0 and up, and thus are not bound by a media query. The remaining breakpoints, however, do include a breakpoint abbreviation.

The classes are named using the format {property}{sides}-{size} for xs and {property}{sides}-{breakpoint}-{size} for sm, md, lg, and xl.

Where property is one of:

  • m - for classes that set margin
  • p - for classes that set padding

Where sides is one of (please note differences in BS4 and BS5):

  • t - for classes that set margin-top or padding-top
  • b - for classes that set margin-bottom or padding-bottom
  • l - for classes that set margin-left or padding-left (Bootstrap 4)
  • s - for classes that set margin-left or padding-left (Bootstrap 5)
  • r - for classes that set margin-right or padding-right (Bootstrap 4)
  • e - for classes that set margin-right or padding-right (Bootstrap 5)
  • x - for classes that set both *-left and *-right
  • y - for classes that set both *-top and *-bottom
  • blank - for classes that set a margin or padding on all 4 sides of the element

Where size is one of:

  • 0 - for classes that eliminate the margin or padding by setting it to 0
  • 1 - (by default) for classes that set the margin or padding to $spacer * .25
  • 2 - (by default) for classes that set the margin or padding to $spacer * .5
  • 3 - (by default) for classes that set the margin or padding to $spacer
  • 4 - (by default) for classes that set the margin or padding to $spacer * 1.5
  • 5 - (by default) for classes that set the margin or padding to $spacer * 3

So to have some extra vertical space above and below an element you would use my-5 class.

Leveloff answered 26/1, 2017 at 21:21 Comment(1)
Could you give an example of a valid class string?Coincidence
P
52

In v2, there isn't anything built-in for that much vertical space, so you'll want to stick with a custom class. For smaller heights, I usually just throw a <div class="control-group"> around a button.

Paris answered 5/9, 2012 at 2:7 Comment(12)
or <div class="btn-toolbar">Daugavpils
Thanks. I now have created some spacer10, spacer50, etc classes for this use. I see there is a feature request in github for this already: bit.ly/R9oap9Pearlinepearlman
@Ryan: Nice, didn't see the pull request. I'm a little conflicted by it. Bootstrap is almost entirely presentational (and convenient), but if you squint hard enough to ignore all the extra HTML, you can derive semantics from it. Vertical spacing is purely presentational, so I think it's best done with a custom class/ID.Paris
Also, one thing you might want to look into is using @extend in your SCSS (or something comparable in LESS) to avoid littering your HTML with Bootstrap-specific classes.Paris
it's good to add a css tag like this style="margin-right:10px;" because bootstrap doesn't have good way to add vertical space specially for buttonsPorphyry
@digitalny: I hope you're joking -- inline styles really should be avoided, especially if you're going to be duplicating them across an app. If you need to customize an element, just @extend it in SCSS or apply a new class.Paris
@Paris yes I agree with your idea that's the better way to do thatPorphyry
OP & @Daugavpils this doesn't seem to work in v3, is this for v2?Kowtow
This was for v2, but it seems like v3 has minimized the spacing around .btn-toolbar, so you should just add a new class with your own margins.Paris
Apparently the github feature request has been closed and the feature may make it in to Bootstrap v4. bit.ly/R9oap9Pearlinepearlman
Now that Bootstrap 4 is available, it appears they have built-in spacer classes. See the now checked response by @vlpPearlinepearlman
Thanks for the update @Ryan. I am not sure what the SO etiquette is for splitting questions and answers based on versions, but I think you should probably clarify the version in your question. It's pretty confusing having answers for multiple versions (but not labeled for which version), as it has been six years now (!) since the original was asked and answered. I just added "v2" to my old answer for future time travelers.Paris
O
51

Wrapping works but when you just want a space, I like:

<div class="col-xs-12" style="height:50px;"></div>
Oxyacid answered 3/6, 2015 at 23:53 Comment(3)
Adding an inline style when working with Bootstrap is generally not a good idea. It should at least go into CSS.Registrant
notice that "col-xs-12" is exactly the same as having no classes at allNephralgia
Not a good idea. "What's the best way to add vertical whitespace using Twitter's Bootstrap"... Instead defining custom classes would be better to avoid inline-styles.Paramedic
L
44

In Bootstrap 4/5 there are spacing utilites (BS4, BS5).

Excerpt from the documentation:

Spacing utilities that apply to all breakpoints, from xs to xl, have no breakpoint abbreviation in them. This is because those classes are applied from min-width: 0 and up, and thus are not bound by a media query. The remaining breakpoints, however, do include a breakpoint abbreviation.

The classes are named using the format {property}{sides}-{size} for xs and {property}{sides}-{breakpoint}-{size} for sm, md, lg, and xl.

Where property is one of:

  • m - for classes that set margin
  • p - for classes that set padding

Where sides is one of (please note differences in BS4 and BS5):

  • t - for classes that set margin-top or padding-top
  • b - for classes that set margin-bottom or padding-bottom
  • l - for classes that set margin-left or padding-left (Bootstrap 4)
  • s - for classes that set margin-left or padding-left (Bootstrap 5)
  • r - for classes that set margin-right or padding-right (Bootstrap 4)
  • e - for classes that set margin-right or padding-right (Bootstrap 5)
  • x - for classes that set both *-left and *-right
  • y - for classes that set both *-top and *-bottom
  • blank - for classes that set a margin or padding on all 4 sides of the element

Where size is one of:

  • 0 - for classes that eliminate the margin or padding by setting it to 0
  • 1 - (by default) for classes that set the margin or padding to $spacer * .25
  • 2 - (by default) for classes that set the margin or padding to $spacer * .5
  • 3 - (by default) for classes that set the margin or padding to $spacer
  • 4 - (by default) for classes that set the margin or padding to $spacer * 1.5
  • 5 - (by default) for classes that set the margin or padding to $spacer * 3

So to have some extra vertical space above and below an element you would use my-5 class.

Leveloff answered 26/1, 2017 at 21:21 Comment(1)
Could you give an example of a valid class string?Coincidence
B
38

Sorry to dig an old grave here, but why not just do this?

<div class="form-group">
    &nbsp;
</div>

It will add a space the height of a normal form element.

It seems about 1 line on a form is roughly 50px (47px on my element I just inspected). This is a horizontal form, with label on left 2col and input on right 10col. So your pixels may vary.

Since mine is basically 50px, I would create a spacer of 50px tall with no margins or padding;

.spacer { margin:0; padding:0; height:50px; }
<div class="spacer"></div>
Blavatsky answered 6/8, 2015 at 0:27 Comment(6)
This adds space below group but not aboveUnderstandable
Of course it does, that's exactly what he asked for, a way to add a vertical blank space. If you need custom form-group or row or even a button, make your own class to add to it, overriding the margins. Ie: class="form-group spaced-group" and ".spaced-group { margin-top: 15px !important; margin-bottom: 15px !important; }. .. It's just common css-sense.Blavatsky
The OP specifically asked: "a bit (100px) of blank white space above and below a certain button"Understandable
No, that was an example, not his question. His question was "What's the best way to add vertical blank space using Twitter's Bootstrap?" - a vertical blank space is different than a margin on a specific element. If he was doing it for all buttons he could change the margin of btn or make his own class like "btn-spaced" with his desired margins. If he just wants a way to add a gap wherever he wants, a spacer of a fixed height like I answered is what he needs. He asked one question, and gave an example of another. I assumed he knew CSS enough to know he could override the margins.Blavatsky
Since he said a DRY method, and button, then I would create a button class with the margins I wanted: btn-spaced. If it wasn't for only buttons then a general margin that would apply to rows, form-groups, whatever. Like class="row space-small" or class="form-group space-medium".. Where small medium and large are just variants of the margins, and could be applied to virtually anything. - hard to tell which the op wants. For buttons only or just for any element..Blavatsky
Sorry, my bad, I didn't realise that you meant to add this entire block of HTML. I read it that you were suggesting to just use the form-group class around what ever element needed spacing. But what you're suggesting is to put this div element above and below to create space.Understandable
A
24

I know this is old, but I came here searching for the same thing, I found that Bootstrap has the help-block, very handy for these situations:

<div class="help-block"></div>
Aerology answered 16/11, 2015 at 18:12 Comment(0)
M
21

For version 3 there doesn't appear to be "bootstrap" way to achieve this neatly.

A panel, a well and a form-group all provide some vertical spacing.

A more formal specific vertical spacing solution is, apparently, on the roadmap for bootstrap v4

https://github.com/twbs/bootstrap/issues/4286#issuecomment-36331550 https://github.com/twbs/bootstrap/issues/13532

Mothy answered 18/2, 2015 at 13:30 Comment(1)
.form-group seems the best choice as the css is limited to margin-bottom:15px. .well and .panel should not be used if you want simply add some spacing as they have other attributes (such as background, padding, border, etc)Thales
D
9

I merely created a div class using various heights i.e.

<div class="divider-10"></div>

The CSS is:

.divider-10 {
    width:100%; 
    min-height:1px; 
    margin-top:10px; 
    margin-bottom:10px;  
    display:inline-block; 
    position:relative;
}

Just create a divider class for what ever heights are needed.

Debar answered 24/11, 2016 at 2:4 Comment(0)
B
8

My trick. Not elegant, but it works:

<p>&nbsp;</p>
Bellow answered 4/2, 2017 at 3:17 Comment(1)
@JayKilleen +1 You should post that as an answer. Perhaps people are becoming to reliant on the frameworks and have begun forgetting the basics.Monorail
B
5

I know this is old and there are several good solutions already posted, but a simple solution that worked for me is the following CSS

<style>
  .divider{
    margin: 0cm 0cm .5cm 0cm;
  }
</style>

and then create a div in your html

<div class="divider"></div>
Brushwood answered 2/3, 2017 at 19:52 Comment(0)
M
5

Just use <br/>. I found myself here looking for the answer to this question and then felt sort of silly for not thinking about using a simple line break as suggested by user JayKilleen in a comment.

Monorail answered 8/8, 2017 at 19:14 Comment(2)
Downside is you don't get exact 100px control, but it should certainly work to add a reasonable amount of space.Earn
@mix3d, you're right there, I didn't notice at first that he cared about it being exactly 100px. I thought he just wanted a "bit". He could definitely make his own helper class like everyone suggested. But since this post is so many years old, he could now just upgrade to bootstrap 4 for precise spacing, or take the helper classes from it.Monorail
O
4

I tried using <div class="control-group"> and it did not change my layout. It did not add vertical space. The solution that worked for me was:

<ol style="visibility:hidden;"></ol>

If that doesn't give you enough vertical space, you can incrementally get more by adding nested <li>&nbsp;</li> tags.

Obtect answered 16/1, 2015 at 19:12 Comment(1)
Thanks, and your suggestion would work with or without Bootstrap. But the question was specifically about using Twitter Bootstrap to add the spacing.Heins
P
1
<form>
 <fieldset class="form-group"><input type="text" class="form-control" placeholder="Input"/></fieldset>
 <fieldset class="form-group"><button class="btn btn-primary"/>Button</fieldset>
</form>

http://v4-alpha.getbootstrap.com/components/forms/#form-controls

Phenolphthalein answered 13/5, 2016 at 11:41 Comment(4)
How is this an answer to the question?Sophey
It adds vertical spacing as the original poster asked for, also bootstrap devs are now catering for it in the latest version (4) in beta state. It is actually the correct answer and if you google vertical spacing in bootstrap you would see it was not catered for previously. Please don't downvote stuff you have not tested or googled.Phenolphthalein
You clearly have not read the question. "I would like a bit (100px) of blank white space above and below a certain button." A certain button... a certain button...Sophey
Before that he said: "For example, let's say". It was just an example, it wasn't the specific problem. Don't quote out of context. The example I provided is ample and all the info I gave is adequate.Phenolphthalein
B
-3

There is nothing more DRY than

.btn {
    margin-bottom:5px;
}
Bovine answered 18/9, 2014 at 10:19 Comment(2)
I'd recommend against modifying the styling of base bootstrap css classes.Ayotte
@JamesPalawaga +1. But if this was to be a general standard across your application, you could override the bootstrap styling, using LESS and variables and/or carefully managed override stylesheets.Heins

© 2022 - 2024 — McMap. All rights reserved.