:root selector for scoped CSS
See original GitHub issueWhat 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:
- Created 4 years ago
- Reactions:95
- Comments:16
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1024 for this one!
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!