0
0
Bootsrapmarkup~8 mins

Display utilities in Bootsrap - Performance & Optimization

Choose your learning style9 modes available
Performance: Display utilities
MEDIUM IMPACT
Display utilities affect how elements are shown or hidden, impacting layout calculation and rendering speed.
Toggling element visibility on user interaction
Bootsrap
<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>
Using Bootstrap's display utility classes leverages CSS for visibility toggling, minimizing inline style changes and reflows.
📈 Performance GainSingle reflow per toggle with efficient CSS class changes, reducing layout thrashing.
Toggling element visibility on user interaction
Bootsrap
<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>
Directly manipulating inline styles causes layout recalculation and can trigger multiple reflows if done repeatedly.
📉 Performance CostTriggers 1 reflow per toggle, causing layout thrashing if toggled frequently.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Inline style display toggleDirect style changes on element1 reflow per toggleMedium paint cost[X] Bad
Bootstrap display utility class toggleClass attribute toggle1 reflow per toggleMedium paint cost[OK] Good
Rendering Pipeline
Display utilities change the CSS display property, affecting the Layout and Paint stages by showing or hiding elements.
Style Calculation
Layout
Paint
⚠️ BottleneckLayout stage is most expensive because changing display affects element size and position.
Core Web Vital Affected
CLS
Display utilities affect how elements are shown or hidden, impacting layout calculation and rendering speed.
Optimization Tips
1Avoid inline style changes for display toggling; prefer CSS classes.
2Changing display triggers layout recalculation, so minimize toggles.
3Use Bootstrap display utilities to leverage optimized CSS for visibility.
Performance Quiz - 3 Questions
Test your performance knowledge
Which method is more efficient for toggling element visibility?
ARemoving and re-adding the element from the DOM
BToggling Bootstrap display utility classes
CChanging inline style display property directly
DUsing visibility: hidden CSS property
DevTools: Performance
How to check: Record a performance profile while toggling display. Look for layout and paint events in the flame chart.
What to look for: Frequent layout recalculations indicate costly display changes; fewer layout events mean better performance.