3

Is it possible to isolate Bootstrap's css file to one Angular component?

When including the file globally in styles.css or in .angular-cli.json, the global styles are causing issues with other components that should not be using bootstrap. I have tried to do this by importing it from the component's CSS file but the styles do not seem to render properly...

I am using Angular 5 and Angular Cli 1.7.2.

1 Answer 1

2

you can explicitly set the encapsulation strategy using the encapsulation property.Set it to ViewEncapsulation.Native for your case

@Component({
// ...
encapsulation: ViewEncapsulation.Native,
styles: [
  // ...
]
})
export class HelloComponent {
// ...
}

Native - styles from main HTML do not propagate to the component. Styles defined in this component's @Component decorator are scoped to this component only. Refer this View Encapsulation.

Sign up to request clarification or add additional context in comments.

7 Comments

When trying this, the component does not render at all, any ideas?
That should not happen what is the error you get on browser's console if possible share your code?
Ah looks like it's not supported in the newest version of firefox... TypeError: hostEl.createShadowRoot is not a function. Any other way to accomplish this?
This approach worked for me, I couldn't find any other approach.
Use ---> encapsulation: ViewEncapsulation.ShadowDom
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.