Component-Scoped Styles
Keep styles local to a component.
Component-Scoped Styles is a free Angular Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Styles Belong to Components
Angular lets each component carry its own CSS. By default these styles are scoped, meaning they only affect that component template and not the rest of the page.
Inline styles Array
You can define styles inline in the @Component decorator using the styles array.
@Component({
selector: "app-card",
template: "<div class='card'>Hello</div>",
styles: [".card { padding: 16px; border: 1px solid #ccc; }"]
})
export class CardComponent {}External styleUrls
More commonly, point to one or more external stylesheets with styleUrls.
@Component({
selector: "app-card",
templateUrl: "./card.component.html",
styleUrls: ["./card.component.css"]
})
export class CardComponent {}The styleUrl Shorthand
For a single stylesheet, modern Angular supports the singular styleUrl property.
@Component({
selector: "app-card",
templateUrl: "./card.component.html",
styleUrl: "./card.component.css"
})
export class CardComponent {}Scoping in Action
A .card rule in one component will not leak into another component that also uses the class card. Each is isolated.
/* card.component.css */
.card { color: navy; }
/* this navy color stays inside CardComponent only */How Scoping Works
Angular adds unique attributes to a component elements and rewrites its CSS selectors to match, so styles apply only within that component.
<!-- generated markup -->
<div class="card" _ngcontent-abc>Hello</div>
/* generated css */
.card[_ngcontent-abc] { color: navy; }Styles Do Not Leak In
Just as your styles stay in, most outside styles do not override component rules unintentionally, which keeps components self-contained.
Global Styles
For app-wide styles, use the global styles.css listed in angular.json. These are not scoped.
/* src/styles.css - global */
body { font-family: system-ui; }Multiple Stylesheets
A component can load several stylesheets by listing them in the array.
@Component({
selector: "app-page",
styleUrls: ["./base.css", "./layout.css"]
})
export class PageComponent {}Using class Bindings
Scoped styles pair naturally with class bindings to apply rules conditionally.
<div class="card" [class.featured]="isFeatured"></div>
/* card.component.css */
.featured { border-color: gold; }Why Scoping Helps
Scoped styles prevent naming collisions across a large app, let teams work independently, and make components portable and easy to reason about.
Quick Check
Test your understanding of component-scoped styles.
Recap
Components carry CSS via styles, styleUrls, or styleUrl. By default these rules are scoped to the component, preventing collisions. Use the global stylesheet for app-wide rules.
Frequently asked questions
Is the “Component-Scoped Styles” lesson free?
Yes — the full text of “Component-Scoped Styles” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “Component-Scoped Styles”?
Keep styles local to a component. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Component-Scoped Styles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Component-Scoped Styles
- View Encapsulation Modes
- Host and Host-Context Styling
- Dynamic Classes and Styles