Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. Angular
  4. How to Apply css on child components in Angular

How to Apply css on child components in Angular

Date- Jan 16,2022 Updated Jan 2026 10192
Angular js Child Components

Understanding Parent and Child Components

In Angular, components are the building blocks of the application. Each component can contain its own styles, which are encapsulated by default. This encapsulation ensures that styles defined in one component do not affect others, leading to a predictable and maintainable code base. However, there are scenarios where we want to style child components from a parent component, which can be tricky due to this encapsulation.

When you attempt to apply styles from a parent component's CSS file to a child component, you may notice that the styles do not apply as expected. This is because the CSS is scoped to the component itself, which prevents it from cascading down to child components. Understanding how to manage this encapsulation is key to effectively styling your Angular applications.

Old Method: Using ::ng-deep

In earlier versions of Angular, developers could use the ::ng-deep combinator to apply styles from a parent component to its children. This method allowed for shadow-piercing styles that could penetrate the encapsulation barrier.

:host(.test) ::ng-deep div.label { border: 1px solid red; }
:host(.test) ::ng-deep div.value { border: 1px solid green; }

However, this approach has been deprecated and is no longer recommended for use in modern Angular applications. As a result, developers must explore alternative methods to achieve similar results.

Using ViewEncapsulation.None

One of the most effective ways to apply CSS from a parent component to its child components is by setting the ViewEncapsulation to None. This allows the styles defined in the parent component to be applied globally, including to its child components.

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'app-log',
  templateUrl: './log.component.html',
  styleUrls: ['./log.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class LogComponent {}

By applying ViewEncapsulation.None, you can ensure that the styles defined in the parent component's CSS file are applied to the child components. Remember to restart your Angular application after making this change to see the effects.

Alternative Methods for Styling Child Components

Aside from using ViewEncapsulation.None, there are other techniques to apply styles to child components effectively. One method is to use CSS variables, which allow you to define styles in the parent component and use them in the child components.

:host {
  --main-bg-color: blue;
}

.child-component {
  background-color: var(--main-bg-color);
}

In this example, the parent component defines a CSS variable --main-bg-color that can be referenced in the child component's styles. This approach provides a flexible and maintainable way to share styles between components.

Edge Cases & Gotchas

While applying CSS to child components, there are several edge cases and gotchas to be aware of:

  • Global Styles: Be cautious when using ViewEncapsulation.None as it can lead to unintended global styles affecting other components in your application.
  • Specificity Issues: CSS specificity rules apply, so ensure that your styles are specific enough to override any existing styles in child components.
  • Performance Considerations: Using global styles can impact performance, especially in large applications with many components.

Performance & Best Practices

When working with CSS in Angular, keep the following best practices in mind:

  • Scoped Styles: Whenever possible, use scoped styles to maintain encapsulation and avoid unintended side effects.
  • CSS Variables: Utilize CSS variables for shared styles to improve maintainability and reduce duplication.
  • Keep It Simple: Avoid overcomplicating your CSS structure. Simple, clear styles are easier to manage and understand.
  • Test Styles: Regularly test your styles in various scenarios to ensure they behave as expected across different components.

Conclusion

In summary, applying CSS to child components in Angular requires an understanding of component encapsulation and the available methods for styling. By utilizing techniques such as ViewEncapsulation.None and CSS variables, you can effectively manage styles in your Angular applications.

  • Understand the encapsulation model of Angular components.
  • Be aware of deprecated methods like ::ng-deep.
  • Use ViewEncapsulation.None judiciously to apply styles to child components.
  • Explore CSS variables for flexible styling options.
  • Follow best practices to maintain performance and clarity in your styles.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Tag or mention people like WhatsApp & Skype by using @ in Angular
Jan 16, 2022
How to reload child components in vue.js
May 15, 2021
Next in Angular
Tag or mention people like WhatsApp & Skype by using @ in Angular
Buy me a pizza

Comments

On this page

More in Angular

  • Export to Excel in Angular using ExcelJS? 9799 views
  • Angular Material Select Dropdown with Image 8289 views
  • How To Consume Web API Using Angular 4025 views
  • Mastering Angular Directives: ngIf, ngFor, and ngSwitch Expl… 52 views
  • Mastering Angular Unit Testing with Jasmine and Karma: A Com… 48 views
View all Angular posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor