Preboot

A collection of LESS mixins and variables for writing better CSS from @mdo.

Introducing Preboot

Preboot is a comprehensive and flexible collection of LESS utilities. Its original variables and mixins became the precursor to Bootstrap. Since then, it's all come full circle.

After a two-year hiatus, Preboot has been reborn with many of the LESS variable and mixin improvements from Bootstrap, along with some new enhancements, too. It's one of the lightest and most powerful tools for CSS development without any prebuilt components.

Need something more powerful? Use Bootstrap.

Download Preboot 2 GitHub project

What's included

Here's the rundown of what you can find in Preboot. Jump to a section for some lightweight documentation and code snippets.

Variables

Variables still don't exist in CSS, but they do in LESS and other CSS preprocessors. Preboot includes a several groups of meaningful and useful variables for any project.

Colors

Easily make use of two color schemes included in Preboot: grayscale and semantic. Grayscale colors provide quick access to shades of black (in increments of 10%) while semantic includes various colors assigned to meaningful values.

1 @black-10: darken(#fff, 10%);
2 @black-20: darken(#fff, 20%);
3 @black-30: darken(#fff, 30%);
4 @black-40: darken(#fff, 40%);
5 @black-50: darken(#fff, 50%);
6 @black-60: darken(#fff, 60%);
7 @black-70: darken(#fff, 70%);
8 @black-80: darken(#fff, 80%);
9 @black-90: darken(#fff, 90%);
1 @brand-primary: #428bca;
2 @brand-success: #5cb85c;
3 @brand-warning: #f0ad4e;
4 @brand-danger:  #d9534f;
5 @brand-info:    #5bc0de;

Use any of these color variables as they are or reassign them to more meaningful variables for your project.

 1 // Use as-is
 2 .masthead {
 3   ...
 4   background-color: @brand-primary;
 5   ...
 6 }
 7 
 8 // Reassigned variables in LESS
 9 @alert-message-background: @brand-info;
10 .alert {
11   background-color: @alert-message-background;
12 }

Scaffolding

A handful of variables for quickly customizing key elements of your site's skeleton.

1 // Scaffolding
2 @body-background: #fff;
3 @text-color:      @black-50;

Easily style your links with the right color with only one value.

 1 // Variables
 2 @link-color:       @brand-primary;
 3 @link-color-hover: darken(@link-color, 15%);
 4 
 5 // Usage
 6 a {
 7   color: @link-color;
 8   text-decoration: none;
 9 
10   &:hover {
11     color: @link-color-hover;
12     text-decoration: underline;
13   }
14 }

Note that the @link-color-hover uses a function, another awesome tool from LESS, to automagically create the right hover color. You can use darken, lighten, saturate, and desaturate.

Typography

Easily set your type face, text size, leading, and more with a few quick variables. Preboot makes use of these as well to provide easy typographic mixins as well.

 1 // Typography
 2 
 3 // Font stacks for easy inclusion and customization
 4 @font-family-sans-serif:  "Helvetica Neue", Helvetica, Arial, sans-serif;
 5 @font-family-serif:       Georgia, "Times New Roman", Times, serif;
 6 @font-family-monospace:   Monaco, Menlo, Consolas, "Courier New", monospace;
 7 @font-family-base:        @font-family-sans-serif;
 8 
 9 // Generate a straightforward type scale
10 @font-size-base:          14px;
11 @font-size-large:         @font-size-base * 1.25; // ~18px
12 @font-size-small:         @font-size-base * 0.85; // ~12px
13 @font-size-mini:          @font-size-base * 0.75; // ~11px
14 
15 // Baseline for the line-height of the body
16 // To be used in conjunction with @font-size-base
17 @line-height-base:        20px;
18 
19 // Enable the option to customize headings
20 @headings-font-family:    inherit; // From @font-family-base
21 @headings-font-weight:    500;

Grid system

Generate semantic, mobile-first grid layouts with a few variables and mixins, all without superfluous markup.

Grid variables

There are three grid variables that Preboot uses to power the grid mixins.

1 // Specify the number of available columns
2 @grid-columns:          12;
3 // Inner padding on each side of a column to create gutters
4 @grid-column-padding:   15px;
5 // Point at which the floats kick in and horizontally align columns
6 @grid-float-breakpoint: 768px;

Customizing these variables should automatically work with any of the grid mixins, so tweak away.

Grid mixins

There are three available mixins for each part of a standard grid system:

Heads up! These grid columns are mobile first, meaning by default they stack vertically. Floating only kicks in with viewports of 768px or higher.

 1 .make-row() {
 2   // Negative margin the row out to align the content of columns
 3   margin-left: -@grid-column-padding;
 4   margin-right: -@grid-column-padding;
 5   // Then clear the floated columns
 6   .clearfix();
 7 }
 8 
 9 .make-column(@columns) {
10   @media (min-width: @grid-float-breakpoint) {
11     float: left;
12     // Calculate width based on number of columns available
13     width: percentage(@columns / @grid-columns);
14   }
15   // Prevent columns from collapsing when empty
16   min-height: 1px;
17   // Set inner padding as gutters instead of margin
18   padding-left: @grid-column-padding;
19   padding-right: @grid-column-padding;
20   // Proper box-model (padding doesn't add to width)
21   .box-sizing(border-box);
22 }
23 
24 .make-column-offset(@columns) {
25   @media (min-width: @grid-float-breakpoint) {
26     margin-left: percentage(@columns / @grid-columns);
27   }
28 }

Example usage

Using these mixins to generate columns is easy. Here's the HTML and CSS of an example implementation.

1 <div class="wrapper">
2   <div class="content-main">
3     ...
4   </div>
5   <div class="content-sidebar">
6     ...
7   </div>
8 </div>
 1 .wrapper {
 2   .make-row();
 3 }
 4 .content-main {
 5   .make-column(8);
 6 }
 7 .content-sidebar {
 8   .make-column(3);
 9   .make-column-offset(1);
10 }

Need another example? Our table of contents at the top of the page uses a custom grid as well. In the source docs.less file you'll find its super lightweight implementation:

1 .row { .make-row(); }
2 .grid-4 { .make-column(4); }

That's it. No need for additional classes as we just don't need them in this simple of a site.

Vendor mixins

Write more efficient CSS by writing single-line mixins instead of multiple lines of prefixed properties.

Box-sizing

Reset your components' box model with a single mixin. For context, see this helpful article from Mozilla.

1 .box-sizing(@box-model) {
2   -webkit-box-sizing: @box-model; // Safari <= 5
3      -moz-box-sizing: @box-model; // Firefox <= 19
4           box-sizing: @box-model;
5 }

Rounded corners

Today all modern browsers support the non-prefixed border-radius property. As such, there is no .border-radius() mixin, but Preboot does include shortcuts for quickly rounding two corners on a particular side of an object.

 1 .border-top-radius(@radius) {
 2   border-top-right-radius: @radius;
 3    border-top-left-radius: @radius;
 4 }
 5 .border-right-radius(@radius) {
 6   border-bottom-right-radius: @radius;
 7      border-top-right-radius: @radius;
 8 }
 9 .border-bottom-radius(@radius) {
10   border-bottom-right-radius: @radius;
11    border-bottom-left-radius: @radius;
12 }
13 .border-left-radius(@radius) {
14   border-bottom-left-radius: @radius;
15      border-top-left-radius: @radius;
16 }

Box (Drop) shadows

If your target audience is using the latest and greatest browsers and devices, be sure to just use the box-shadow property on its own. If you need support for older Android (pre-v4) and iOS devices (pre-iOS 5), use the mixin to pick up the required -webkit prefix.

Be sure to use RGBA colors in your box shadows so they blend as seamlessly as possible with backgrounds.

1 .box-shadow(@shadow: 0 1px 3px rgba(0,0,0,.25)) {
2   -webkit-box-shadow: @shadow; // iOS <4.3 & Android <4.1
3           box-shadow: @shadow;
4 }

Transitions

Three mixins for flexibility. Set all transition information with one, or specify a separate delay and duration as needed.

 1 .transition(@transition) {
 2   -webkit-transition: @transition;
 3      -moz-transition: @transition;
 4        -o-transition: @transition;
 5           transition: @transition;
 6 }
 7 .transition-delay(@transition-delay) {
 8   -webkit-transition-delay: @transition-delay;
 9      -moz-transition-delay: @transition-delay;
10        -o-transition-delay: @transition-delay;
11           transition-delay: @transition-delay;
12 }
13 .transition-duration(@transition-duration) {
14   -webkit-transition-duration: @transition-duration;
15      -moz-transition-duration: @transition-duration;
16        -o-transition-duration: @transition-duration;
17           transition-duration: @transition-duration;
18 }

Transformations

Rotate, scale, translate (move), or skew any object.

 1 .rotate(@degrees) {
 2   -webkit-transform: rotate(@degrees);
 3      -moz-transform: rotate(@degrees);
 4       -ms-transform: rotate(@degrees);
 5        -o-transform: rotate(@degrees);
 6           transform: rotate(@degrees);
 7 }
 8 .scale(@ratio) {
 9   -webkit-transform: scale(@ratio);
10      -moz-transform: scale(@ratio);
11       -ms-transform: scale(@ratio);
12        -o-transform: scale(@ratio);
13           transform: scale(@ratio);
14 }
15 .translate(@x, @y) {
16   -webkit-transform: translate(@x, @y);
17      -moz-transform: translate(@x, @y);
18       -ms-transform: translate(@x, @y);
19        -o-transform: translate(@x, @y);
20           transform: translate(@x, @y);
21 }
22 .skew(@x, @y) {
23   -webkit-transform: skew(@x, @y);
24      -moz-transform: skew(@x, @y);
25       -ms-transform: skewX(@x) skewY(@y);
26        -o-transform: skew(@x, @y);
27           transform: skew(@x, @y);
28   -webkit-backface-visibility: hidden;
29 }
30 .translate3d(@x, @y, @z) {
31   -webkit-transform: translate3d(@x, @y, @z);
32      -moz-transform: translate3d(@x, @y, @z);
33        -o-transform: translate3d(@x, @y, @z);
34           transform: translate3d(@x, @y, @z);
35 }

Opacity

Set the opacity for all browsers and provide a filter fallback for IE8.

1 .opacity(@opacity) {
2   opacity: @opacity;
3   @opacity-ie: @opacity * 100;
4   filter: ~"alpha(opacity=@{opacity-ie})"; // IE8
5 }

Placeholder text

Provide context for form controls within each field.

1 .placeholder(@color: @input-color-placeholder) {
2   &:-moz-placeholder            { color: @color; } // Firefox 4-18
3   &::-moz-placeholder           { color: @color; } // Firefox 19+
4   &:-ms-input-placeholder       { color: @color; } // Internet Explorer 10+
5   &::-webkit-input-placeholder  { color: @color; } // Safari and Chrome
6 }

Columns

Generate columns via CSS within a single element.

 1 .content-columns(@width, @count, @gap) {
 2   -webkit-column-width: @width;
 3      -moz-column-width: @width;
 4           column-width: @width;
 5   -webkit-column-count: @count;
 6      -moz-column-count: @count;
 7           column-count: @count;
 8   -webkit-column-gap: @gap;
 9      -moz-column-gap: @gap;
10           column-gap: @gap;
11 }

Gradients

Easily turn any two colors into a background gradient. Get more advanced and set a direction, use three colors, or use a radial gradient. With a single mixin you get all the prefixed syntaxes you'll need.

1 #gradient > .vertical(#333, #000);
2 #gradient > .horizontal(#333, #000);
3 #gradient > .radial(#333, #000);

You can also specify the angle of a standard two-color, linear gradient:

1 #gradient > .directional(#333, #000, 45deg);

If you need a barber-stripe style gradient, that's easy, too. Just specify a single color and we'll overlay a translucent white stripe.

1 #gradient > .striped(#333, #000, 45deg);

Up the ante and use three colors instead. Set the first color, the second color, the second color's color stop (a decimal value like 0.25), and the third color with these mixins:

1 #gradient > .vertical-three-colors(#777, #333, .25, #000);
2 #gradient > .horizontal-three-colors(#777, #333, .25, #000);

Heads up! Should you ever need to remove a gradient, be sure to remove any IE-specific filter you may have added. You can do that by using .reset-filter() mixin alongside background-image: none;.

Utility mixins

Similar to vendor mixins, utility mixins provide useful and often repeated snippets of CSS in single lines.

Clearfix

Forget adding class="clearfix" to any element and instead add the .clearfix() mixin where appropriate. Uses the micro clearfix from Nicolas Gallager.

 1 // Mixin
 2 .clearfix() {
 3   &:before,
 4   &:after {
 5     content: " ";
 6     display: table;
 7   }
 8   &:after {
 9     clear: both;
10   }
11 }
12 
13 // Usage
14 .container {
15   .clearfix();
16 }

Horizontal centering

Quickly center any element within its parent. Requires width or max-width to be set.

 1 // Mixin
 2 .center-block() {
 3   display: block;
 4   margin-left: auto;
 5   margin-right: auto;
 6 }
 7 
 8 // Usage
 9 .container {
10   width: 940px;
11   .center-block();
12 }

Sizing helpers

Specify the dimensions of an object more easily.

 1 // Mixins
 2 .size(@width, @height) {
 3   width: @width;
 4   height: @height;
 5 }
 6 .square(@size) {
 7   .size(@size, @size);
 8 }
 9 
10 // Usage
11 .image { .size(400px, 300px); }
12 .avatar { .square(48px); }

Resizable textareas

Easily configure the resize options for any textarea, or any other element. Defaults to normal browser behavior (both).

1 .resizable(@direction: both) {
2   // Options: horizontal, vertical, both
3   resize: @direction;
4   // Safari fix
5   overflow: auto;
6 }

Truncating text

Easily truncate text with an ellipsis with a single mixin. Requires element to be block or inline-block level.

 1 // Mixin
 2 .text-truncate() {
 3   overflow: hidden;
 4   text-overflow: ellipsis;
 5   white-space: nowrap;
 6 }
 7 
 8 // Usage
 9 .branch-name {
10   display: inline-block;
11   max-width: 200px;
12   .text-truncate();
13 }

Retina images

Specify two image paths and the @1x image dimensions, and Preboot will provide an @2x media query. If you have many images to serve, consider writing your retina image CSS manually in a single media query.

 1 // Mixin
 2 .retina-image(@file-1x, @file-2x, @width-1x, @height-1x) {
 3   background-image: url("@{file-1x}");
 4 
 5   @media
 6   only screen and (-webkit-min-device-pixel-ratio: 2),
 7   only screen and (   min--moz-device-pixel-ratio: 2),
 8   only screen and (     -o-min-device-pixel-ratio: 2/1),
 9   only screen and (        min-device-pixel-ratio: 2),
10   only screen and (                min-resolution: 192dpi),
11   only screen and (                min-resolution: 2dppx) {
12     background-image: url("@{file-2x}");
13     background-size: @width-1x @height-1x;
14   }
15 }
16 
17 // Usage
18 .jumbotron {
19   .retina-image("/img/bg-1x.png", "/img/bg-2x.png", 100px, 100px);
20 }