🎨 HTML / CSS Intermediate

What is the :root pseudo-class in CSS and what is it used for?

Answer

The :root pseudo-class matches the root element of the document, which in HTML is the <html> element. It has higher specificity than the html element selector. Its primary use is declaring CSS custom properties (variables) that are globally accessible throughout the stylesheet: :root { --primary: #3490dc; --radius: 8px; }. Variables declared on :root are available to every element. It is also a convenient place for global resets and font size definitions. Many design systems and component libraries declare all their design tokens on :root, enabling easy theming by overriding these variables.