View Encapsulation issue angular5












2















I want to apply CSS to my child components that are called from a parent component using the trick it works fine



Encapsulation: ViewEncapsulation.None



But when i Navigate to other routes or pages it picks the style of the component whose Encapsulation is none weird behaiour



i.e when I hit route
http://localhost:4200/example



and Example component has



@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
encapsulation: ViewEncapsulation.None

})


on this route there are two other components



example.component.html



<app-example1></app-example1>

<app-example2></app-example2>


css is apllied to the respective childerns



but when i hit example2 it also picks the css of example1



@Component({
selector: 'app-example2',
templateUrl: './example2.component.html',
styleUrls: ['./example2.component.css'],


})


when i remove the encapsulation property it doesn't pick for example2 route



or when i access directly the route http://localhost:4200/example2



it works fine and css only pick up the example2 css but when i navigate to example1 and naviagtes to example2



it picks the style of example 1 component because of



 encapsulation: ViewEncapsulation.None


when i remove this property or directly access the route it works fine, what exactly is the issue I dont know why it is behaving like this



and example1 and example2 are two different modules and have its own routes










share|improve this question























  • Can you share your style files as well?

    – Bunyamin Coskuner
    Nov 23 '18 at 9:10
















2















I want to apply CSS to my child components that are called from a parent component using the trick it works fine



Encapsulation: ViewEncapsulation.None



But when i Navigate to other routes or pages it picks the style of the component whose Encapsulation is none weird behaiour



i.e when I hit route
http://localhost:4200/example



and Example component has



@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
encapsulation: ViewEncapsulation.None

})


on this route there are two other components



example.component.html



<app-example1></app-example1>

<app-example2></app-example2>


css is apllied to the respective childerns



but when i hit example2 it also picks the css of example1



@Component({
selector: 'app-example2',
templateUrl: './example2.component.html',
styleUrls: ['./example2.component.css'],


})


when i remove the encapsulation property it doesn't pick for example2 route



or when i access directly the route http://localhost:4200/example2



it works fine and css only pick up the example2 css but when i navigate to example1 and naviagtes to example2



it picks the style of example 1 component because of



 encapsulation: ViewEncapsulation.None


when i remove this property or directly access the route it works fine, what exactly is the issue I dont know why it is behaving like this



and example1 and example2 are two different modules and have its own routes










share|improve this question























  • Can you share your style files as well?

    – Bunyamin Coskuner
    Nov 23 '18 at 9:10














2












2








2








I want to apply CSS to my child components that are called from a parent component using the trick it works fine



Encapsulation: ViewEncapsulation.None



But when i Navigate to other routes or pages it picks the style of the component whose Encapsulation is none weird behaiour



i.e when I hit route
http://localhost:4200/example



and Example component has



@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
encapsulation: ViewEncapsulation.None

})


on this route there are two other components



example.component.html



<app-example1></app-example1>

<app-example2></app-example2>


css is apllied to the respective childerns



but when i hit example2 it also picks the css of example1



@Component({
selector: 'app-example2',
templateUrl: './example2.component.html',
styleUrls: ['./example2.component.css'],


})


when i remove the encapsulation property it doesn't pick for example2 route



or when i access directly the route http://localhost:4200/example2



it works fine and css only pick up the example2 css but when i navigate to example1 and naviagtes to example2



it picks the style of example 1 component because of



 encapsulation: ViewEncapsulation.None


when i remove this property or directly access the route it works fine, what exactly is the issue I dont know why it is behaving like this



and example1 and example2 are two different modules and have its own routes










share|improve this question














I want to apply CSS to my child components that are called from a parent component using the trick it works fine



Encapsulation: ViewEncapsulation.None



But when i Navigate to other routes or pages it picks the style of the component whose Encapsulation is none weird behaiour



i.e when I hit route
http://localhost:4200/example



and Example component has



@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
encapsulation: ViewEncapsulation.None

})


on this route there are two other components



example.component.html



<app-example1></app-example1>

<app-example2></app-example2>


css is apllied to the respective childerns



but when i hit example2 it also picks the css of example1



@Component({
selector: 'app-example2',
templateUrl: './example2.component.html',
styleUrls: ['./example2.component.css'],


})


when i remove the encapsulation property it doesn't pick for example2 route



or when i access directly the route http://localhost:4200/example2



it works fine and css only pick up the example2 css but when i navigate to example1 and naviagtes to example2



