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 include various colors assigned to meaningful values.

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

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

// Use as-is
.masthead {
  ...
  background-color: @brand-primary;
  ...
}

// Reassigned variables in LESS
@alert-message-background: @brand-info;
.alert {
  background-color: @alert-message-background;
}

Scaffolding

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

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

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

// Variables
@link-color:       @brand-primary;
@link-color-hover: darken(@link-color, 15%);

// Usage
a {
  color: @link-color;
  text-decoration: none;

  &:hover {
    color: @link-color-hover;
    text-decoration: underline;
  }
}

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.

// Typography

// Font stacks for easy inclusion and customization
@font-family-sans-serif:  "Helvetica Neue", Helvetica, Arial, sans-serif;
@font-family-serif:       Georgia, "Times New Roman", Times, serif;
@font-family-monospace:   Monaco, Menlo, Consolas, "Courier New", monospace;
@font-family-base:        @font-family-sans-serif;

// Generate a straightforward type scale
@font-size-base:          14px;
@font-size-large:         @font-size-base * 1.25; // ~18px
@font-size-small:         @font-size-base * 0.85; // ~12px
@font-size-mini:          @font-size-base * 0.75; // ~11px

// Baseline for the line-height of the body
// To be used in conjunction with @font-size-base
@line-height-base:        20px;

// Enable the option to customize headings
@headings-font-family:    inherit; // From @font-family-base
@headings-font-weight:    500;

Grid system

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

Grid variables

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

// Specify the number of available columns
@grid-columns:          12;
// Inner padding on each side of a column to create gutters
@grid-column-padding:   15px;
// Point at which the floats kick in and horizontally align columns
@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.

.make-row() {
  // Negative margin the row out to align the content of columns
  margin-left: -@grid-column-padding;
  margin-right: -@grid-column-padding;
  // Then clear the floated columns
  .clearfix();
}

.make-column(@columns) {
  @media (min-width: @grid-float-breakpoint) {
    float: left;
    // Calculate width based on number of columns available
    width: percentage(@columns / @grid-columns);
  }
  // Prevent columns from collapsing when empty
  min-height: 1px;
  // Set inner padding as gutters instead of margin
  padding-left: @grid-column-padding;
  padding-right: @grid-column-padding;
  // Proper box-model (padding doesn't add to width)
  .box-sizing(border-box);
}

.make-column-offset(@columns) {
  @media (min-width: @grid-float-breakpoint) {
    margin-left: percentage(@columns / @grid-columns);
  }
}

Example usage

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

<div class="wrapper">
  <div class="content-main">
    ...
  </div>
  <div class="content-sidebar">
    ...
  </div>
</div>
.wrapper {
  .make-row();
}
.content-main {
  .make-column(8);
}
.content-sidebar {
  .make-column(3);
  .make-column-offset(1);
}

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 it's super lightweight implementation:

.row { .make-row(); }
.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.

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

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.

.border-top-radius(@radius) {
  border-top-right-radius: @radius;
   border-top-left-radius: @radius;
}
.border-right-radius(@radius) {
  border-bottom-right-radius: @radius;
     border-top-right-radius: @radius;
}
.border-bottom-radius(@radius) {
  border-bottom-right-radius: @radius;
   border-bottom-left-radius: @radius;
}
.border-left-radius(@radius) {
  border-bottom-left-radius: @radius;
     border-top-left-radius: @radius;
}

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 it's own. If you need support for older Android (pre-v4) and iOS devices (pre-iOS 5), use of 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.

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

Transitions

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

.transition(@transition) {
  -webkit-transition: @transition;
     -moz-transition: @transition;
       -o-transition: @transition;
          transition: @transition;
}
.transition-delay(@transition-delay) {
  -webkit-transition-delay: @transition-delay;
     -moz-transition-delay: @transition-delay;
       -o-transition-delay: @transition-delay;
          transition-delay: @transition-delay;
}
.transition-duration(@transition-duration) {
  -webkit-transition-duration: @transition-duration;
     -moz-transition-duration: @transition-duration;
       -o-transition-duration: @transition-duration;
          transition-duration: @transition-duration;
}

Transformations

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

.rotate(@degrees) {
  -webkit-transform: rotate(@degrees);
     -moz-transform: rotate(@degrees);
      -ms-transform: rotate(@degrees);
       -o-transform: rotate(@degrees);
          transform: rotate(@degrees);
}
.scale(@ratio) {
  -webkit-transform: scale(@ratio);
     -moz-transform: scale(@ratio);
      -ms-transform: scale(@ratio);
       -o-transform: scale(@ratio);
          transform: scale(@ratio);
}
.translate(@x, @y) {
  -webkit-transform: translate(@x, @y);
     -moz-transform: translate(@x, @y);
      -ms-transform: translate(@x, @y);
       -o-transform: translate(@x, @y);
          transform: translate(@x, @y);
}
.skew(@x, @y) {
  -webkit-transform: skew(@x, @y);
     -moz-transform: skew(@x, @y);
      -ms-transform: skewX(@x) skewY(@y);
       -o-transform: skew(@x, @y);
          transform: skew(@x, @y);
  -webkit-backface-visibility: hidden;
}
.translate3d(@x, @y, @z) {
  -webkit-transform: translate3d(@x, @y, @z);
     -moz-transform: translate3d(@x, @y, @z);
       -o-transform: translate3d(@x, @y, @z);
          transform: translate3d(@x, @y, @z);
}

Opacity

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

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

Placeholder text

Provide context for form controls within each field.

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

Columns

Generate columns via CSS within a single element.

.content-columns(@width, @count, @gap) {
  -webkit-column-width: @width;
     -moz-column-width: @width;
          column-width: @width;
  -webkit-column-count: @count;
     -moz-column-count: @count;
          column-count: @count;
  -webkit-column-gap: @gap;
     -moz-column-gap: @gap;
          column-gap: @gap;
}

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.

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

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

#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.

#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:

#gradient > .vertical-three-colors(#777, #333, .25, #000);
#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 alignside 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.

// Mixin
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// Usage
.container {
  .clearfix();
}

Horizontal centering

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

// Mixin
.center-block() {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage
.container {
  width: 940px;
  .center-block();
}

Sizing helpers

Specify the dimensions of an object more easily.

// Mixins
.size(@width, @height) {
  width: @width;
  height: @height;
}
.square(@size) {
  .size(@size, @size);
}

// Usage
.image { .size(400px, 300px); }
.avatar { .square(48px); }

Resizable textareas

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

.resizable(@direction: both) {
  // Options: horizontal, vertical, both
  resize: @direction;
  // Safari fix
  overflow: auto;
}

Truncating text

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

// Mixin
.text-truncate() {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// Usage
.branch-name {
  display: inline-block;
  max-width: 200px;
  .text-truncate();
}

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.

// Mixin
.retina-image(@file-1x, @file-2x, @width-1x, @height-1x) {
  background-image: url("@{file-1x}");

  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and (     -o-min-device-pixel-ratio: 2/1),
  only screen and (        min-device-pixel-ratio: 2),
  only screen and (                min-resolution: 192dpi),
  only screen and (                min-resolution: 2dppx) {
    background-image: url("@{file-2x}");
    background-size: @width-1x @height-1x;
  }
}

// Usage
.jumbotron {
  .retina-image("/img/bg-1x.png", "/img/bg-2x.png", 100px, 100px);
}