Performance: Display utilities
Display utilities affect how elements are shown or hidden, impacting layout calculation and rendering speed.
<div class="d-none" id="content">Content</div> <button id="btn">Show</button> <script> document.getElementById('btn').addEventListener('click', () => { document.getElementById('content').classList.toggle('d-none'); }); </script>
<div style="display:none;">Content</div> <button id="btn">Show</button> <script> document.getElementById('btn').addEventListener('click', () => { const el = document.querySelector('div'); el.style.display = 'block'; }); </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline style display toggle | Direct style changes on element | 1 reflow per toggle | Medium paint cost | [X] Bad |
| Bootstrap display utility class toggle | Class attribute toggle | 1 reflow per toggle | Medium paint cost | [OK] Good |
d-none do to an element?d-none class purposed-none class in Bootstrap sets the CSS property display: none; which hides the element completely.d-none applies to all screen sizes, hiding the element everywhere.d-none = hide element always [OK]d-none hides the element on all screens, and d-md-block shows it as block starting from medium screens and up.d-none d-md-block hides the element on small and extra small screens, then shows it on medium and larger.<div class="d-none d-lg-block">Hello</div>
d-none and d-lg-blockd-none hides the element on all screen sizes by default. d-lg-block overrides this to show the element as block starting at large screens (lg) and above.d-none d-sm-block hides the element on all screens by default, then shows it from small screens and up. This means it is hidden only on extra small screens, not small screens.d-none hides on all by default (xs and sm included). d-md-block shows block from medium and up (md, lg, xl). d-xl-inline overrides to inline only on extra large screens.Pass the test and this pattern moves unknown