Build Smaller Bootstrap CSS Upfront

Build Smaller Bootstrap CSS Upfront

Scenario

Reduce the file size of bootstrap.min.cs to 15K or less increasing website response by 1 second or more by removing unnecessary components before customization.

  • Year: 2025
  • Difficulty: High
  • Implementation: 1-2 hours
  • Testing: 1-2 hours
  • Platform: Visual Studio 2022
  • Framework: Bootstrap 5.3.3
  • Tooling: Nodejs 22.13.0

Fream Principle: Prepare ahead of time or suffer during and afterwards.

Remove Unnecessary Components Upfront

While there are a few downloadable and online utilities (Purifycss, Purgecss, Unusedcss) to remove unused CSS automatically after a website is published, this approach is not recommended because of unreliable results often with required recurring subscription cost. Complex scripts with myriad options are usually required to get working first. Then file size may be greatly reduced, except your website won't work because of missing characters or required components. Without testing, web pages may look fine visually but things like menus or forms may not function. See minimum required Bootstrap components below:

// Configuration
@import "functions";
@import "variables";
@import "mixins";
@import "utilities";

// Layout & components
@import "root";
@import "reboot";
@import "type";
@import "grid";
@import "buttons";
@import "forms";
@import "tables";
@import "nav";
@import "navbar";
@import "card";

// Utilities
@import "utilities/api";        

Comment Out Unused Import Components

Edit the BOOTSTRAP.SCSS located in "../../node_modules/bootstrap/scss/bootstrap" of your Bootswatch Customization and comment out the layout components you didn't use in your project code. You may need to experiment with removing certain components to make the resulting BOOTSTRAP.MIN.CSS smaller. Generally, the raw files size should be 150K or less to be 15K or less after compression.

/*!
 * Bootstrap v5.1.3 (https://getbootstrap.com/)
 * Copyright 2011-2021 The Bootstrap Authors
 * Copyright 2011-2021 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
 */

// scss-docs-start import-stack
// Configuration
@import "functions";
@import "variables";
@import "mixins";
@import "utilities";

// Layout & components
@import "root";
@import "reboot";
@import "type";
@import "images";
@import "containers";
@import "grid";
@import "tables";
@import "forms";
@import "buttons";
@import "transitions";
@import "dropdown";
@import "button-group";
@import "nav";
@import "navbar";
@import "card";
// @import "accordion";
@import "breadcrumb";
// @import "pagination";
@import "badge";
@import "alert";
// @import "progress";
@import "list-group";
// @import "close";
// @import "toasts";
// @import "modal";
@import "tooltip";
// @import "popover";
// @import "carousel";
// @import "spinners";
// @import "offcanvas";
@import "placeholders";

// Helpers
@import "helpers";

// Utilities
@import "utilities/api";
// scss-docs-end import-stack        

Better Approach

Now when you compile your BOOTSTRAP.MIN.CSS the bloat has been automatically removed upfront with no complicated scripts or recurring cost. Less is more as you have better control and understanding of the process. The bonus is all pages will load faster.

  1. npm install -g grunt-cli
  2. grunt swatch:custom
  3. Copy bootstrap.min.css to your project and test


For more thought leadership, follow Kevin Fream.

要查看或添加评论,请登录

Kevin Fream的更多文章

  • Business Rundown

    Business Rundown

    T - Minus 311 Days It's only mid 50's but it feels like Summer as Neo and I walk the whole neighborhood. Every year…

  • Well Played

    Well Played

    T - Minus 312 Days Alan Ritcher as Reacher lives a life most people can't imagine - having only the clothes on your…

  • Please Don't Stop The Music

    Please Don't Stop The Music

    T - Minus 313 Days Tom MacDonald is smart enough to know if you enable copyright on YouTube videos, then they can't be…

  • Untouchable Leadership

    Untouchable Leadership

    T - Minus 314 Days It didn't make a lot of sense how Al Capone couldn't be arrested. There would be crazy shootouts…

  • Fewer Choices

    Fewer Choices

    T - Minus 315 Days 2019 was going to be our best year ever until Covid and putting Netflix on continuous play seemed…

  • Cold Plunge

    Cold Plunge

    T - Minus 316 Days It's 3 and feels like -15 so Neo and I played follow-the-leader all through the house instead of…

  • Mystery Creates Wonder

    Mystery Creates Wonder

    T - Minus 317 Days As a kid, my hero was William Shatner as Captain Kirk and you never knew what you would see on Star…

  • Finding Balance

    Finding Balance

    T - Minus 318 Days Some years back my trainer at Gold's Gym said, "Kevin you have terrible posture." "Whaddaya mean I…

    1 条评论
  • Natural Law

    Natural Law

    T - Minus 319 Days It was a typical Sunday where I would meet my friend Scott. He never asked me where my mom was just…

  • Tide Turning

    Tide Turning

    T - Minus 320 Days It's one of my personal rules. Saturday is for music, comedy, movies, and games.

社区洞察

其他会员也浏览了