What you could do is subscribe on router.events to know when a navigation occurs. Then on NavigationEnd retrieve the current route path value and apply it as a CSS class on the desired HTML element with ngClass.
This means for example that navigating to home page will add a home class on the element you apply ngClass. You can then set the CSS class to style the element as you want.
StackBlitz example available here: https://stackblitz.com/edit/angular-stackoverflow-52907143
app.component.html
<div class="bg" [ngClass]="bgClass">
<div id="logo">
<a routerLink=""><b>Logo</b></a>
</div>
<div id="navigation">
<ul>
<li><a routerLink="/home">Home</a></li>
<li><a routerLink="/products">Products</a></li>
<li><a routerLink="/about">About</a></li>
</ul>
</div>
</div>
<router-outlet></router-outlet>
app.component.ts
import { Component } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
bgClass = 'default';
constructor(
private router: Router,
) {
// subscribe to router navigation
this.router.events.subscribe(event => {
// filter `NavigationEnd` events
if (event instanceof NavigationEnd) {
// get current route without leading slash `/`
const eventUrl = /(?<=\/).+/.exec(event.urlAfterRedirects);
const currentRoute = (eventUrl || []).join('');
// set bgClass property with the value of the current route
this.bgClass = currentRoute;
}
});
}
}
app.component.css
.default {
background: lightgray;
}
.about {
background: lightpink;
}
.home {
background: powderblue;
}
.products {
background: lightgreen;
}
<div #mainDiv>, access it through@ViewChild, then,OnInit(or, even better,OnChange), get the item and replace the CSS accordingly. Side note: please include the code as text, not as an image, thanks.