Back to JavaScript Performance

Batch DOM CSS Changes

javascriptdomcssperformancereflow
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

Browsers perform a costly operation called reflow whenever the geometric properties or layout of a DOM element change. When developers modify inline styles sequentially—such as setting height, then width, then padding—the browser may force a synchronous recalculation for every property update. This skill optimizes render performance by batching these mutations into a single operation. By applying CSS classes or rewriting the cssText property in one execution, the engine minimizes style recalculation cycles and layout thrashing. This approach ensures the browser performs only one reflow per update, significantly improving frame rates during animations or complex UI transitions. Prioritizing stylesheet updates over individual style object properties maintains a cleaner separation between visual presentation and application logic while reducing main-thread overhead during interaction-heavy tasks.

When to Use This Skill

  • Dynamic animation sequences that update multiple box model properties
  • Switching complex themes or state-based layouts
  • Batching style changes triggered by rapid scroll or mouse move events
  • Transitioning elements between visible and hidden states involving multiple dimensions

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • Batch my DOM style changes to fix layout thrashing
  • Convert my inline style updates to a single CSS class
  • Optimize sequential element style updates in my code
  • Help me refactor individual style assignments to reduce reflows
  • Clean up messy inline styles into performant CSS declarations

What this skill does

  • Reduces document reflow counts during style updates
  • Consolidates multiple CSS property changes into single browser paint cycles
  • Automates migration from fragmented inline style blocks to class-based toggles
  • Wraps sequential style mutations into single cssText assignments
  • Optimizes React component rendering by eliminating direct style object manipulation

When not to use it

  • When updating a single, isolated CSS property that changes infrequently
  • When runtime-computed variables (like mouse coordinates) must be mapped to styles per-frame

Example workflow

  1. Analyze current codebase for sequential element.style assignments
  2. Group related visual properties into a descriptive CSS class
  3. Replace individual JavaScript style assignments with classList.add or toggle
  4. Refactor fallback logic to use cssText if static classes are insufficient
  5. Verify performance gain using browser developer tool rendering metrics

Prerequisites

  • Existing DOM-heavy JavaScript or TypeScript project
  • Access to CSS files or style blocks

Pitfalls & limitations

  • !Overwriting existing inline styles when assigning to cssText
  • !Creating CSS specificity conflicts by mixing classes and inline styles
  • !Difficulty managing dynamic values that cannot be pre-defined in a class

FAQ

Why is changing styles one by one slow?
Every modification to layout properties triggers a browser reflow; sequential changes force the browser to recalculate the document structure multiple times.
Is cssText better than classList?
classList is preferred for static styling, while cssText is more appropriate for dynamic values that must be calculated at runtime but should still be applied together.
Does this impact React component performance?
Yes, using classes instead of direct DOM manipulation in refs prevents unnecessary manual reconciliation and aligns with the declarative nature of the framework.

How it compares

While manual coding often results in scattered, reactive-style property setting, this skill systematically enforces batching patterns that ensure structural integrity and maximum browser efficiency.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## Batch DOM CSS Changes

Avoid changing styles one property at a time. Group multiple CSS changes together via classes or cssText to minimize browser reflows.

**Incorrect (multiple reflows):**

function updateElementStyles(element: HTMLElement) {
// Each line triggers a reflow
element.style.width = '100px'
element.style.height = '200px'
element.style.backgroundColor = 'blue'
element.style.border = '1px solid black'
}


**Correct (add class - single reflow):**

// CSS file
.highlighted-box {
width: 100px;
height: 200px;
background-color: blue;
border: 1px solid black;
}

// JavaScript
function updateElementStyles(element: HTMLElement) {
element.classList.add('highlighted-box')
}


**Correct (change cssText - single reflow):**

function updateElementStyles(element: HTMLElement) {
element.style.cssText =
width: 100px;
height: 200px;
background-color: blue;
border: 1px solid black;

}


**React example:**

// Incorrect: changing styles one by one
function Box({ isHighlighted }: { isHighlighted: boolean }) {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
if (ref.current && isHighlighted) {
ref.current.style.width = '100px'
ref.current.style.height = '200px'
ref.current.style.backgroundColor = 'blue'
}
}, [isHighlighted])

return <div ref={ref}>Content</div>
}

// Correct: toggle class
function Box({ isHighlighted }: { isHighlighted: boolean }) {
return (
<div className={isHighlighted ? 'highlighted-box' : ''}>
Content
</div>
)
}


Prefer CSS classes over inline styles when possible. Classes are cached by the browser and provide better separation of concerns.

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/js-batch-dom-css/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/vercel-labs/agent-skills/js-batch-dom-css/SKILL.md
  • Cursor: ~/.cursor/skills/vercel-labs/agent-skills/js-batch-dom-css/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/vercel-labs/agent-skills/js-batch-dom-css/SKILL.md

🚀 Install with CLI:
npx skills add vercel-labs/agent-skills

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid javascript performance issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under JavaScript Performance and is published by Vercel Engineering, maintained in vercel-labs/agent-skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.