Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we control ViewEncapsulation.None from affecting the other components in the angular application?

@Component({
...
...
...
encapsulations: ViewEncapsulation.None
})

when i use encapsulation like that it conflicts the default style in the other components in application.

like image 619
Rajesh Malakar Avatar asked Sep 16 '25 20:09

Rajesh Malakar


1 Answers

When you load a component with ViewEncapsulation.None, its styles are applied to all the app.

So the answer is no, you can't...

But, what you can do is writing unique css style in your components style file, for example:

<app-toolbar></app-toolbar>

in your toolbar html you have something like:

<span>hello</span>
<div>My name is</div>

to apply styles only to this component write in its styles file:

app-toolbar span{
  color: green;
}

app-toolbar div{
  color: red;
}

or even better if you use scss files:

app-toolbar {
  span {
    color: green;
  }
  div {
    color: red;
  }
}

EDIT:

What you also could do and this works for sure (tested):

<app-toolbar></app-toolbar>

In this component html file (wrap all of its content in a div):

<div class="my-toolbar">
  <span>hello</span>
  <div>My name is</div>
</div>

Then in its style file:

.my-toolbar span{
  color: green;
}

.my-toolbar div{
  color: red;
}

or in scss format:

.my-toolbar {
  span {
    color: green;
  }
  div {
    color: red;
  }
}
like image 128
Freeky Avatar answered Sep 19 '25 13:09

Freeky