it picks the style of example 1 component because of



 encapsulation: ViewEncapsulation.None


when i remove this property or directly access the route it works fine, what exactly is the issue I dont know why it is behaving like this



and example1 and example2 are two different modules and have its own routes







angular angular2-routing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 9:08









Kumail HussainKumail Hussain

360625




360625













  • Can you share your style files as well?

    – Bunyamin Coskuner
    Nov 23 '18 at 9:10



















  • Can you share your style files as well?

    – Bunyamin Coskuner
    Nov 23 '18 at 9:10

















Can you share your style files as well?

– Bunyamin Coskuner
Nov 23 '18 at 9:10





Can you share your style files as well?

– Bunyamin Coskuner
Nov 23 '18 at 9:10












2 Answers
2






active

oldest

votes


















1














View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa.



For ViewEncapsulation None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page. Be careful with apps that have None and Native components in the application. All components with None encapsulation will have their styles duplicated in all components.



If you remove ViewEncapsulation propertythen it will take default one which is Emulated



In this case styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.






share|improve this answer
























  • how can i apply property of parent to specific components only

    – Kumail Hussain
    Nov 23 '18 at 9:32











  • Or how can i turn off encapsulation in other components or allow in specific components

    – Kumail Hussain
    Nov 23 '18 at 9:33













  • You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

    – Hardik Patel
    Nov 23 '18 at 10:35













  • can you suggest an example for global level class

    – Kumail Hussain
    Nov 23 '18 at 10:36











  • put css in style.css file

    – Hardik Patel
    Nov 23 '18 at 10:36



















1














simple old css hack



Add a unique class like
.app-component to the top most element of your component, and make sure all other styles are child of this class.



Better yet if you are using any kind of css preprocessors.



Info:



When using ViewEncapsulation.None, the defined styles are added to the html page using style tag in the head element.



So by ancient conventions, your styles get cascaded.



Hence when you have time invest your time in css styling conventions like BEM or something else.



Ciao!






share|improve this answer
























  • Where to add the class in css file

    – Kumail Hussain
    Nov 23 '18 at 10:35













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443566%2fview-encapsulation-issue-angular5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa.



For ViewEncapsulation None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page. Be careful with apps that have None and Native components in the application. All components with None encapsulation will have their styles duplicated in all components.



If you remove ViewEncapsulation propertythen it will take default one which is Emulated



In this case styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.






share|improve this answer
























  • how can i apply property of parent to specific components only

    – Kumail Hussain
    Nov 23 '18 at 9:32











  • Or how can i turn off encapsulation in other components or allow in specific components

    – Kumail Hussain
    Nov 23 '18 at 9:33













  • You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

    – Hardik Patel
    Nov 23 '18 at 10:35













  • can you suggest an example for global level class

    – Kumail Hussain
    Nov 23 '18 at 10:36











  • put css in style.css file

    – Hardik Patel
    Nov 23 '18 at 10:36
















1














View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa.



For ViewEncapsulation None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page. Be careful with apps that have None and Native components in the application. All components with None encapsulation will have their styles duplicated in all components.



If you remove ViewEncapsulation propertythen it will take default one which is Emulated



In this case styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.






share|improve this answer
























  • how can i apply property of parent to specific components only

    – Kumail Hussain
    Nov 23 '18 at 9:32











  • Or how can i turn off encapsulation in other components or allow in specific components

    – Kumail Hussain
    Nov 23 '18 at 9:33













  • You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

    – Hardik Patel
    Nov 23 '18 at 10:35













  • can you suggest an example for global level class

    – Kumail Hussain
    Nov 23 '18 at 10:36











  • put css in style.css file

    – Hardik Patel
    Nov 23 '18 at 10:36














1












1








1







View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa.



For ViewEncapsulation None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page. Be careful with apps that have None and Native components in the application. All components with None encapsulation will have their styles duplicated in all components.



If you remove ViewEncapsulation propertythen it will take default one which is Emulated



In this case styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.






share|improve this answer













View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa.



For ViewEncapsulation None - styles from the component propagate back to the main HTML and therefore are visible to all components on the page. Be careful with apps that have None and Native components in the application. All components with None encapsulation will have their styles duplicated in all components.



If you remove ViewEncapsulation propertythen it will take default one which is Emulated



In this case styles from main HTML propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 9:16









Hardik PatelHardik Patel

2,07211327




