Introduction
You change the spacing scale to control how much space appears around elements, making your page look neat and balanced.
// In your custom SCSS file $spacer: 1.5rem; // Or to customize the whole scale $spacers: ( 0: 0, 1: 0.25rem, 2: 0.5rem, 3: 1rem, 4: 1.5rem, 5: 3rem );
$spacer: 2rem;$spacers: ( 0: 0, 1: 0.5rem, 2: 1rem, 3: 1.5rem, 4: 2rem, 5: 4rem );
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Custom Spacing Scale Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> <style> /* Custom spacing scale by overriding CSS variables */ :root { --bs-spacer: 2rem; } .custom-margin { margin-bottom: var(--bs-spacer); background-color: #d1e7dd; padding: 1rem; border-radius: 0.25rem; } </style> </head> <body> <main class="container mt-4"> <div class="custom-margin">Box with custom bottom margin (2rem)</div> <div class="custom-margin">Another box with same spacing</div> </main> </body> </html>
$spacer variable in Bootstrap SCSS affect?$spacer$spacer variable sets the base size for spacing in Bootstrap, like margins and paddings.$spacer is modified$spacer changes the size unit used in spacing utilities, affecting all related classes.$spacer controls spacing size [OK]--variable-name inside selectors like :root.--bs-spacer as the CSS variable for spacing, so overriding it in :root is correct.:root { --bs-spacer: 3rem; }class="m-2" in Bootstrap?--bs-spacer divided by 4 for each step. For m-2, margin is 0.5 * $spacer.--bs-spacer: 3rem, m-2 margin = 3rem * 0.5 = 1.5rem.$spacer: 3rem; in your CSS file but it didn't work. What is the likely reason?$spacer is a SCSS variable used during compilation, not valid in CSS files.--bs-spacer.$spacer in CSS fails because it's invalid SCSS syntax (A) and the incorrect variable name for CSS (D).$spacers as a map with keys for each step.(0: 0, 1: 0.25rem, ...).Pass the test and this pattern moves unknown