Performance: Before pseudo-element
This affects the rendering performance by adding extra elements to the DOM visually, which can increase paint and composite work.
Jump into concepts and practice - no test required
p::before { content: ''; display: inline-block; width: 200px; height: 200px; background-image: url('small-icon.svg'); background-size: contain; background-repeat: no-repeat; }
p::before { content: url('large-image.png'); display: inline-block; width: 200px; height: 200px; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large image in ::before | No extra DOM nodes, but visual layer added | 1 reflow if size not fixed | High paint cost due to large image | [X] Bad |
| Small SVG background in ::before | No extra DOM nodes, visual layer added | No reflow with fixed size | Low paint cost with vector image | [OK] Good |
::before pseudo-element do?::before::before pseudo-element inserts content before the main content of an element without modifying the HTML structure.::before. It adds content after the element's main content. describes ::after, not ::before.::before adds content before = A [OK]::before?content property is mandatory to show anything with ::before. Without it, no content appears.display, position, and visibility affect layout or visibility but do not create content.p::before { content: "Hello "; }<p>Given HTML: <p>World</p>::before adds "Hello " before the paragraph's original text "World".h1::before { content; "Note: "; color: red; }content; "Note: " which is incorrect. It should be content: "Note: " with a colon.h1::before is correct, color can be used, and content can be any string, so other options are wrong.::before to add a red asterisk (*) before all required form labels for accessibility?label.required::before to add content only before labels with class "required".content: "*" and color: red to show a red star before the label text for accessibility.