Be a boss and use Bootstrap's built-in variables, mixins, and more via LESS
Bootstrap is made with LESS at it's core, a dynamic stylesheet language created by Alexis Sellier. It makes developing systems-based CSS faster, easier, and more fun.
As an extension of CSS, LESS includes variables, mixins for reusable snippets of code, operations for simple math, nesting, and even color functions.
Visit the official website at http://lesscss.org to learn more.
Managing colors and pixel values in CSS can be a bit of a pain, usually full of copy and paste. Not with LESS though—assign colors or pixel values as variables and change them once.
Those three border-radius declarations you need to make in regular ol' CSS? Now they're down to one line with the help of mixins, snippets of code you can reuse anywhere.
Make your grid, leading, and more super flexible by doing the math on the fly with operations. Multiple, divide, add, and subtract your way to CSS sanity.
Variable | Value | Usage |
---|---|---|
@linkColor |
#0069d6 | Default link text color |
@linkColorHover |
darken(@linkColor, 15) |
Default link text hover color |
@black |
#000 | Black |
@grayDark |
lighten(@black, 25%) |
Dark gray |
@gray |
lighten(@black, 50%) |
Medium gray |
@grayLight |
lighten(@black, 75%) |
Light gray |
@grayLighter |
lighten(@black, 90%) |
Lighter gray |
@white |
#fff | White |
@blue |
#049CDB | |
@green |
#46a546 | |
@red |
#9d261d | |
@yellow |
#ffc40d | |
@orange |
#f89406 | |
@pink |
#c3325f | |
@purple |
#7a43b6 |
@gridColumns |
12 | |
@gridColumnWidth |
60px | |
@gridGutterWidth |
20px | |
@siteWidth |
(@gridColumns * @gridColumnWidth) + (@gridGutterWidth * (@gridColumns - 1)) |
@baseFontSize |
13px | |
@baseFontFamily |
"Helvetica Neue", Helvetica, Arial, sans-serif |
|
@baseLineHeight |
18px |
@primaryButtonColor |
@blue |
Bootstrap was built with Preboot, an open-source pack of mixins and variables to be used in conjunction with Less, a CSS preprocessor for faster and easier web development.
Check out how we used Preboot in Bootstrap and how you can make use of it should you choose to run Less on your next project.
Use this option to make full use of Bootstrap’s Less variables, mixins, and nesting in CSS via javascript in your browser.
<link rel="stylesheet/less" href="less/bootstrap.less" media="all" /> <script src="js/less-1.1.3.min.js"></script>
Not feeling the .js solution? Try the Less Mac app or use Node.js to compile when you deploy your code.
Here are some of the highlights of what’s included in Twitter Bootstrap as part of Bootstrap. Head over to the Bootstrap website or Github project page to download and learn more.
Variables in Less are perfect for maintaining and updating your CSS headache free. When you want to change a color value or a frequently used value, update it in one spot and you’re set.
// Links @linkColor: #8b59c2; @linkColorHover: darken(@linkColor, 10); // Grays @black: #000; @grayDark: lighten(@black, 25%); @gray: lighten(@black, 50%); @grayLight: lighten(@black, 70%); @grayLighter: lighten(@black, 90%); @white: #fff; // Accent Colors @blue: #08b5fb; @green: #46a546; @red: #9d261d; @yellow: #ffc40d; @orange: #f89406; @pink: #c3325f; @purple: #7a43b6; // Baseline grid @basefont: 13px; @baseline: 18px;
Less also provides another style of commenting in addition to CSS’s normal /* ... */
syntax.
// This is a comment /* This is also a comment */
Mixins are basically includes or partials for CSS, allowing you to combine a block of code into one. They’re great for vendor prefixed properties like box-shadow
, cross-browser gradients, font stacks, and more. Below is a sample of the mixins that are included with Bootstrap.
#font { .shorthand(@weight: normal, @size: 14px, @lineHeight: 20px) { font-size: @size; font-weight: @weight; line-height: @lineHeight; } .sans-serif(@weight: normal, @size: 14px, @lineHeight: 20px) { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: @size; font-weight: @weight; line-height: @lineHeight; } ... }
#gradient { ... .vertical (@startColor: #555, @endColor: #333) { background-color: @endColor; background-repeat: repeat-x; background-image: -khtml-gradient(linear, left top, left bottom, from(@startColor), to(@endColor)); // Konqueror background-image: -moz-linear-gradient(@startColor, @endColor); // FF 3.6+ background-image: -ms-linear-gradient(@startColor, @endColor); // IE10 background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, @startColor), color-stop(100%, @endColor)); // Safari 4+, Chrome 2+ background-image: -webkit-linear-gradient(@startColor, @endColor); // Safari 5.1+, Chrome 10+ background-image: -o-linear-gradient(@startColor, @endColor); // Opera 11.10 background-image: linear-gradient(@startColor, @endColor); // The standard } ... }
Get fancy and perform some math to generate flexible and powerful mixins like the one below.
// Griditude @gridColumns: 16; @gridColumnWidth: 40px; @gridGutterWidth: 20px; @siteWidth: (@gridColumns * @gridColumnWidth) + (@gridGutterWidth * (@gridColumns - 1)); // Make some columns .columns(@columnSpan: 1) { width: (@gridColumnWidth * @columnSpan) + (@gridGutterWidth * (@columnSpan - 1)); }
After modifying the .less
files in /lib/, you'll need to recompile them in order to regenerate the bootstrap-*.*.*.css and bootstrap-*.*.*.min.css files. If you're submitting a pull request to GitHub, you must always recompile.
Method | Steps |
---|---|
Node with makefile |
Install the less command line compiler with npm by running the following command: $ npm install lessc Once installed just run Additionally, if you have watchr installed, you may run |
Javascript |
Download the latest Less.js and include the path to it (and Bootstrap) in the <link rel="stylesheet/less" href="/path/to/bootstrap.less"> <script src="/path/to/less.js"></script> To recompile the .less files, just save them and reload your page. Less.js compiles them and stores them in local storage. |
Command line |
If you already have the less command line tool installed, simply run the following command: $ lessc ./lib/bootstrap.less > bootstrap.css Be sure to include |
Mac app |
The unofficial Mac app watches directories of .less files and compiles the code to local files after every save of a watched .less file. If you like, you can toggle preferences in the app for automatic minifying and which directory the compiled files end up in. |