Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use em or rem font-size in Bootstrap 3
/**
* Use em or rem font-size in Bootstrap 3
*/
@font-size-root: 14px;
@font-unit: 0rem; // Pick em or rem here
// Convert all variables to em
@font-size-base: 1 + @font-unit;
@font-size-large: 1.25 + @font-unit;
@font-size-small: 0.85 + @font-unit;
@font-size-h1: 2.6 + @font-unit;
@font-size-h2: 2.15 + @font-unit;
@font-size-h3: 1.7 + @font-unit;
@font-size-h4: 1.25 + @font-unit;
@font-size-h5: @font-size-base;
@font-size-h6: 0.85 + @font-unit;
@line-height-computed: (@font-size-base * @line-height-base);
@padding-base-vertical: ((6px / @font-size-root) + @font-unit);
@padding-base-horizontal: ((12px / @font-size-root) + @font-unit);
@padding-large-vertical: ((10px / @font-size-root) + @font-unit);
@padding-large-horizontal: ((16px / @font-size-root) + @font-unit);
@padding-small-vertical: ((5px / @font-size-root) + @font-unit);
@padding-small-horizontal: ((10px / @font-size-root) + @font-unit);
@padding-xs-vertical: ((1px / @font-size-root) + @font-unit);
@padding-xs-horizontal: ((5px / @font-size-root) + @font-unit);
@border-radius-base: ((4px / @font-size-root) + @font-unit);
@border-radius-large: ((6px / @font-size-root) + @font-unit);
@border-radius-small: ((3px / @font-size-root) + @font-unit);
@caret-width-base: ((4px / @font-size-root) + @font-unit);
@caret-width-large: ((5px / @font-size-root) + @font-unit);
//** Default `.form-control` height
@input-height-base: (@line-height-computed + (@padding-base-vertical * 2));
//** Large `.form-control` height
@input-height-large: ((@font-size-large * @line-height-large) + (@padding-large-vertical * 2));
//** Small `.form-control` height
@input-height-small: ((@font-size-small * @line-height-small) + (@padding-small-vertical * 2));
// Set root font size
html {
font-size: @font-size-root;
}
// Change styling for elements that mix pixels and ems
pre {
font-size: ( @font-size-base - (1 / @font-size-root) );
}
.form-control-feedback {
width: calc(@input-height-base ~" + 2px");
height: calc(@input-height-base ~" + 2px");
}
.input-lg + .form-control-feedback {
width: calc(@input-height-large ~" + 2px");
height: calc(@input-height-large ~" + 2px");
}
.input-sm + .form-control-feedback {
width: calc(@input-height-small ~" + 2px");
height: calc(@input-height-small ~" + 2px");
}
.form-control {
height: calc(@input-height-base ~" + 2px");
}
.form-horizontal {
.radio, .checkbox, .radio-inline, .checkbox-inline {
padding: @padding-base-vertical;
border: 1px solid transparent;
}
.radio, .checkbox {
min-height: calc((@line-height-computed + @padding-base-vertical) ~"+ 1px");
}
.form-control-static {
padding-top: @padding-base-vertical;
border-top: 1px solid transparent;
padding-bottom: @padding-base-vertical;
border-bottom: 1px solid transparent;
}
@media (min-width: @screen-sm-min) {
.control-label {
padding-top: @padding-base-vertical;
border-top: 1px solid transparent;
}
}
}
@davidcie

This comment has been minimized.

Copy link

davidcie commented Apr 22, 2014

Sounds interesting but can you please comment on how one is supposed to use this with Bootstrap (i.e. order, LESS vs CSS etc.).

@jasny

This comment has been minimized.

Copy link
Owner Author

jasny commented May 25, 2014

@DavidCle You need to work with less and should load this after bootstrap.

@import 'bootstrap/less/bootstrap.less';
@import 'bootstrap-em.less';
@adamjgomez

This comment has been minimized.

Copy link

adamjgomez commented Jun 5, 2014

jasny have you got your gist bootstrap-em.less working with bootstrap-sass in a rails environment?

@jasny

This comment has been minimized.

Copy link
Owner Author

jasny commented Jun 18, 2014

@adamjgomez Nop. I'm a PHP developer and use Less myself. Though you should be able to do something similar with Sass. Please fork and modify this gist and leave a link to it here in the comments.

@ivayloc

This comment has been minimized.

Copy link

ivayloc commented Jul 8, 2014

@adamjgomez if you still need Bootstrap's Sass version rewritten with em and rem units you can check this - https://github.com/ivayloc/twbs-rem-em

@neil-h

This comment has been minimized.

Copy link

neil-h commented Jul 8, 2014

I've got this working in sass here: https://gist.github.com/neil-h/631c5e848ba1f2ae92d1
I had to alter _nav-vertical-align.scss as I was getting an error about incompatible units.

@soymanuelrc

This comment has been minimized.

Copy link

soymanuelrc commented Jul 31, 2014

it works for someone? I don't know if this is the right way.

@ivanistheone

This comment has been minimized.

Copy link

ivanistheone commented Oct 25, 2014

That's very cool, but complicated---it feels like we're fighting the framework.

I just needed a simple adjustable-width headline, and used font-size:6vw; based on the vw (viewport width/100) unit. Apparently vw is well supported these days.

@vol7ron

This comment has been minimized.

Copy link

vol7ron commented Dec 9, 2014

@ivanthisone yes, viewport width and height are supported and preferred over percentages in certain circumstances.

@pribilinskiy

This comment has been minimized.

Copy link

pribilinskiy commented Mar 24, 2015

Unfortunately that gist breaks all rules where @line-height-computed is used as a value for margins and paddings as those properties take the element's own font-size as the base size.

Such rules occur 31 times in Bootstrap 3.3.2 and that makes this gist useless unless all those places are patched.

And even after overriding those rules such patching need to be maintained for every new release of Bootstrap.

Hopefully BS4 will have ems/rems as base units

@sponnet

This comment has been minimized.

Copy link

sponnet commented Mar 25, 2015

Are there any plans to use rems in BS4 ? We have a working rems version that we use in our company - we could contribute it to BS4. Do you know who maintains the backlog or planning of BS4 ?

@pribilinskiy

This comment has been minimized.

Copy link

pribilinskiy commented Mar 25, 2015

More info available in a stackoverflow topic and a Github Issue

For those following along, we'll be able to change from pixels to REMs in v4 when we drop IE8 support
@koddo

This comment has been minimized.

Copy link

koddo commented Apr 6, 2015

@pribilinskiy, could you please elaborate on why @line-height-computed here breaks those margin and padding rules? I'm not against patching my copy of bootstrap. How to make it work with this gist?

@bisubus

This comment has been minimized.

Copy link

bisubus commented May 21, 2015

@koddo, because old @line-height-computed was already applied to the rules that use it, you need to extract all of them and add it to the gist.

@ezersky

This comment has been minimized.

Copy link

ezersky commented Oct 3, 2015

Where are variables @line-height-large and @line-height-small ?

@TensaZangetsu

This comment has been minimized.

Copy link

TensaZangetsu commented Dec 30, 2015

does this convert all px in bootstrap into ems or just some controls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.