Lean Sass imports

When using Sass in your asset pipeline, make sure you optimize Bootstrap by only @importing the components you need. Your largest optimizations will likely come from the Layout & Components section of our bootstrap.scss.

  1. // Configuration
  2. @import "functions";
  3. @import "variables";
  4. @import "mixins";
  5. @import "utilities";
  6. // Layout & components
  7. @import "root";
  8. @import "reboot";
  9. @import "type";
  10. @import "images";
  11. @import "containers";
  12. @import "grid";
  13. @import "tables";
  14. @import "forms";
  15. @import "buttons";
  16. @import "transitions";
  17. @import "dropdown";
  18. @import "button-group";
  19. @import "nav";
  20. @import "navbar";
  21. @import "card";
  22. @import "accordion";
  23. @import "breadcrumb";
  24. @import "pagination";
  25. @import "badge";
  26. @import "alert";
  27. @import "progress";
  28. @import "list-group";
  29. @import "close";
  30. @import "toasts";
  31. @import "modal";
  32. @import "tooltip";
  33. @import "popover";
  34. @import "carousel";
  35. @import "spinners";
  36. // Helpers
  37. @import "helpers";
  38. // Utilities
  39. @import "utilities/api";

If you’re not using a component, comment it out or delete it entirely. For example, if you’re not using the carousel, remove that import to save some file size in your compiled CSS. Keep in mind there are some dependencies across Sass imports that may make it more difficult to omit a file.

Lean JavaScript

Bootstrap’s JavaScript includes every component in our primary dist files (bootstrap.js and bootstrap.min.js), and even our primary dependency (Popper) with our bundle files (bootstrap.bundle.js and bootstrap.bundle.min.js). While you’re customizing via Sass, be sure to remove related JavaScript.

For instance, assuming you’re using your own JavaScript bundler like Webpack or Rollup, you’d only import the JavaScript you plan on using. In the example below, we show how to just include our modal JavaScript:

  1. // Import just what we need
  2. // import 'bootstrap/js/dist/alert';
  3. // import 'bootstrap/js/dist/button';
  4. // import 'bootstrap/js/dist/carousel';
  5. // import 'bootstrap/js/dist/collapse';
  6. // import 'bootstrap/js/dist/dropdown';
  7. import 'bootstrap/js/dist/modal';
  8. // import 'bootstrap/js/dist/popover';
  9. // import 'bootstrap/js/dist/scrollspy';
  10. // import 'bootstrap/js/dist/tab';
  11. // import 'bootstrap/js/dist/toast';
  12. // import 'bootstrap/js/dist/tooltip';

This way, you’re not including any JavaScript you don’t intend to use for components like buttons, carousels, and tooltips. If you’re importing dropdowns, tooltips or popovers, be sure to list the Popper dependency in your package.json file.

Default Exports

Files in bootstrap/js/dist use the default export, so if you want to use one of them you have to do the following:

  1. import Modal from 'bootstrap/js/dist/modal'
  2. const modal = new Modal(document.getElementById('myModal'))

Autoprefixer .browserslistrc

Bootstrap depends on Autoprefixer to automatically add browser prefixes to certain CSS properties. Prefixes are dictated by our .browserslistrc file, found in the root of the Bootstrap repo. Customizing this list of browsers and recompiling the Sass will automatically remove some CSS from your compiled CSS, if there are vendor prefixes unique to that browser or version.

Unused CSS

Help wanted with this section, please consider opening a PR. Thanks!

While we don’t have a prebuilt example for using PurgeCSS with Bootstrap, there are some helpful articles and walkthroughs that the community has written. Here are some options:

Lastly, this CSS Tricks article on unused CSS shows how to use PurgeCSS and other similar tools.

Minify and gzip

Whenever possible, be sure to compress all the code you serve to your visitors. If you’re using Bootstrap dist files, try to stick to the minified versions (indicated by the .min.css and .min.js extensions). If you’re building Bootstrap from the source with your own build system, be sure to implement your own minifiers for HTML, CSS, and JS.

Nonblocking files

Help wanted with this section, please consider opening a PR. Thanks!

Always use https

Help wanted with this section, please consider opening a PR. Thanks!