HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcssTip

What's the difference between :root and html in CSS?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
whatandbetweencssdifferencetheroothtml

Problem

CSS has two ways to target the root element of an HTML document - the :root pseudo-class and the html selector. While these are very similar to each other, they have a couple of differences you should know.
The :root selector has a higher specificity than the html selector. This is because :root is a pseudo-class selector, while html is a type selector.
CSS can be used to style other types of documents, apart from HTML. This is where the :root element comes in to play, allowing you to style the root element of a document. This can be especially important when styling SVG documents, where the html selector will not work.

Solution

:root {
  background-color: red;
}

html {
  background-color: blue;
}

/* The HTML document's root element will have a red background-color.  */


CSS can be used to style other types of documents, apart from HTML. This is where the :root element comes in to play, allowing you to style the root element of a document. This can be especially important when styling SVG documents, where the html selector will not work.

Code Snippets

:root {
  background-color: red;
}

html {
  background-color: blue;
}

/* The HTML document's root element will have a red background-color.  */

Context

From 30-seconds-of-code: root-vs-html

Revisions (0)

No revisions yet.