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
EasyThe 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>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks