Performance: Importing Bootstrap in projects
How quickly the page loads and renders styles by including Bootstrap CSS and JS files.
Use only needed Bootstrap components via custom build or import CSS partials and defer JS loading: <link rel="stylesheet" href="bootstrap-custom.min.css" media="print" onload="this.media='all'"> <script defer src="bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full Bootstrap CSS and JS loaded synchronously | Minimal DOM impact | Triggers 1 reflow after CSS load | High paint cost due to large styles | [X] Bad |
| Custom Bootstrap CSS partials with deferred JS | Minimal DOM impact | Single reflow after CSS load | Lower paint cost due to smaller CSS | [OK] Good |
<head> section to load styles before the page shows.<link> tag is used to include external CSS files like Bootstrap.<script> tag using the src attribute.bootstrap.bundle.min.js and it must be linked with src, not href.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <title>Bootstrap Test</title> </head> <body> <button class="btn btn-primary">Click Me</button> </body> </html>
<link> tag includes Bootstrap CSS from CDN, so styles apply.btn btn-primary style the button with Bootstrap's primary blue color.<script href="bootstrap.bundle.min.js"></script><script> tag uses src to link files, not href.href means the browser does not load the JavaScript file, so features fail.<link> in the <head> for styles to load early.<script src="..."></script> before closing </body> for proper loading and performance.Pass the test and this pattern moves unknown