{"id":2788,"date":"2026-03-08T06:12:05","date_gmt":"2026-03-08T11:12:05","guid":{"rendered":"https:\/\/izendestudioweb.com\/articles\/?p=2788"},"modified":"2026-03-08T06:12:05","modified_gmt":"2026-03-08T11:12:05","slug":"all-the-different-ways-to-select-the-html-element-in-css","status":"publish","type":"post","link":"https:\/\/izendestudioweb.com\/articles\/2026\/03\/08\/all-the-different-ways-to-select-the-html-element-in-css\/","title":{"rendered":"All the Different Ways to Select the &lt;html&gt; Element in CSS"},"content":{"rendered":"<p>The <strong>&lt;html&gt;<\/strong> element sits at the root of every web page, yet in many projects it is barely touched in CSS. Most developers simply use the plain <strong>html<\/strong> selector and move on. However, there are several other, lesser-known ways to target this root element\u2014some practical, some mostly academic, but all useful to understand if you care about robust CSS architecture.<\/p>\n<p>This article walks through the various ways to select the <strong>&lt;html&gt;<\/strong> element in CSS, explains when they may be appropriate, and highlights the implications for modern, standards-based web design.<\/p>\n<h2>Key Takeaways<\/h2>\n<ul>\n<li>The <strong>html<\/strong> element can be targeted via simple element selectors, pseudo-classes, and attribute-based selectors.<\/li>\n<li>Some methods are more explicit than others, but they typically end up matching the same root element.<\/li>\n<li>Choosing the right selector impacts maintainability, readability, and cross-browser behavior.<\/li>\n<li>Consistent use of root-level selectors is important for typography, layout scaling, theming, and accessibility.<\/li>\n<\/ul>\n<hr>\n<h2>Why Target the &lt;html&gt; Element at All?<\/h2>\n<p>Before reviewing the different selection techniques, it is useful to clarify why you would target the <strong>&lt;html&gt;<\/strong> element in the first place. For many teams, root-level styling controls global behavior and provides a foundation for the entire design system.<\/p>\n<p>Common use cases include:<\/p>\n<ul>\n<li>Setting a base <strong>font-size<\/strong> to drive <em>rem<\/em>-based typography.<\/li>\n<li>Defining default <strong>scroll-behavior<\/strong> or <strong>background<\/strong> for the page.<\/li>\n<li>Applying <strong>color-scheme<\/strong> or theme variables used throughout the site.<\/li>\n<li>Managing <strong>box-sizing<\/strong> strategies (often in combination with the <strong>*<\/strong> universal selector).<\/li>\n<\/ul>\n<blockquote>\n<p>The way you style the <strong>&lt;html&gt;<\/strong> element often defines how predictable and scalable the rest of your CSS will be.<\/p>\n<\/blockquote>\n<hr>\n<h2>1. The Classic Element Selector<\/h2>\n<h3>Direct Element Selector: html<\/h3>\n<p>The most familiar and straightforward way to target the root element is simply:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>html { font-size: 16px; }<\/p>\n<p>This selector directly matches the <strong>&lt;html&gt;<\/strong> tag in the DOM. It is universally supported, instantly readable, and should be your default for most use cases.<\/p>\n<p>Use this approach when you need global defaults, such as:<\/p>\n<ul>\n<li><strong>Typography:<\/strong> html { font-size: 100%; }<\/li>\n<li><strong>Smooth scrolling:<\/strong> html { scroll-behavior: smooth; }<\/li>\n<li><strong>Base theme colors:<\/strong> html { color-scheme: light dark; }<\/li>\n<\/ul>\n<hr>\n<h2>2. Using Pseudo-Classes on the &lt;html&gt; Element<\/h2>\n<p>Pseudo-classes let you refine when and how the <strong>&lt;html&gt;<\/strong> element is styled based on states or environmental conditions. These techniques are especially useful for accessibility and user-preference-driven design.<\/p>\n<h3>:root Pseudo-Class<\/h3>\n<p>The <strong>:root<\/strong> pseudo-class selects the root element of the document. In HTML documents, <strong>:root<\/strong> and <strong>html<\/strong> match the same node, but <strong>:root<\/strong> is conceptually more aligned with using CSS custom properties and design tokens.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>:root { &#8211;brand-color: #0052cc; &#8211;font-base: 1rem; }<\/p>\n<p>Reasons to use <strong>:root<\/strong> instead of <strong>html<\/strong>:<\/p>\n<ul>\n<li>Improved semantics when defining <strong>CSS variables<\/strong>.<\/li>\n<li>Consistency across different document types (e.g., SVG, XML) where the actual root element name may vary.<\/li>\n<li>Clear intent that you are working at the topmost level of the cascade.<\/li>\n<\/ul>\n<h3>:lang() on the Root<\/h3>\n<p>The <strong>:lang()<\/strong> pseudo-class lets you conditionally style the root based on the page language, typically defined on <strong>&lt;html lang=&#8221;&#8230;&#8221;&gt;<\/strong>. This is particularly valuable for internationalization and typography.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>:root:lang(en) { font-family: system-ui, -apple-system, &#8220;Segoe UI&#8221;, sans-serif; }<\/p>\n<p>:root:lang(ja) { font-family: &#8220;Hiragino Kaku Gothic ProN&#8221;, &#8220;Meiryo&#8221;, sans-serif; }<\/p>\n<p>While you can write <strong>html:lang(en)<\/strong>, many developers prefer <strong>:root:lang(en)<\/strong> to emphasize that they are styling the entire document context.<\/p>\n<h3>Preference and State Pseudo-Classes<\/h3>\n<p>Modern CSS also enables environment-aware styling via pseudo-classes such as <strong>:has()<\/strong> (where supported) and media queries that affect root-level rules. While not direct selectors like <strong>html<\/strong>, they often end up being applied to the root.<\/p>\n<p><strong>Example with prefers-reduced-motion:<\/strong><\/p>\n<p>@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }<\/p>\n<p>This combination ensures that your site respects user preferences across the entire page, starting at the root.<\/p>\n<hr>\n<h2>3. Attribute Selectors Targeting the &lt;html&gt; Element<\/h2>\n<p>Attribute selectors can target <strong>&lt;html&gt;<\/strong> based on its attributes, such as <strong>lang<\/strong>, <strong>dir<\/strong>, or custom data attributes used for theming or application state.<\/p>\n<h3>Language-Based Attribute Selectors<\/h3>\n<p>You can match the document language directly using attribute selectors on the <strong>html<\/strong> element:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>html[lang=&#8221;en&#8221;] { font-size: 100%; }<\/p>\n<p>html[lang^=&#8221;fr&#8221;] { font-family: &#8220;Helvetica Neue&#8221;, Arial, sans-serif; }<\/p>\n<p>This approach is similar to <strong>:lang()<\/strong> but uses pure attribute matching. It can be useful when you need more complex patterns, such as partial matches for language codes (e.g., <strong>fr-CA<\/strong>, <strong>fr-FR<\/strong>).<\/p>\n<h3>Directionality: [dir] and [dir=&#8221;rtl&#8221;]<\/h3>\n<p>For right-to-left (RTL) languages, styling the root based on text direction is essential:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>html[dir=&#8221;rtl&#8221;] { direction: rtl; }<\/p>\n<p>html[dir=&#8221;rtl&#8221;] body { text-align: right; }<\/p>\n<p>This allows layout and typography adjustments to cascade from the root element based on the document direction.<\/p>\n<h3>Data Attributes for Theme or Mode<\/h3>\n<p>Many modern applications toggle themes by setting a <strong>data-*<\/strong> attribute on the <strong>html<\/strong> element. Attribute selectors then drive global theming:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>html[data-theme=&#8221;dark&#8221;] { color-scheme: dark; background-color: #0b0c10; }<\/p>\n<p>html[data-theme=&#8221;light&#8221;] { color-scheme: light; background-color: #ffffff; }<\/p>\n<p>This pattern is common in design systems and SPAs where JavaScript updates the root attribute to change modes without reloading the page.<\/p>\n<hr>\n<h2>4. Structural Relationships Involving &lt;html&gt;<\/h2>\n<p>Because <strong>&lt;html&gt;<\/strong> is the root, it has no parent element in the document tree. This limits some structural selectors (like parent or ancestor selectors). However, you can still reference <strong>html<\/strong> in certain relational ways that clarify your intent.<\/p>\n<h3>Descendant Context: html body<\/h3>\n<p>While this does not select <strong>&lt;html&gt;<\/strong> itself, it is frequently used alongside root styling to define a clear hierarchy:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>html { font-size: 100%; }<\/p>\n<p>html body { margin: 0; min-height: 100vh; }<\/p>\n<p>Using <strong>html body<\/strong> communicates that these rules apply to the body in the context of the document root, which can improve readability in larger codebases.<\/p>\n<h3>Universal Selector with :root<\/h3>\n<p>When normalizing <strong>box-sizing<\/strong>, a common pattern starts at <strong>:root<\/strong> and then extends to all elements:<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>:root { box-sizing: border-box; }<\/p>\n<p>*, *::before, *::after { box-sizing: inherit; }<\/p>\n<p>Here, <strong>:root<\/strong> is effectively selecting <strong>&lt;html&gt;<\/strong> and establishing a baseline that all other elements inherit. This is more explicit than styling <strong>html<\/strong> alone and reflects best practices for consistent layout behavior.<\/p>\n<hr>\n<h2>5. Browser and Standards Considerations<\/h2>\n<p>In modern browsers, <strong>html<\/strong> and <strong>:root<\/strong> are functionally equivalent in HTML documents. However, there are subtle considerations to keep in mind for long-term maintainability and cross-technology compatibility.<\/p>\n<h3>Consistency Across Document Types<\/h3>\n<p>If your stack includes technologies like inline SVG or custom document formats, the actual root element name may differ. In such cases, <strong>:root<\/strong> remains consistent, while <strong>html<\/strong> does not exist.<\/p>\n<p>For organizations with design systems shared between web apps, documentation sites, and embedded content, standardizing on <strong>:root<\/strong> for variables and core configuration can simplify CSS reuse.<\/p>\n<h3>Readability and Intent<\/h3>\n<p>Choosing between <strong>html<\/strong>, <strong>:root<\/strong>, and attribute selectors is often less about capability and more about intent:<\/p>\n<ul>\n<li>Use <strong>html<\/strong> for straightforward, global page styles.<\/li>\n<li>Use <strong>:root<\/strong> when defining system-level variables or behaviors.<\/li>\n<li>Use <strong>html[&#8230;]<\/strong> or <strong>:root:lang(&#8230;)<\/strong> when the styling is conditional on document attributes.<\/li>\n<\/ul>\n<p>Clear patterns help your team and future developers understand why a particular selector was used and what scope of impact it has.<\/p>\n<hr>\n<h2>Conclusion<\/h2>\n<p>There are multiple ways to select the <strong>&lt;html&gt;<\/strong> element in CSS, ranging from the simple <strong>html<\/strong> selector to more expressive options like <strong>:root<\/strong> and attribute-based selectors. While many of these approaches ultimately target the same node, the choice of selector communicates how you intend to use the root element in your design system.<\/p>\n<p>For most projects, a blend of <strong>html<\/strong> for core global rules, <strong>:root<\/strong> for CSS variables, and attribute selectors for theming or localization strikes the right balance between clarity, flexibility, and maintainability. Understanding these options gives both business owners and developers greater control over how their sites behave at the most fundamental level.<\/p>\n<hr>\n<div class=\"cta-box\" style=\"background: #f8f9fa; border-left: 4px solid #007bff; padding: 20px; margin: 30px 0;\">\n<h3 style=\"margin-top: 0;\">Need Professional Help?<\/h3>\n<p>Our team specializes in delivering enterprise-grade solutions for businesses of all sizes.<\/p>\n<p>  <a href=\"https:\/\/izendestudioweb.com\/services\/\" style=\"display: inline-block; background: #007bff; color: white; padding: 12px 24px; text-decoration: none; border-radius: 4px; font-weight: bold;\"><br \/>\n    Explore Our Services \u2192<br \/>\n  <\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>All the Different Ways to Select the &lt;html&gt; Element in CSS<\/p>\n<p>The &lt;html&gt; element sits at the root of every web page, yet in many projects it is b<\/p>\n","protected":false},"author":1,"featured_media":2787,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[29,34,125],"class_list":["post-2788","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-design","tag-design","tag-development","tag-frontend"],"jetpack_featured_media_url":"https:\/\/izendestudioweb.com\/articles\/wp-content\/uploads\/2026\/03\/unnamed-file-12.png","_links":{"self":[{"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/posts\/2788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/comments?post=2788"}],"version-history":[{"count":1,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/posts\/2788\/revisions"}],"predecessor-version":[{"id":2798,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/posts\/2788\/revisions\/2798"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/media\/2787"}],"wp:attachment":[{"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/media?parent=2788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/categories?post=2788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/izendestudioweb.com\/articles\/wp-json\/wp\/v2\/tags?post=2788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}