0
0
CSSmarkup~8 mins

Starts-with and ends-with selectors in CSS - Performance & Optimization

Choose your learning style9 modes available
Performance: Starts-with and ends-with selectors
MEDIUM IMPACT
These selectors affect how quickly the browser matches elements to styles during rendering.
Styling elements based on attribute prefixes or suffixes
CSS
.btn-primary { color: blue; } .active { font-weight: bold; }
Using simple class selectors lets the browser quickly match styles using fast lookup.
📈 Performance Gainstyle matching is near-instant, reducing LCP and improving responsiveness
Styling elements based on attribute prefixes or suffixes
CSS
[class^='btn-'] { color: blue; } [class$='-active'] { font-weight: bold; }
These selectors require the browser to check attribute values character-by-character for many elements, causing slower style matching.
📉 Performance Costtriggers slower style calculation on large DOMs, increasing LCP by tens of milliseconds
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
[attr^='value'] or [attr$='value']Many attribute checks per element0 (style only)Low to medium (style recalculation)[!] OK
.class or #id selectorsFast direct lookup0Low[OK] Good
Rendering Pipeline
Starts-with and ends-with selectors are evaluated during the Style Calculation stage, where the browser matches CSS rules to DOM elements by checking attribute values.
Style Calculation
⚠️ BottleneckStyle Calculation is slower because attribute substring matching is more complex than simple class or ID matching.
Core Web Vital Affected
LCP
These selectors affect how quickly the browser matches elements to styles during rendering.
Optimization Tips
1Avoid overusing starts-with and ends-with selectors on large DOMs.
2Prefer simple class or ID selectors for faster style matching.
3Use DevTools Performance panel to spot slow style recalculations.
Performance Quiz - 3 Questions
Test your performance knowledge
Why can starts-with ([attr^='value']) selectors slow down page rendering?
ABecause they increase the number of DOM nodes
BBecause they require checking attribute values character-by-character for many elements
CBecause they cause layout shifts
DBecause they block network requests
DevTools: Performance
How to check: Record a performance profile while loading the page. Look for long Style Calculation times in the summary.
What to look for: High time spent in 'Style Recalculation' indicates expensive selectors like starts-with or ends-with.