Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS for Performance – Avoidi | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 48 of 50

·css

CSS for Performance – Avoiding Layout Thrash

9 mins

Course progress96%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `contain` tells the browser the element’s layout, style, and paint are self‑contained, reducing repaint cost.
  • Animate only `transform`/`opacity` for smooth 60 fps experience.
  • Minimize the number of **recalculations** in scroll‑heavy pages.

A layout thrash happens when JavaScript reads a layout‑dependent property (e.g., offsetHeight) and then writes a style that forces the browser to re‑calculate the layout. Repeating this each frame kills performance.

Best Practices to Eliminate Thrashing

  1. Batch reads & writes using requestAnimationFrame.
    let latestWidth;
    function read() { latestWidth = element.offsetWidth; }
    function write() { element.style.setProperty('--width', `${latestWidth}px`); }
    requestAnimationFrame(() => { read(); write(); });
    
  2. Animate only transform & opacity – they stay on the compositor layer.
  3. Use the contain property to tell the browser the element is self‑contained.
    .widget { contain:layout style paint; }
    
    This prevents changes inside .widget from triggering re‑flows elsewhere.
  4. Avoid setting width:100% on elements that also have margin or border if box-sizing:border-box isn’t applied.
  5. Debounce scroll‑related handlers (e.g., infinite‑scroll) with requestAnimationFrame or a small timeout.

Example – Sticky Header with contain

header {
  position:sticky; top:0; background:#fff; z-index:10;
  contain:layout;          /* the header’s size never influences the rest */
}

Pro Tip – Use CSS‑only Animation for simple motion

If an animation can be expressed with @keyframes on transform/opacity, let the browser handle it – it runs on the compositor without JavaScript.

Measuring Layout Thrash

Open Chrome DevTools → Performance tab → Record a scroll or interaction. Look for “Recalculate Style” and “Layout” spikes. If you see many of them per frame, refactor the code.

Accessibility Note

Even when you minimise layout work, always keep focus management reliable; fast animation should not hide focus states.

Real‑World Checklist

  • ✅ All animated properties are transform or opacity.
  • ✅ contain is applied to isolated widgets (modals, carousels).
  • ✅ JavaScript reads DOM measurements once per frame.
  • ✅ Heavy background images use object-fit:cover and are appropriately sized.

Browser Support

contain is supported in Chrome 71+, Edge 79+, Safari 12+, and Firefox 44+ (partial). Use a fallback (overflow:hidden) for older browsers if needed.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Grid + Flexbox Hybrid LayoutsContinue to lesson 49CSS Testing – Visual Regression & LintingBack to CSS Basics to Advanced curriculum