Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

Create the Header & Footer components

Now that we have Saas configured, let’s take a few minutes to create header and footer components for our web application.

Note: At this point I am copying the project from the Module14 folder to a Module15 folder. The pathway I am using for development here is:
C:\Development\FredsCars\FullStack\Module15.
Table Of Contents
  1. Create the Header Component
    • MatToolbarRow
    • Center the title message
  2. Create the Footer Component
  3. What's Next

Create the Header Component

Open a command line and navigate to the FredsCars project in the solution.

Type the following command.

ng generate component Header --skip-tests --dry-run

In the above command I am taking a dry run at creating a component named Header and specifiying to forgo the creation of a test file.

And I get the following results telling me “More than one module matches”.

It looks like we will need to use the module switch we talked about earlier. Type the following command.

ng generate component Header --skip-tests --module=app --dry-run

and you should see the following results.

This time the dry run is telling us we will get the results we are expecting. A folder called header will be created and in the new header folder our three component files will be created with no test file and the root module will be updated to register the new home component. Let’s do it for real now! Type the following command.

ng generate component Header --skip-tests --module=app

And you should see the following results.

In Solution Explorer, you should now see the new header component.

If you look at the generated code for the header components TypeScript file, it specifies a selector of app-header.

import { Component } from '@angular/core';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent {
}

So to use the new Header Component, we need to put an <app-header> element somewhere in the HTML. Open the app.component.html file in the FredsCars/src/app folder and make the following modification in bold blue font.

<app-header></app-header>
<app-nav-menu></app-nav-menu>
<div class="container">
  <router-outlet></router-outlet>
</div>

The results in your browser window should now look similar to the screenshot below.

In the above screenshot the Header component is now rendering above our MatToolbar in AppComponent with the autogenerated HTML, “Header works”.

All is looking good. Right? Well we could probably continue down this road and get everything looking the way we want. But we are running into a slight problem. If we look back at Chapter one, Module three, “Mock your site with HTML, we laid out the header and title of our website with the following HTML where the header and title is in bold blue font.

<html>
<head>
	<title>Fred's Cars, Trucks & Jeeps</title>
</head>
<body>
    <header>
	Fred's Cars, Trucks & Jeeps
    </header>

    <h1>Welcome to Fred's Cars!</h1>

    Thank you for visiting our site.
</body>
</html>

which after adding styling renders like this.

But here in our full-stack application we have already used the <header> element in our NavMenuComponent HTML to contain our MatToolbar component with our navigation buttons. I would like our Angular Header component to also live in the HTML <header> element along with the top navigation. How can we accomplish this?

MatToolbarRow

Well, it turns out MatToolbar also supports multiple rows with MatToolbarRow. Let’s make some modifications to make our Header and NavMenu Components live in the HTML <header> element seamlessly together.

First let’s take the <app-header> element out of app.component.html and put it back like we had it.

FredsCars/src/app/app.component.html

<app-nav-menu></app-nav-menu>
<div class="container">
  <router-outlet></router-outlet>
</div>

Next make the following modifications to nav-menu.component.html.

FredsCars/src/app/nav-menu/nav-menu.component.html

<header>
  <mat-toolbar color="primary">
    <mat-toolbar-row>
      <app-header></app-header>
    </mat-toolbar-row>
    <mat-toolbar-row>
      <a mat-icon-button [routerLink]="['/']">
        <mat-icon>
          home
        </mat-icon>
      </a>
      <a mat-flat-button [routerLink]="['/vehicles']">
        Vehicles
      </a>
    </mat-toolbar-row>
  </mat-toolbar>
</header>

And the results in your browser should look similar to the results in the following screenshot.

It does look like our Header and NavMenu components are coexisting quite nicely together in the <header> element. Let’s take a look at the html and Angular component structure in the web development tools. In your browser window hit the F12 key to bring up the web development tools window. Below is a screenshot highlighting with red borders the important elements and attributes to pay attention too for this exercise.

In the screenshot above we can see in the elements tab of the web development tools window the mixture of HTML elements and Angular components. The root component, AppComponent, represented by the <app-root> element is the top component laid out in the single index.html page that is served up by the application.

AppComponent loads up our NavMenu Component.

The HTML in NavMenuComponent contains the HTML <header> element for the dynamic webpage.

Inside of the <header> element sits an Angular Material MatToolbar Component.

The MatToolbar component contains two Angular Material MatToobarRow Components.

The first MatToolbarRow component contains our Header component.

And the second MatToolbarRow component contains the MatIconButton and MatFlatButton <a> elements.


Next let’s add our “Fred’s Cars, Trucks & Jeeps” text into our Header component with black background and white text like we had back in Chapter one.

Open header.component.html from the FredsCars/src/app/header folder and replace the html contents with the code below.

