Performance: ID selectors
ID selectors affect how quickly the browser matches CSS rules to elements during style calculation.
Jump into concepts and practice - no test required
#header { color: blue; } #footer { color: blue; } #sidebar { color: blue; }
#header, #footer, #sidebar { color: blue; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using #id selector | Single element matched quickly | 0 | Low | [OK] Good |
| Using multiple ID selectors in one rule | Multiple single element matches | 0 | Low | [OK] |
| Overusing IDs for styling many elements | Inefficient CSS management | 0 | Low | [!] |
#header { color: blue; }# symbol in CSS is used to select an element by its unique ID attribute.main-content in CSS?# symbol followed by the ID name without spaces.#main-content { } to select the element with ID "main-content".<div id="box">Hello</div>
#box { color: red; } .box { color: blue; }#box and a class selector .box.#box selector applies, setting color to red. The class selector does not apply.nav is not styled?#nav { background-color: yellow color: white };. Omitting semicolons between properties causes errors.#nav { background-color: yellow color: white } is missing a semicolon after yellow. This can cause some browsers to ignore the rule.footer to have a green background and white text. Which CSS code correctly does this and ensures no other elements are affected?#idname. Here, #footer targets the element with ID "footer".background-color: green; and color: white; style the background and text color. Using #footer ensures only that element is styled.