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

Reuse for HTML: PART 1

In the last module, we prepared some CSS classes from the main landing page to be reused in the details page. In this part of the module we will reuse those classes while building a details page for car1 of the vehicle results.

You can download the finished code for this module with the link below:

Download
Table Of Contents
  1. Reusing the header and footer
    • Using the w3-include-html attribute


Create a new text file in the fredscars/html/Chapter01Mod10a directory (or your current development directory) called “car1-details.html”. Right click in the files pane in Windows Explorer to create the file and select “New -> Text Document” as shown below and name it “car1-details.html”.

Note: If you are creating a new Module Folder for each Module and copying your source files over as you go, then the path for the new file in the image above will be,
“C:\Development\FredsCars\Chapt01Mod10a\car1-details.html”.

Open up car1-details.html in TextPad or your favorite text editor and add the html code below.

<!doctype html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Car 1 Details</title>

   <!-- Bootstrap CSS -->
   <!-- CSS only: not combined with popper -->
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
		  crossorigin="anonymous">

	
   <link rel="stylesheet" href="css/site.css">

   <!-- Removed <style> element since we have no internal styles yet -->

   <!-- Removed <script> element since we have no internal JavaScript yet -->
</head>
<body>
   <header>
      Fred's Cars, Trucks & Jeeps
   </header>

   <div class="container-fluid py-4" style="border:1px solid black;">
      <h3 class="text-center bg-primary py-2">Car 1 Details</h3>
      <h1>Content</h1>
   </div>

   <div class="spacer"></div>

   <footer style="background-color:black;
	   color: white;
	   text-align:center">
      Contact us: (555)555-5555<br />
      121 Fredway St.<br />
      Cartown USA
   </footer>
</body>
</html>

In the above code I basically copied the html code from fredscars.html to car1-details.html, removed the style and script elements, and replaced the main content div that held the main content, category buttons, and results, with a div that looks like this:

<div class="container-fluid py-4" style="border:1px solid black;">
   <h3 class="text-center bg-primary py-2">Car 1 Details</h3>
   <h1>Content</h1>
</div>

If you now open car1-details.html in a browser it should look similar to the screen shot below.

Let’s inspect the new main content <div> in the code snippet above.

We have replaced the welcome message, category buttons, and results content with a heading 3 ,<h3>, element which gives a visual title to the page of “Car 1 Details”, centered it’s text with Bootstrap, gave it a Bootstrap primary background color and a Bootstrap vertical padding of 2 pixels.

The <h3> heading along with an <h1> heading containing the page’s placeholder text content, “Content”, are contained within our main content <div> element. We have applied Bootstrap’s container-fluid class to the main content <div> which makes the div full-width and gave it a vertical padding of 4 pixels using Bootstraps padding class notation. We have also applied a border style of 1 pixel, solid, black so you can see visually that the main content <div> is actually full width on the screen.

Notice how easily we were able to reapply the css styles to the header and footer by importing the site.css file with the <link> element in car1-details.html as well as fredscars.html.

<link href="css/site.css" rel="stylesheet" />

Reusing the header and footer

Take another look at the new code in car1-details.html. Notice we are using the same exact html for the header and the footer as in fredscars.html:

<!-- ... -->
<header>
   Fred's Cars, Trucks & Jeeps
</header>

<!-- ... -->

<footer style="background-color:black;
        color: white;
        text-align:center">
   Contact us: (555)555-5555<br />
   121 Fredway St.<br />
   Cartown USA
</footer>
<!-- ... -->

Imagine if we copy this to dozens or potentially hundreds or thousands of detail pages and then need to make a change to the header or footer. We would have to go through every webpage and manually make the change.

We need a way to breakout and reuse the header and footer html across all the detail pages like we did for our css file.

Using the w3-include-html attribute

It’s very surprising that html does not provide away to do this. But, fortunately, w3Schools.com has an attribute called w3-include-html that can be used to accomplish our goal: to reuse the same header and footer on all detail pages that the main page uses.

For the first step, we need to break out the header and footer html to separate files similar to the way we did with site.css. Create a new folder called “include” next to your “css” and “images” folders:

In the new include folder we are going to create two new files; header.html and footer.html. Create header.html now in the include folder and copy the header code into it from fredscars.html and save the file.

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

Now create footer.html and copy the footer html from fredscars.html into it and save the file:

<footer style="background-color:black;
	color: white;
	text-align:center">
   Contact us: (555)555-5555<br />
   121 Fredway St.<br />
   Cartown USA
</footer>

Modify the contents of car1-details.html with the following code.

<!doctype html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Car 1 Details</title>
   <script src="https://www.w3schools.com/lib/w3.js"></script>

   <!-- Bootstrap CSS -->
   <!-- CSS only: not combined with popper -->
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
      crossorigin="anonymous">

   <link rel="stylesheet" href="css/site.css">
   <!-- Removed <style> element since we have no internal styles yet -->

   <!-- Removed <script> element since we have no internal JavaScript yet -->
</head>
<body>
   <div w3-include-html="include/header.html"></div>

   <div class="container-fluid py-4" style="border:1px solid black;">
      <h3 class="text-center bg-primary py-2">Car 1 Details</h3>
      <h1>Content</h1>
   </div>

   <div class="spacer"></div>

   <div w3-include-html="include/footer.html"></div>

   <script>
      w3.includeHTML();
   </script>

</body>
</html>

At the top of the code above we imported a JavaScript file called w3.js from w3schools.com in the src attribute of a script tag.

At the bottom of the code we call a function from w3.js called w3.includeHTML(). This will look through all of our html elements and find any with the w3-include-html attribute and include the html from the file we point to.

We then replaced our header and footer divs with two divs that point to “include/header.html” and include/footer.html respectively.

If you open car1-details.html from the path c:\development\fredscars\html\Chapter01Mod10a\car1-details.html in your browser, it should look similar to the screen shot below.

Well wait a minute! What happened to our header and footer?

What’s happening here is that the w3-include-html attribute and supporting JavaScript will not work running the webpage from our local file system.

It will only work if you run the webpage from a Webhost Server. Windows comes with a host server called IIS (Internet Information Services). In the next part of this module we’ll set up our environment so that fredscars.html and car1-details.html are running from IIS.

< 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
  • Create the Update Page
  • Create the Delete Page
  • Validation
  • Logging & Configuration
  • Storing Secrets
  • Error Handling
  • Security & Administration

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