Performance: Class attribute
The class attribute affects CSS selector matching and DOM updates, impacting rendering speed and style recalculations.
<div class="btn btn-primary btn-large"></div>
<div class="btn btn-primary btn-large btn-primary btn-large"></div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated class names | No extra DOM nodes | Multiple style recalculations | Higher paint cost due to style thrashing | [X] Bad |
| Unique, simple class names | No extra DOM nodes | Single style recalculation | Lower paint cost with stable styles | [OK] Good |
class attribute in HTML?class attribute is used to assign one or more class names to an HTML element.<div class="red bold"></div> uses a space between "red" and "bold", which is correct. Others use commas, semicolons, or dots which are invalid.<style>
.red { color: red; }
.bold { font-weight: bold; }
</style>
<p class="red bold">Hello World</p><div class=red bold>Content</div>
class=red bold lacks quotes, so the browser misinterprets it.primary should also be bold. Which HTML and CSS setup achieves this?<button class="btn primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } correctly assigns two classes separated by space and styles them separately. <button class="btn,primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } uses comma in class attribute which is invalid. <button class="btn primary">Click</button>\n.btn.primary { background: green; color: white; font-weight: bold; } combines styles but makes all primary buttons green and bold, missing the separate "btn" style. <button class="primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } misses "btn" class on button, so green background won't apply.Pass the test and this pattern moves unknown