Bootstrap LESS
Introduction
Less is a CSS preprocessor that makes CSS dynamic.
On the other hand, Bootstrap is a toolkit for rapidly developing web apps and sites.
In this tutorial, we will discuss knowledge about using Bootstrap with Less CSS. This way, you can use Bootstrap's Less variables, mixins, and nesting in your CSS.
View the Less getting started tutorial
Getting Bootstrap and Less CSS
Bootstrap official website:http://getbootstrap.com/
Less official website:http://lesscss.org/
What's Included
Download Bootstrap and unzip the files. Bootstrap's Less components are located in the 'lib' directory. Since Bootstrap v1.4.0, nine less files have been included. Let's take a look at what each of these nine files contains.
bootstrap.less
This is the main Less file. Some other less files are imported in this file. There is no code in this file.
forms.less
This Less file contains styles for form layouts and input types.
mixins.less
This Less file makes CSS code reusable.
patterns.less
This Less file contains CSS code for repeated user interface elements and is not overridden by the basic styles located in the scaffolding Less file.
reset.less
This Less file contains the CSS reset. It is an update to Eric Meyer's CSS reset. The reset of some HTML elements such as dfn and samp is exempted.
scaffolding.less
This Less file holds the basic styles needed to create the grid system, structured layouts, and page templates.
tables.less
This Less file contains styles for creating tables.
type.less
Typography-related styles can be found in this Less file. Styles for headings, paragraphs, lists, code, and so on are located in this file.
variables.less
This Less file contains variables for customizing the look and feel of Bootstrap.
How to Use
To use it, include the following code in your HTML page:
Note that less-1.1.5.min.js is not in the js folder; you need to download it and place it in the specified folder.
LESS Variables and Variable Values, and Usage Guide
Hyperlinks
@linkColor |
#08c | Default link color | |
|---|---|---|---|
@linkColorHover |
darken(@linkColor, 15%) |
Default link color on hover |
Grayscale
@black |
#000 | |
|---|---|---|
@grayDarker |
#222 | |
@grayDark |
#333 | |
@gray |
#555 | |
@grayLight |
#999 | |
@grayLighter |
#eee | |
@white |
#fff |
Accent color
@blue |
#049cdb | |
|---|---|---|
@green |
#46a546 | |
@red |
#9d261d | |
@yellow |
#ffc40d | |
@orange |
#f89406 | |
@pink |
#c3325f | |
@purple |
#7a43b6 |
button
@primaryButtonBackground |
@linkColor |
|---|
The form
@placeholderText |
@grayLight |
|---|
Navigation bar
@navbarHeight |
40px | |
|---|---|---|
@navbarBackground |
@grayDarker |
|
@navbarBackgroundHighlight |
@grayDark |
|
@navbarText |
@grayLight |
|
@navbarLinkColor |
@grayLight |
|
@navbarLinkColorHover |
@white |
Form states and notifications
@warningText |
#c09853 | |
|---|---|---|
@warningBackground |
#f3edd2 | |
@errorText |
#b94a48 | |
@errorBackground |
#f2dede | |
@successText |
#468847 | |
@successBackground |
#dff0d8 | |
@infoText |
#3a87ad | |
@infoBackground |
#d9edf7 |
Bootstrap Mixins
Basic mixins
Essentially, basic mixins include whole or partial code snippets. They are written like a CSS class and can be called anywhere.
.element {
.clearfix();
}
Mixins with parameters
Mixins with parameters are very similar to basic mixins; the difference is that the former can receive parameters (by name), and parameters can have default values.
.element {
.border-radius(4px);
}
Easy to customize
Basically, all of Bootstrap's mixins are stored in mixins.less, which is an excellent utility .less file; we can use its mixins in any .less file.
Therefore, you can either use existing mixins directly or define a new one yourself.
Included Mixins
Utility functions
| Mixin | Parameters | Usage |
|---|---|---|
.clearfix() |
None | Clear floats |
.tab-focus() |
None | Add Webkit-like focus styles and Firefox-like outlines |
.center-block() |
None | Usemargin: autoAutomatically center a block-level element |
.ie7-inline-block() |
None | Add a regular hack display: inline-block to support IE7 |
.size() |
@height: 5px, @width: 5px |
Quickly set line height and line width |
.square() |
@size: 5px |
Based on.size() Set a square area |
.opacity() |
@opacity: 100 |
Set the opacity percentage (for example "50" or "75") |
The form
| Mixin | Parameters | Usage |
|---|---|---|
.placeholder() |
@color: @placeholderText |
Set the placeholder's placeholder font color in the input box |
Typography
| Mixin | Parameters | Usage |
|---|---|---|
#font > #family > .serif() |
None | Apply a series of serif fonts to an element |
#font > #family > .sans-serif() |
None | Apply a series of sans-serif fonts to an element |
#font > #family > .monospace() |
None | Apply a series of monospace fonts to an element |
#font > .shorthand() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Conveniently set font size, weight, and line spacing |
#font > .serif() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Set the serif font family, font size, weight, and line spacing |
#font > .sans-serif() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Set the sans-serif font family, font size, weight, and line spacing |
#font > .monospace() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Set the monospace font family, font size, weight, and line spacing |
Grid system
| Mixin | Parameters | Usage |
|---|---|---|
.container-fixed() |
None | Create a horizontally centered container to hold content |
#grid > .core() |
@gridColumnWidth, @gridGutterWidth |
Use n Column and gutter x pixel widths, generating a pixel-based grid system (container, row, column) |
#grid > .fluid() |
@fluidGridColumnWidth, @fluidGridGutterWidth |
Use n Column and gutter x % gutter width, generating a percentage-based grid system |
CSS3 properties
| Mixin | Parameters | Usage |
|---|---|---|
.border-radius() |
@radius: 5px |
Round an element's corners, with either a single value or separate values for the four corners |
.box-shadow() |
@shadow: 0 1px 3px rgba(0,0,0,.25) |
Apply a shadow to an element |
.transition() |
@transition |
Add CSS3 transition effects (for example, all .2s linear) |
.rotate() |
@degrees |
rotate an element a given number of n degrees). |
.scale() |
@ratio |
Scale an element to n times its original size |
.translate() |
@x: 0, @y: 0 |
Move x and y pixels on a plane |
.background-clip() |
@clip |
Crop an element's background (used, for example, for border-radius) |
.background-size() |
@size |
changing the background image size via CSS3 |
.box-sizing() |
@boxmodel |
Change an element's box model (for example, used with 100% width input on border-box ) |
.user-select() |
@select |
Change the text selection cursor of the page |
.resizable() |
@direction: both |
Change the bottom-right coordinates to resize an element |
.content-columns() |
@columnCount, @columnGap: @gridColumnGutter |
Make the content of an element use CSS3 columns |
Backgrounds and Gradients
| Mixin | Parameters | Usage |
|---|---|---|
#translucent > .background() |
@color: @white, @alpha: 1 |
Set an element's background color and transparency |
#translucent > .border() |
@color: @white, @alpha: 1 |
Set an element's border color and transparency |
#gradient > .vertical() |
@startColor, @endColor |
Create a cross-browser vertical background gradient |
#gradient > .horizontal() |
@startColor, @endColor |
Create a cross-browser horizontal background gradient |
#gradient > .directional() |
@startColor, @endColor, @deg |
Create a cross-browser angled background gradient |
#gradient > .vertical-three-colors() |
@startColor, @midColor, @colorStop, @endColor |
Create a cross-browser three-color background gradient |
#gradient > .radial() |
@innerColor, @outerColor |
Create a cross-browser radial background gradient |
#gradient > .striped() |
@color, @angle |
Create a cross-browser striped background gradient |
#gradientBar() |
@primaryColor, @secondaryColor |
Used to assign gradient backgrounds and light or dark borders to buttons |
How to Compile
Node.js with a makefile
Run the following command to install the LESS command-line compiler and uglify-js globally via npm:
$ npm install -g less uglify-js
After successful installation, run make in your Bootstrap root directory to compile the CSS.
In addition, if you have watchr installed, you can run make watch, so that Bootstrap is automatically recompiled each time you modify it. (This is not required; it is just for convenience.)
Using JavaScript
Simply include the less.js file (download the latest Less.js ) and reload the page.
The JS file will then compile the less.
Command line
If you have installed the Less command-line tool, run the following command to compile:
$ lessc ./lib/bootstrap.less > bootstrap.css
If you want a minified compilation, you can use the --compress command.
Less Mac app
Although unofficial, there is an available Less Mac application that can be used for compiling.
Click here to download all the HTML, CSS, JS, and image files used in this tutorial.