2,07211327













  • how can i apply property of parent to specific components only

    – Kumail Hussain
    Nov 23 '18 at 9:32











  • Or how can i turn off encapsulation in other components or allow in specific components

    – Kumail Hussain
    Nov 23 '18 at 9:33













  • You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

    – Hardik Patel
    Nov 23 '18 at 10:35













  • can you suggest an example for global level class

    – Kumail Hussain
    Nov 23 '18 at 10:36











  • put css in style.css file

    – Hardik Patel
    Nov 23 '18 at 10:36



















  • how can i apply property of parent to specific components only

    – Kumail Hussain
    Nov 23 '18 at 9:32











  • Or how can i turn off encapsulation in other components or allow in specific components

    – Kumail Hussain
    Nov 23 '18 at 9:33













  • You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

    – Hardik Patel
    Nov 23 '18 at 10:35













  • can you suggest an example for global level class

    – Kumail Hussain
    Nov 23 '18 at 10:36











  • put css in style.css file

    – Hardik Patel
    Nov 23 '18 at 10:36

















how can i apply property of parent to specific components only

– Kumail Hussain
Nov 23 '18 at 9:32





how can i apply property of parent to specific components only

– Kumail Hussain
Nov 23 '18 at 9:32













Or how can i turn off encapsulation in other components or allow in specific components

– Kumail Hussain
Nov 23 '18 at 9:33







Or how can i turn off encapsulation in other components or allow in specific components

– Kumail Hussain
Nov 23 '18 at 9:33















You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

– Hardik Patel
Nov 23 '18 at 10:35







You can not do it for specific component. Either you expose to all component or keep it secret. In your case you need to make unique class at global level.

– Hardik Patel
Nov 23 '18 at 10:35















can you suggest an example for global level class

– Kumail Hussain
Nov 23 '18 at 10:36





can you suggest an example for global level class

– Kumail Hussain
Nov 23 '18 at 10:36













put css in style.css file

– Hardik Patel
Nov 23 '18 at 10:36





put css in style.css file

– Hardik Patel
Nov 23 '18 at 10:36













1














simple old css hack



Add a unique class like
.app-component to the top most element of your component, and make sure all other styles are child of this class.



Better yet if you are using any kind of css preprocessors.



Info:



When using ViewEncapsulation.None, the defined styles are added to the html page using style tag in the head element.



So by ancient conventions, your styles get cascaded.



Hence when you have time invest your time in css styling conventions like BEM or something else.



Ciao!






share|improve this answer
























  • Where to add the class in css file

    – Kumail Hussain
    Nov 23 '18 at 10:35


















1














simple old css hack



Add a unique class like
.app-component to the top most element of your component, and make sure all other styles are child of this class.



Better yet if you are using any kind of css preprocessors.



Info:



When using ViewEncapsulation.None, the defined styles are added to the html page using style tag in the head element.



So by ancient conventions, your styles get cascaded.



Hence when you have time invest your time in css styling conventions like BEM or something else.



Ciao!






share|improve this answer
























  • Where to add the class in css file

    – Kumail Hussain
    Nov 23 '18 at 10:35
















1












1








1







simple old css hack



Add a unique class like
.app-component to the top most element of your component, and make sure all other styles are child of this class.



Better yet if you are using any kind of css preprocessors.



Info:



When using ViewEncapsulation.None, the defined styles are added to the html page using style tag in the head element.



So by ancient conventions, your styles get cascaded.



Hence when you have time invest your time in css styling conventions like BEM or something else.



Ciao!






share|improve this answer













simple old css hack



Add a unique class like
.app-component to the top most element of your component, and make sure all other styles are child of this class.



Better yet if you are using any kind of css preprocessors.



Info:



When using ViewEncapsulation.None, the defined styles are added to the html page using style tag in the head element.



So by ancient conventions, your styles get cascaded.



Hence when you have time invest your time in css styling conventions like BEM or something else.



Ciao!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 10:00









Akash PanigrahiAkash Panigrahi

714




714













  • Where to add the class in css file

    – Kumail Hussain
    Nov 23 '18 at 10:35





















  • Where to add the class in css file

    – Kumail Hussain
    Nov 23 '18 at 10:35



















Where to add the class in css file

– Kumail Hussain
Nov 23 '18 at 10:35







Where to add the class in css file

– Kumail Hussain
Nov 23 '18 at 10:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443566%2fview-encapsulation-issue-angular5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python