FredsCars/src/app/header/header.component.html

<div>
  Fred's Cars, Trucks & Jeeps
</div>

Next open header.component.scss and fill the contents with the following CSS.

FredsCars/src/app/header/header.component.scss

div {
  font-size:16pt;
}

At this point the title would render to the left of the top MatToolbarRow. The next couple steps will center the title message and give the top MatToolbarRow a black background.

Center the title message

Now open the nav-menu.component.html file in the FredsCars/src/app/nav-menu folder and modify its contents with the code highlighted in bold blue font.

FredsCars/src/app/nav-menu/nav-menu.component.html

<header>
  <mat-toolbar color="primary">
    <mat-toolbar-row class="matToolbar-header-title">
      <span class="spacer"></span>
        <app-header></app-header>
      <span class="spacer"></span>
    </mat-toolbar-row>
    <mat-toolbar-row>
      <a mat-icon-button [routerLink]="['/']">
        <mat-icon>
          home
        </mat-icon>
      </a>
      <a mat-flat-button [routerLink]="['/vehicles']">
        Vehicles
      </a>
    </mat-toolbar-row>
  </mat-toolbar>
</header>

Finally open nav-menu.component.scss and fill it with the CSS below.

FredsCars/src/app/nav-menu/nav-menu.component.scss

.matToolbar-header-title {
  background-color: black;
}

.spacer {
  flex: 1 1 auto;
}

And now the results in your browser should look similar to the following.

In the code above we took the following steps.

  • header.component.html – Added our “Fred’s Cars, Trucks & Jeeps message wrapped in a DIV element.
  • header.component.scss – targeted our DIV message element and gave it a font size of 16pt.
  • nav-menu.component.html – Added to our top MatToolbarRow element a class called “matToobar-header-title so we can target the top row and give it a background color of black with CSS. Wrapped the <app-header> element, which is our Header component, between two SPAN elements with a class of “spacer”. We use this class in nav-menu-component.scss to center the top row title message, “Fred’s Cars, Trucks & Jeeps”.
  • nav-menu.component.scss – set the top MatToolbarRow’s background color to black.
    Use the flex system to make any element with the class, “spacer” grow horizontally and take up all available space. Since the top row message in the toolbar is wrapped between two SPAN elements with this class, the effect will be to center the message.

Create the Footer Component

Now that we have a nice Header title message, let’s move on and create the Footer component.

Open a command line and navigate to the FredsCars project in the solution.

Run the following command. (Remember, you can do a dry run first if you want to make sure the new Header component will be registered in the root module rather then the AppRouting or AngularMaterial modules).

ng generate component Footer --skip-tests --module=app

You should now have a new Footer folder in the FredsCars/src/app folder with the three new Header component files.

Open the app.component.html file and modify the code with contents below.

FredsCars/src/app/app.component.html

<app-nav-menu></app-nav-menu>
<div class="container">
  <router-outlet></router-outlet>
</div>
<app-footer></app-footer>

The results in your browser should look like the following screenshot.

In the above screenshot we can see the Footer comonent is now showing at the bottom of the screen. Let’s finish it.

Modify the contents of footer.component.html with the code below.

FredsCars\src\app\footer\footer.component.html

<footer>
  Contact us: (555)555-5555<br />
  121 Fredway St.<br />
  Cartown USA
</footer>

Now modify the footer css in footer.component.scss with the code below.

FredsCars/src/app/footer/footer.component.scss

footer {
  background-color: black;
  color: white;
  text-align: center;
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 15px;
}

Now the results in your browser should look like the following screenshot.

The Header component and accompanying CSS is pretty simple. We basically used the same <footer> element and contents from Chapter One and dressed it up with CSS to:

  • Center align the text.
  • Make the background color black and the text color white.
  • Give the footer a width of 100% so it will fit all the way accross the screen horizontally.
  • Give it some padding of 15 pixels.
  • Make the footer “sticky” to stay on the bottom of the screen as the user scrolls if there is enough content vertically to create a scrollbar.
    • Set position to fixed and bottom to 0.

This approach to a sticky footer is a little different then the approach used in Chapter one. In Chapter One if there is a lot of content in the browser you have to scroll down to the end of the content to see the footer. The term sticky there just refers to the fact that the footer is not sitting in the middle of the screen even if there is not enough content to push it down.

Here the footer is truly sticky and stays at the bottom of the screen no matter what position the scroll bar is at.

What’s Next

In this module we created Header and Footer components and the look of our application is progressing quickly. It is no longer bombarded by the purplish blue in the Indigo-Pink theme we chose when installing Angular Material. The white on black text in the header and footer and the light blue alternating rows in the Vehicles table is starting to give our application it’s own brand, look & feel.

In the next module we will add some functionality to the Vehicles table with paging and sorting using the Angular Material MatTable component.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme