Performance: Writing reusable CSS
This affects page load speed and rendering efficiency by reducing CSS size and minimizing style recalculations.
Jump into concepts and practice - no test required
.btn { padding: 1rem; border-radius: 0.5rem; color: white; } .btn-primary { background-color: blue; } .btn-secondary { background-color: gray; }
.btn-primary { background-color: blue; color: white; padding: 1rem; border-radius: 0.5rem; } .btn-secondary { background-color: gray; color: white; padding: 1rem; border-radius: 0.5rem; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated styles in multiple classes | No extra DOM nodes | Multiple reflows due to repeated style recalculations | Higher paint cost due to inefficient styles | [X] Bad |
| Shared reusable classes for common styles | No extra DOM nodes | Single reflow for shared styles | Lower paint cost with efficient style application | [OK] Good |
button?.button which is correct. #button { color: blue; } uses an ID selector (#), C uses an element selector, and D uses an invalid selector..red { color: red; } .bold { font-weight: bold; }Hello World
red and bold. The red class sets text color to red, and bold sets font weight to bold.button { background-color: blue; } .button { background-color: red; }Click me
button targets all <button> elements, while .button targets elements with class "button"..button applies. The similar names can confuse developers.button and class .button conflict causing confusion. -> Option B.card is correct. #card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; } uses an ID selector (#), C uses an invalid element selector, and D uses incorrect CSS properties.box-shadow, padding, and border-radius are correct CSS properties. .card { shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; rounded: 0.5rem; } uses invalid properties shadow and rounded.