Performance: Why utility classes speed development
Utility classes reduce CSS file size and speed up rendering by reusing small, atomic styles instead of large custom CSS blocks.
<div class="p-4 text-primary m-4">Content</div>
.custom-padding { padding: 1rem; } .custom-color { color: #007bff; } .custom-margin { margin: 1rem; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Custom CSS classes for spacing/colors | Low | Multiple reflows if styles change | Moderate | [X] Bad |
| Bootstrap utility classes (e.g., p-4, m-4, text-primary) | Low | Single reflow | Low | [OK] Good |
m for margin and t for top, combined as mt-3.mt-3 matches Bootstrap's utility class naming convention for margin-top spacing.<button class='btn btn-primary p-4'>Click Me</button>
btn-primary applies the primary color style, and p-4 adds padding all around the button.<div class='p-3 mt-5x text-center'>Content</div>
p-3 is valid for padding, text-center is valid for text alignment, but mt-5x is not a valid Bootstrap class.mt-5 without the extra 'x'.d-flex makes a flex container, flex-column stacks children vertically, and flex-lg-row changes direction to row on large screens.Pass the test and this pattern moves unknown