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.

prevent tags from being used without classes in scoped style definitions

See original GitHub issue

Observed 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:

  1. According to the vue docs, using tags inside scoped style blocks “will be many times slower”
  2. The DOM structure is coupled to the styling. For example, turning h2 to h3 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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
indirectlylitcommented, May 5, 2020

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?

  • If yes, use a class with some simple, meaningful name (e.g. <h1 class="header">)
  • If no, convert to an inline style in the template (e.g. <h1 style="margin: 0">)

We should never use ID-based styles.

0reactions
bjestercommented, Jun 23, 2020

This is guidance from the official vue docs: https://vue-loader.vuejs.org/guide/scoped-css.html#also-keep-in-mind

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.

Unfortunately the link “Here’s a playground where you can test the differences yourself” is broken…

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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