I have DOM that looks something like this:
...
You can use the adjacent sibling selector
router-outlet + project { ... }
https://developer.mozilla.org/en/docs/Web/CSS/Adjacent_sibling_selectors
but only if @drewmoore's approach doesn't apply.
<app>
<div class="your css class">
<router-outlet></router-outlet>
</div>
</app>
This works for me
since router injects the component after the the router-outler element, if we would like to style all injected component with the same set of rules the folowing rule is can be helpful.
the css "+" operator select the first sibling element of a certain type, while asterisk (*) is used as a wild card to select any 1st sibling of router-outlet
router-outlet + * {
// your rules
}
The key is /deep/ keyword:
:host /deep/ router-outlet + project {
display: block;
border: 10px solid black;
}
This works without any extra configurations.
:host /deep/ router-outlet + *
for whatever component dynamically created by Angular Router.
Since Angular 4.3.0 made /deep/
deprecated, its suggested alternative is ::ng-deep
. And there were a long discussion about this.
If you need to add a class conditionally, you can add it programmatically from the component:
constructor(private renderer: Renderer2, private elemRef: ElementRef) {
if(someCondition){
renderer.addClass(elemRef.nativeElement, 'myClass');
}
}
use the adjacent sibling selector
and the *
wildcard to select the element immediately following the router-outlet
styles.css
router-outlet + * {
/* your css */
}