Overriding the encapsulated :host-style of external component

Benedikt Beun

Is it possible to override :host-styling of an external angular2-component?

We're making a library including a sidebar-component. This component has a default (fallback) background, but this should be overridable by css/theme used in the app.

@Component({
    selector: 'sidebar',
    styles: [`
        :host { background-color: green; }         
    `],
    template: `
        <h1>sidebar</h1>
        <ng-content></ng-content>
    `
})
export class SideBarComponent { .... }

Main App css:

<style>
    sidebar {background: red; color: yellow; }
</style>

This returns a sidebar with green background and yellow text, but I want a red background...

Benedikt Beun

Edited:

As found on http://blog.angular-university.io/how-to-create-an-angular-2-library-and-how-to-consume-it-jspm-vs-webpack/: add an attribute to the body-tag:

<body override>
    <app></app>
</body>

And in your css: use a selector for this attribute:

[override] hello-world h1 {
    color:red;
}

This way, your css does not have to be parsed.

Previous solution:

I've found a solution myself: instead of linking my (theming) css-file in index.html, which isn't parsed, I imported this particular css-file in the app.component.ts annotation.

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
      <link rel="stylesheet/css" type="text/css" href="/assets/style/app.css" />
    </head>
    <body>
        <app></app>
    </body>
</html>

app.component.ts:

import { ... }
@Component({
    selector: 'app',
    styles: [`
        @import "assets/style/theme.css";
    `], 
    template: `
        ...`,
})
export class AppComponent {...}

theme.css:

sidebar {background: red; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Overriding the encapsulated CSS of external component

From Dev

Internal style not overriding external style sheet

From Dev

Overriding antd component style for just one component

From Dev

Angular Material Overriding Default Style of SnackBar Component

From Dev

style host in Angular from component

From Dev

Vue Component with external template and style?

From Dev

styled-components: style in theme is overriding component style

From Dev

Angular 5: creating a parent component with an external template and overriding the inner part

From Dev

Style host element on Stencil component based on class

From

Angular 2: How to style host element of the component?

From Dev

Style host component based on model property

From Dev

React Native External Style Sheet not updating component

From Dev

Right way to override child component style from host component angular

From Dev

How to dynamically set component host style Angular 8

From Dev

No style from turborepo "external" package component imported in NextJS page

From Javascript

Overriding !important style

From Dev

Overriding application wide style

From Dev

Overriding certain style in SCSS

From Dev

overriding <html> element style

From Dev

Loop not overriding style of a div

From Dev

Overriding TextBox style for BorderBrush

From Dev

TFrame overriding style color

From Dev

MouseListener overriding Paint Component

From Dev

Overriding a component method

From Dev

Overriding custom component property

From Dev

Overriding nsIWindowWatcher Component

From Dev

Traefik forwarding to a host and overriding IP

From Dev

Overriding JQXWidget css style not working

From Dev

Overriding the style of react select options

Related Related

HotTag

Archive