The easiest way to customize Bootstrap through LESS is to customize its variables. To do this, you need to perform the following few modifications to your project:
custom-variables.less
in the less
directory. Open main.less
and add the following line to import the new file:@import "custom-variables";
You can now override Bootstrap variables easily by redeclaring them in custom-variables.less
, and Bootstrap will use the overridden values instead of the defaults when it is compiled into CSS.
custom-variables.less
and add the following line:@brand-color: #bada55;
This will override the primary color in Bootstrap with the same greenish color that you used in the previous chapter. As you can see, instead of overriding a lot of rules, we simply need to change the value of a single LESS variable in order to apply the customization to all of the elements that use the primary color across Bootstrap. It is also worth mentioning that back when you used CSS, you only added rules for those components that were used on the page. It would take a lot more rules to override all of the elements to use your own primary color.
custom.css
and use LESS to apply the customizations instead. Create a new LESS file named custom-theme.less
in the less
directory and copy the following content from custom.css
to it, after which you can delete custom.css
:.container .jumbotron { border-radius: 0; }. .label { border-radius: 0; }
main.less
:@import "custom-theme";
When you now recompile main.less
and reload your project, you will see that you have reapplied all the customizations to it from earlier, as shown in the following screenshot:
custom-variables.less
:@navbar-default-bg: darken(@brand-primary, 10%); @navbar-default-brand-hover-color: #fff; @navbar-default-link-color: #fff; @navbar-default-link-active-color: lighten(@brand-primary, 25%); @navbar-default-link-active-bg: darken(@brand-primary, 20%); @jumbotron-bg: lighten(@brand-primary, 30%);
.container div
with the following code:<div class="row"> <div class="content"> <div class="jumbotron"> <div class="container"> <h1>A simple blog</h1> </div> </div> <article> <header> <h2>Extending Bootstrap</h2> <p><time pubdate="pubdate">1/12/2012 3:36 PM</time> · <a href="#">Blogger</a></p> </header> <p>Recently I stumbled on a book on extending Twitter Bootstrap and it really ...</p> <p class="read-more"><a href="#">Read more »</a></p> <footer> <ul class="list-inline"> <li><a href="#" class="label label-primary">Bootstrap</a></li> <li><a href="#" class="label label-primary">CSS</a></li> <li><a href="#" class="label label-primary">LESS</a></li> <li><a href="#" class="label label-primary">JavaScript</a></li> <li><a href="#" class="label label-primary">Grunt</a></li> </ul> </footer> </article> </div> <aside class="sidebar"> <img class="sidebar-avatar" src="http://lorempixel.com/400/400/cats" alt="Avatar"/> <p class="sidebar-bio">Christoffer is a web developer that Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, maxime, neque? Assumenda at commodi et eum illum, incidunt ipsa laborum molestias, necessitatibus numquam quod ratione sint vero. Amet, facilis iusto. </p> </aside> </div>
custom-theme.less
:.content { .make-md-column(9); article { margin-bottom: 40px; } } .sidebar { .make-md-column(3); } .sidebar-avatar { display: block; margin-bottom: 20px; max-width: 100%; } .sidebar-bio { color: @gray; }
Now if you compile your styles and reload the project, it should look something like the following:
Let us walk through what you just did. You added some HTML and CSS for the sidebar and used two mixins to turn the .content
and .sidebar
elements into grid elements. A grid in Bootstrap consists of 12 columns by default, so adding .make-md-column(9)
to .content
makes it span three quarters; leaving one quarter for the sidebar and adding .make-md-column(3)
to .sidebar
makes use of the rest of the space.
Bootstrap comes with a large collection of useful mixins, such as gradients, CSS3 properties, various components, and grid, for various purposes. All of these are great once you learn how to use them; they are listed as follows:
.col-md-6
in your HTML in order to allow for the separation of structure and layout. You can achieve this by adding custom classes to your elements and using mixins such as .make-md-column(6)
to make them act like grid columns instead.18.191.176.99