question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

:root selector for scoped CSS

See original GitHub issue

What problem does this feature solve?

Often when I’m writing scoped CSS for my components I need to put a class on the root element so that I can style it. For example:

<template>
    <div class="foo-root">Hello World</div>
</template>

<style scoped>
    .foo-root {
        color: red
    }
</style>

What does the proposed API look like?

CSS already supports a :root selector which means the “root HTML element of the document” (i.e. usually the <html> element).

As far as I know it’s never possible for a component to be the root <html> element of the document. So this :root selector effectively does nothing inside scoped CSS.

Therefore, why not repurpose it to mean the component’s root element? We could then re-write the example above without needing to add a specific class:

<template>
    <div>Hello, World</div>
<template>

<style scoped>
    :root {
        color: red;
    }
</style>

Admittedly a small idea, but one that I think would save a fair bit of typing (and inventing class names)!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:95
  • Comments:16

github_iconTop GitHub Comments

36reactions
bpolaszekcommented, Dec 19, 2019

+1024 for this one!

24reactions
jfbrennancommented, Apr 13, 2020

Guys :root really means <html> and that meaning is very much cemented. Vue shouldn’t mess around with that.

:host is closer to what you’re looking for and it’s what Riot.js uses for this same use case, so support for that in Vue would be much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

root - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
The :root CSS pseudo-class matches the root element of a tree representing the document. In HTML, :root represents the <html> element and is...
Read more >
Scoped CSS - Vue Loader
# Scoped CSS · # Mixing Local and Global Styles · # Child Component Root Elements · # Deep Selectors · # Dynamically...
Read more >
Vue.js Scoped CSS - how to match root element?
I have the following components: Component 1: <style scoped> ...
Read more >
CSS variables: Scoping - LogRocket Blog
CSS variables declared in the :root selector are said to be in the Global scope. This means that they can be accessed anywhere...
Read more >
Sass: @at-root
The @at-root rule is usually written @at-root <selector> { ... } and causes everything within it to be emitted at the root of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found