Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable router outlet in Angular

Tags:

html

css

angular

My page is pretty simple. I want a fixed header and footer regardless of the screen size, and want only the content to be scrolled if it is more(if the content overflows).

<div class="header">
 <h2>Title</h2>
</div>
<div class="content">
  <router-outlet></router-outlet>
</div>
<div class="footer">
  <h2>footer</h2>
</div>

I am trying something like this and the exact below code is not working.

Fixed header, footer with scrollable content

like image 771
Chatra Avatar asked Mar 07 '19 21:03

Chatra


People also ask

Can we pass data in router outlet in Angular?

Angular 7.2. 0 introduced a new way to pass data using State Object while navigating between routed components. The benefit of using a state object is that you can share data between routed components with routeParams without displaying it on the URL segment, unlike queryParams in Angular.

What does router outlet do in Angular?

Router-Outlet is an Angular directive from the router library that is used to insert the component matched by routes to be displayed on the screen.

What is named router outlet in Angular?

In simple, named router outlets are simply router outlets with name. When there are multiple outlets, the names are important as they help Angular to determine where the subpages should go to.

What is router outlet Angular 8?

RouterOutletlink Acts as a placeholder that Angular dynamically fills based on the current router state.


1 Answers

You can use the Flexbox Layout (Flexible Box) module to align and distribute space among header, content and footer.

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

<div class="container">
  <div class="header">
  <nav>
    <a routerLink="/home" routerLinkActive="active">Home</a>
    <a routerLink="/about" routerLinkActive="active">About</a>
  </nav>
  </div>
  <div class="content">
    <router-outlet></router-outlet>
  </div>
  <div class="footer">
    <h2>footer</h2>
  </div>
</div>

we are using an outer element with class=container to apply the display:flex and flex-direction: column properties.

In our CSS we are going to use flex-shrink and flex-grow to distribute the inner elements.

flex-shrink Defines how much a flexbox item should shrink if there's not enough space available.

flex-grow Defines how much a flexbox item should grow if there's space available.

.container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header{
  flex-shrink: 0;
  flex-grow: 0;
}
.content{
  flex-shrink: 1;
  flex-grow: 1;
}

.footer{
  flex-shrink: 0;
  flex-grow: 0;
}

Finally it is important to set height 100% to HTML, body and container elements

html, body{
 height: 100%;
 padding: 0;
 margin: 0;
}

StackBlitz example with angular and flexbox:

https://stackblitz.com/edit/angular-fixed-footer-header

like image 105
Javier Rojano Avatar answered Sep 20 '22 11:09

Javier Rojano