Menu
CoddyTech

Scoped CSS

Part of the Fundamentals section of Coddy's Vue journey. Lesson 14 of 55.

A scoped style rule targets elements owned by its SFC. The badge below becomes green; a matching paragraph in another component is not selected by this rule.

<template><section><p class="badge">Ready</p></section></template>
<style scoped>
.badge { color: rgb(0, 100, 50); }
</style>

The compiler adds scope attributes to the component markup and selectors. Scoped styles are not a Shadow DOM and do not prevent normal inheritance. A parent can style a child root element for layout; in this lesson the styled paragraphs are inside a child wrapper, so the local rule stays on the child content.

challenge icon

Challenge

Easy

The supplied app mounts Notice.vue beside an outside paragraph. Add scoped CSS in Notice.vue so its .notice text is rgb(150, 40, 60). Keep the outside notice at rgb(20, 30, 40).

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals