Oct 07. 2018 · by Helge Sverre

How to use Bootstrap properly

Bootstrap is a popular CSS Framework that features a 12-column grid system, components such as tables, buttons, cards etc, developers like me use bootstrap because it greatly speeds up development of websites and applications because you don't have to reinvent the wheel every time (grids, buttons, list reset styling).

The problem with bootstrap is that when beginners use it, they usually pull it straight from the CDN without any customization or tweaking, or even worse, they try to override the styles in the CDN version of bootstrap, by placing them in a separate file usually with overly specific selectors or using !important.

Speaking from (in)experience, this is a pretty shite way to go about using Bootstrap, because instead of letting bootstrap help you develop your site faster, it will slow you down, and you will start to hate it because you'll have to fight the compiled styles every step of the way.

So what is the right way to use Bootstrap?

Importing it with Sass and tweaking variables until it fits your needs!

src/_bootstrap.scss
SCSS
@import "node_modules/bootstrap/scss/functions";
@import "node_modules/bootstrap/scss/variables";
@import "node_modules/bootstrap/scss/mixins";
@import "node_modules/bootstrap/scss/root";
@import "node_modules/bootstrap/scss/reboot";
@import "node_modules/bootstrap/scss/type";
@import "node_modules/bootstrap/scss/images";
//@import "node_modules/bootstrap/scss/code";
@import "node_modules/bootstrap/scss/grid";
@import "node_modules/bootstrap/scss/tables";
@import "node_modules/bootstrap/scss/forms";
@import "node_modules/bootstrap/scss/buttons";
//@import "node_modules/bootstrap/scss/transitions";
//@import "node_modules/bootstrap/scss/dropdown";
//@import "node_modules/bootstrap/scss/button-group";
//@import "node_modules/bootstrap/scss/input-group";
//@import "node_modules/bootstrap/scss/custom-forms";
//@import "node_modules/bootstrap/scss/nav";
//@import "node_modules/bootstrap/scss/navbar";
//@import "node_modules/bootstrap/scss/card";
//@import "node_modules/bootstrap/scss/breadcrumb";
//@import "node_modules/bootstrap/scss/pagination";
//@import "node_modules/bootstrap/scss/badge";
//@import "node_modules/bootstrap/scss/jumbotron";
//@import "node_modules/bootstrap/scss/alert";
//@import "node_modules/bootstrap/scss/progress";
//@import "node_modules/bootstrap/scss/media";
//@import "node_modules/bootstrap/scss/list-group";
//@import "node_modules/bootstrap/scss/close";
//@import "node_modules/bootstrap/scss/modal";
//@import "node_modules/bootstrap/scss/tooltip";
//@import "node_modules/bootstrap/scss/popover";
//@import "node_modules/bootstrap/scss/carousel";
@import "node_modules/bootstrap/scss/utilities";
//@import "node_modules/bootstrap/scss/print";

Here is an example of a variable.scss file, you can find all the available variables that you can change by checking out the theming bootstrap documentation

src/_variables.scss
SCSS
// Colors
$color-white: #FFFFFF;
$color-light: #FAFAFA;
$color-grey: #888888;
$color-brown: #B18D5B;
$color-black: #000000;

$theme-colors: (
        "white": $color-white,
        "light": $color-light,
        "grey": $color-grey,
        "brown": $color-brown,
        "black": $color-black,
);

// Body
$body-bg: $color-light;
$body-color: $color-grey;

// Misc
$box-shadow: 0 0 50px rgba(0, 0, 0, 0.25);

// Typography
$font-family-sans-serif: 'Source Sans Pro', sans-serif;

// Button
$btn-border-radius: 0;
$btn-border-width: 2px;
$btn-font-weight: bold;
src/app.scss
SCSS
// Remember to import variables before bootstrap
@import 'variables';
@import 'bootstrap';

We import the variables before bootstrap because every variable is defined as a "variable default".

You can assign to variables if they aren't already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won't be re-assigned, but if it doesn't have a value yet, it will be given one.

Sass documentation

I hope that made some sense and helped you on your way.