Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

399
Views
Angular scrollToAnchor not working with flex

I have app component which has following html

<app-header></app-header>
<div class="main-container">
    <router-outlet></router-outlet>
</div>
<app-footer></app-footer>

with css

:host {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.main-container {
  display: flex;
  flex-grow: 1;
  overflow: auto;
}

and component in router-outlet looks like this:

<div id="home">
</div>

<div id="about">
</div>

<div id="contact">
</div> 

I'm trying to add scrollToAnchor, but it's not working. It works if I remove overflow: auto from main container, but then whole page is scrollable, and header and footer are not static. Is there way to enable scrollToAnchor with flex layout and only scrolling middle part of app? I'd prefer flex instead of position: fixed.

main.component.ts:

constructor(
        router: Router,
        viewportScroller: ViewportScroller
    ) {

        router.events.pipe(
            filter(e => e instanceof Scroll)
        ).subscribe(e => {
            e = e as Scroll
            if (e.anchor) {
                // anchor navigation
                viewportScroller.scrollToAnchor(e.anchor);
            }
        });
    }

html that triggers scrollToAnchor

<a mat-button routerLink="/" fragment="home"></a>
<a mat-button routerLink="/" fragment="about"></a>
<a mat-button routerLink="/" fragment="contact"></a>
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!