prevent tags from being used without classes in scoped style definitions
See original GitHub issueObserved behavior
In some places in the codebase, we do things like
<template>
<div>
<h2>Title</h2>
<p>paragraph</p>
</div>
</template>
<style scoped>
h2 {
margin-top: 8px;
}
</style>
This has a few issues:
- According to the vue docs, using tags inside scoped style blocks “will be many times slower”
- The DOM structure is coupled to the styling. For example, turning
h2
toh3
would require changes on multiple lines
Expected behavior
We should add a stylelint rule to prevent this behavior, and update all existing instances to either use an inline style or a class.
Inline style:
<template>
<div>
<h2 style="margin-top: 8px;">Title</h2>
<p>paragraph</p>
</div>
</template>
Class:
<template>
<div>
<h2 class="title">Title</h2>
<p>paragraph</p>
</div>
</template>
<style scoped>
.title {
margin-top: 8px;
}
</style>
User-facing consequences
- slower render performance
- code maintainability
Context
0.13.x
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Saving the Day with Scoped CSS
The scope method allows you to keep those styles specific to the page they're needed on, without bloating your main stylesheet and having...
Read more >Scoped CSS - Vue Loader
Scoped CSS. When a <style> tag has the scoped attribute, its CSS will apply to elements of the current component only. This is...
Read more >scope - CSS: Cascading Style Sheets - MDN Web Docs
The :scope CSS pseudo-class represents elements that are a reference point for selectors to match against. Currently, when used in a stylesheet ...
Read more >What is the current state of the "scoped" attribute for the style ...
As before, using a style tag without the scoped attribute will create global styles, so it will only be scoped if you include...
Read more >The scoped attribute | HTML5 Doctor
The scoped attribute for the <style> element allows you to include styles mid-document that targets a specific element and its children.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
Hi @rationality6 thanks for this! Yes please, let’s move forward with this.
In general there will be two options. You can use the following heuristic:
Does the style have more than one property?
<h1 class="header">
)<h1 style="margin: 0">
)We should never use ID-based styles.
Yeah I saw that but I think it relies on the condition “will be many times slower when scoped (i.e. when combined with an attribute selector)”. From what I see, class selectors become combined with an attribute selector too.
Hmm if you scroll down, the the names of each test are links and launch each test which alerts the time it takes. I observed similar results to the table, although much slower for both.
Test 4 Test 20