What is the difference between <style module>
and <style scoped>
in Vue?
The official documentation (link1, link2) states only that scoped
uses PostCSS transformation and module
uses CSS Modules. But what is the real functional difference between the two?
(Note: If the PostCSS transformation they use is that plugin, it actually uses CSS Modules...)
With scoped , the parent component's styles will not leak into child components. However, a child component's root node will be affected by both the parent's scoped CSS and the child's scoped CSS. This is by design so that the parent can style the child root element for layout purposes.
The scoped attribute is a boolean attribute. When present, it specifies that the styles only apply to this element's parent element and that element's child elements (not the entire document). Note: The scoped attribute is deprecated and will be removed. Do not use it.
Allows CSS rules to be scoped to part of the document, based on the position of the style element. The attribute has been removed from the current specification.
In Vue. js, we can add an inline style to our element by binding the style attribute in the HTML tag. For reference, :style is shorthand for v-bind:style . Inline styling can be done in two ways: using object syntax or array syntax.
They're both ways of handling scoped CSS and do pretty much the same thing. The way they handle it is a bit different though.
Scoped styles in Vue is just normal CSS but with some additional classes added scoping. It's similar to the shadow DOM in that it will scope things out to a component. The benefit of this approach is you carry on writing CSS how you usually do but you get some additional scoping if that's what you want.
CSS Modules is different in that it uses Webpack to compile unique class names based on the block and class. It's kind of automatically creating unique BEM classes. It has a lot of additional functionality above CSS (which you don't have to use).
CSS Modules is not a Vue specific thing so if you learnt that you could apply it to any build where you can import CSS Modules. However Vue CSS scoping is so simple that there isn't really anything additional to learn if you know CSS (a couple of selectors is all really).
CSS Modules classes will be built by webpack as .{component}__{className}__{randomHash}
.
Scoped Vue CSS will be built by postcss as .{className}[data-v-{componentHash}]
. The componentHash
is applied to every element in that component.
Both methods are compiling CSS based on hashes and class names. Scoped CSS is also adding additional data attributes to the HTML for it's scoping. CSS Modules is using javascript a bit more to manage styles.
They're both doing pretty much the same thing though and for me the only real difference is how the classes are created. I guess CSS Modules should be more performant due to the lower specificity of all classes but it will really depend on the person writing the CSS. In my personal opinion I would stick with the simpler one (I'll leave you to decide which one that is)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With