Performance: Starts-with and ends-with selectors
These selectors affect how quickly the browser matches elements to styles during rendering.
Jump into concepts and practice - no test required
.btn-primary { color: blue; } .active { font-weight: bold; }
[class^='btn-'] { color: blue; } [class$='-active'] { font-weight: bold; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| [attr^='value'] or [attr$='value'] | Many attribute checks per element | 0 (style only) | Low to medium (style recalculation) | [!] OK |
| .class or #id selectors | Fast direct lookup | 0 | Low | [OK] Good |
href attribute that starts with "https"?a[href^="https"] matches all anchor tags with href attributes starting with "https".[class$="-btn"]. The dot (.) is for class names, not attribute selectors.<a href="https://example.com">Link1</a> <a href="http://example.com">Link2</a> <a href="https://secure.com">Link3</a>
a[href^="https"] { color: red; }?a[href^="https"] styles only elements whose href attribute starts with "https". So Link1 and Link3 get styled.input[name^="user"] { background-color: yellow; }<input name="login"> <input name="signup"> <input name="emailuser">
img elements whose src attribute starts with "https://cdn." and ends with ".png". Which CSS selector correctly targets these images?img[src^="https://cdn."][src$=".png"].