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 how to use Bootstrap with Less CSS. This way, you can use Bootstrap's Less variables, mixins, and nesting in your CSS.
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 file. Bootstrap's Less components are located in the 'lib' directory. Since Bootstrap v1.4.0, nine less files are included. Let's take a look at what each of these nine files contains.
bootstrap.less
This is the main Less file. This file imports some other less files. This file contains no code.
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, which will not be overridden by the base styles in the scaffolding Less file.
reset.less
This Less file contains the CSS reset. It is an update of Eric Meyer's CSS reset. Resets for some HTML elements such as dfn, samp, etc. are exempted.
scaffolding.less
This Less file contains the basic styles needed to create the grid system, structural layouts, and page templates.
tables.less
This Less file contains styles for creating tables.
type.less
In this Less file, you can find typography-related styles. Styles for headings, paragraphs, lists, code, etc. 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:
<script src="js/less-1.1.5.min.js.html"></script>
Please 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 colors
@black |
#000 | |
@grayDarker |
#222 | |
@grayDark |
#333 | |
@gray |
#555 | |
@grayLight |
#999 | |
@grayLighter |
#eee | |
@white |
#fff |
Accent colors
@blue |
#049cdb | |
@green |
#4676A6 | |
@red |
#9d261d | |
@yellow |
#ffc40d | |
@orange |
#f89406 | |
@pink |
#c3325f | |
@purple |
#7a43b6 |
Buttons
@primaryButtonBackground |
@linkColor |
Forms
@placeholderText |
@grayLight |
Navbar
@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 |
#557EA6 | |
@successBackground |
#D8E4F0 | |
@infoText |
#3a87ad | |
@infoBackground |
#d9edf7 |
Bootstrap Mixins
Basic mixins
Basic mixins are essentially entire or partial code snippets. They are written like a CSS class and can be called anywhere.
.element {
.clearfix();
}
Parametric mixins
Parametric mixins are very similar to basic mixins, the difference being that the former can accept parameters (by name), and parameters can have default values.
.element {
.border-radius(4px);
}
Easy to customize
Basically all of Bootstrap's mixins are saved in mixins.less, which is an excellent utility .less file. We can use its mixins in any .less file.
Therefore, you can either directly use existing mixins or define a new mixin.
Includes mixins
Utilities
| Mixins | Parameters | Usage |
|---|---|---|
.clearfix() |
None | Clear floats |
.tab-focus() |
None | Add Webkit-like focus styles and Firefox-like outlines. |
.center-block() |
None | Usagemargin: autoAutomatically center block-level elements. |
.ie7-inline-block() |
None | Add rulesdisplay: inline-blockto support IE7 |
.size() |
@height: 5px, @width: 5px |
Quickly set the height and width. |
.square() |
@size: 5px |
Based on.size()Set a square area. |
.opacity() |
@opacity: 100 |
Set the opacity percentage (e.g., "50" or "75") |
Forms
| Mixins | Parameters | Usage |
|---|---|---|
.placeholder() |
@color: @placeholderText |
Set the input field'splaceholderfont color. |
Typography
| Mixins | 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 serif font family, font size, weight, and line spacing. |
#font > .sans-serif() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Set sans-serif font family, font size, weight, and line spacing. |
#font > .monospace() |
@size: @baseFontSize, @weight: normal, @lineHeight: @baseLineHeight |
Set monospace font family, font size, weight, and line spacing. |
Grid system
| Mixins | Parameters | Usage |
|---|---|---|
.container-fixed() |
None | Create a horizontally centered container to hold content. |
#grid > .core() |
@gridColumnWidth, @gridGutterWidth |
UsagenColumns andxpixel gutter width, generates a pixel grid system (container, rows, columns). |
#grid > .fluid() |
@fluidGridColumnWidth, @fluidGridGutterWidth |
UsagenColumns andx% gutter width, generates a percentage grid system. |
CSS3 properties
| Mixins | Parameters | Usage |
|---|---|---|
.border-radius() |
@radius: 5px |
Round the element's corners; can be 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 (e.g.,all .2s linear) |
.rotate() |
@degrees |
Rotate an element.ndegree |
.scale() |
@ratio |
Scale an element relative to its original size.ntimes |
.translate() |
@x: 0, @y: 0 |
Move x and y pixels on a plane. |
.background-clip() |
@clip |
Clip an element's background (used forborder-radius) |
.background-size() |
@size |
Change the background image size via CSS3. |
.box-sizing() |
@boxmodel |
Change an element's box model (e.g., used at 100% widthinputonborder-box ) |
.user-select() |
@select |
Change the page's text selection cursor. |
.resizable() |
@direction: both |
Change the bottom-right coordinate to resize an element. |
.content-columns() |
@columnCount, @columnGap: @gridColumnGutter |
Make the content within an element use CSS3 columns. |
Backgrounds and gradients
| Mixins | Parameters | Usage |
|---|---|---|
#translucent > .background() |
@color: @white, @alpha: 1 |
Set an element's background color and opacity. |
#translucent > .border() |
@color: @white, @alpha: 1 |
Set an element's border color and opacity. |
#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 give buttons a gradient background and light/dark borders. |
How to compile
Node.js with a makefile
Run the following commands to install the LESS command-line compiler and uglify-js globally with npm:
$ npm install -g less uglify-js
After installation succeeds, run `make` in your Bootstrap root directory to compile the CSS.
In addition, if you have installed watchr, you can run `make watch`, which will automatically recompile Bootstrap each time you modify it. (Not required, but merely for convenience.)
Using JavaScript
Simply reference the less.js file (Download the latest Less.js) and reload the page.
The js file will compile less.
<link rel="stylesheet/less" href="../path/to/bootstrap.less.html"> <script src="../path/to/less.js.html"></script>
Command line
If you have already installed the Less command line, use the following command to compile:
$ lessc ./lib/bootstrap.less > bootstrap.css
If you want a compressed compilation, you can use the --compress command.
Less Mac app
Although unofficial, there is a useful Less Mac application available for compiling.
Click here to download all HTML, CSS, JS, and image files used in this tutorial.
Other extensions