Prime Vyapar

Tales from a Developer's Trenches

Styling Components

Styling Components - Angular - Prime Vyapar
UI components can optionally include CSS styles that target their elements in the Document Object Model (DOM).

Code Snippet
Copy
                
                   
@Component({ selector: 'app-styling-components', templateUrl: './styling-components.component.html', styles: img { border-radius: 50%; } //styling example, }) export class StylingComponentsComponent { }

Style Scoping

Each component controls how its styles affect the page layout. This is called "view encapsulation." There are three options: Emulated (most common), Shadow DOM (advanced), and None (for special cases). You choose the option when creating the component.

Code Snippet
Copy
                
                   
@Component({ selector: 'app-styling-components', templateUrl: './styling-components.component.html', styles: img { border-radius: 50%; }, //styling example, encapsulation:ViewEncapsulation.None }) export class StylingComponentsComponent { }

ViewEncapsulation.Emulated

By default, Angular keeps a component's styles to itself, preventing them from accidentally affecting other parts of your app. This is called "emulated encapsulation." It works by giving each component a special code that gets secretly added to its elements. This code then tells the styles who they can actually apply to. This way, styles don't leak out and mess with other components. However, styles defined globally (outside of components) can still influence elements within a component using emulated encapsulation.

ViewEncapsulation.ShadowDom

Shadow DOM is a more advanced option for keeping component styles isolated. However, it goes beyond just styles and can affect how events work, how you interact with certain features, and how components appear in developer tools. Before using Shadow DOM, it's important to understand these additional impacts to make sure it's the right choice for your project.

ViewEncapsulation.None

This mode disables all style encapsulation for the component. Any styles associated with the component behave as global styles.

Leave a Comment

This sample demonstrates the full features of Rich Text Editor that includes all the tools and functionalities.

Comments