I\'m trying to style an element placed by the router outlet in angular and want to make sure that the element generated gets a width of 100%
From most of the replies
To avoid changing the default encapsulation, I wrote a helper that append global styles for the component:
deepStyle.ts
import { ViewContainerRef } from '@angular/core';
export function deepStyle(vcr: ViewContainerRef, csss: string[]){
let id = 'deep-' + vcr.element.nativeElement.tagName;
let styleElement = document.getElementById('pierce-' + vcr.element.nativeElement.name);
if(!styleElement){
styleElement = document.createElement('style');
styleElement.id = id;
styleElement.innerHTML = csss.map(css => vcr.element.nativeElement.tagName + ' ' + css).join('\n');
document.head.append(styleElement);
}
}
my-component.ts
import { Component, ViewContainerRef } from '@angular/core';
import { deepStyle } from '../deepStyle';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.css']
})
export class MyComponent {
constructor(vcr: ViewContainerRef) {
deepStyle(vcr, [`
img {
height: 180px;
}
`]);
}
}
result:
<head>
...
<style id="deep-MY-COMPONENT">
MY-COMPONENT img {
height: 180px;
}
</style>
...
</head>