How do CSS Modules differ from Vue scoped CSS?
This question tests build-time versus compiler style isolation. CSS Modules export a hashed class mapping object; Vue scoped CSS adds unique data-attributes. You bind templates via $style.myClass or an imported object. Red flag: claiming both use attributes.
What's really being asked
This question tests whether you understand the difference between build-time class hashing and compiler-level attribute scoping, and whether you know the practical template syntax for CSS Modules in a Vue context. Interviewers care that you see CSS Modules as a framework-agnostic build-tool contract that produces a mapping object, while Vue scoped styles are a framework-specific compiler feature.
The full answer
First, explain the CSS Modules contract. A CSS Module is processed by a build tool so that every class name becomes a globally unique hashed string. The loader exports a JavaScript object where keys are your original local names and values are the generated global class strings. You import that object and reference properties to apply classes. Second, contrast this with Vue scoped CSS. Vue's compiler adds a unique data-attribute, such as data-v-abc123, to every element in the component template and rewrites the style block so each selector targets only elements with that attribute. Third, highlight the practical differences. CSS Modules are portable across React, Vue, or vanilla projects because they rely only on a bundler loader. Vue scoped CSS is tightly coupled to the Vue compiler and Single File Component system. Fourth, show the template code. In a Vue file using CSS Modules via the module attribute on the style block, you bind classes with div v-bind:class="$style.myClass". Alternatively, if you import the module manually in script setup, you bind the imported object's properties directly. For Vue scoped CSS, you use the scoped attribute on the style block and ordinary class names because the compiler handles the scoping automatically.
The mistakes people make
Claiming that CSS Modules work by adding a data-attribute like Vue scoped CSS. Describing CSS Modules as just a naming convention like BEM instead of a build-time transformation. Forgetting that CSS Modules expose an explicit mapping object and instead treating the import as a side-effect. Saying Vue scoped CSS hashes the class names rather than using attribute selectors. Giving template syntax like class="styles.button" without a binding directive in Vue.
What usually comes next
When would you prefer CSS Modules over Vue scoped styles in a Vue application? How do you handle global styles or exceptions in either system? Can you use CSS Modules and scoped styles together? How does composition work in CSS Modules using composes?
A concrete example
Imagine a Button.vue component. With CSS Modules, you write style module, define .button { color: blue; }, and in the template use button :class="style.button". The compiled output replaces style.button with a hashed class like Button_button__3x7a2. With Vue scoped CSS, you write style scoped, define .button { color: blue; }, and use button class="button". The compiler rewrites the CSS to .button[data-v-1a2b3c4] and injects data-v-1a2b3c onto the button element.
Interview question
How do CSS Modules and Vue scoped CSS fundamentally differ in their approach to style encapsulation?
- a.CSS Modules are a Vue compiler feature that scopes styles by attribute, while scoped CSS requires manual class binding via an imported object.
- b.CSS Modules inject unique data-attributes into the DOM, while scoped CSS hashes class names during compilation.
- c.CSS Modules hash class names and expose a mapping object, while scoped CSS adds unique data-attributes to elements and selectors.Correct
- d.Both systems hash class names, but only CSS Modules require explicit binding to an imported object in the template.
Why? this is the answer
CSS Modules use a build tool to hash class names and export a JavaScript mapping object, while Vue scoped CSS relies on the Vue compiler to add unique data-attributes and rewrite selectors. Distractor A reverses these two mechanisms, which is a common misconception.
Just read this? Test yourself on what you have been reading.
Read the original → github.com
- #css modules
- #vue
- #scoped css
- #frontend
- #build tools
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles