Numerous web pages featuring the same title and navigation menus

Creating my personal academic website involves multiple pages that share a common structure.

Question: I'm searching for a method to avoid duplicating the header and menu code in each HTML file. Is there an approach where I can store the code for the header and menu in a separate file, like header.html, and then use a statement such as \input{header.html} (similar to TeX syntax) to include the file into both index.html and publications.html.

A preview of the website's layout is provided below.

Contents of index.html:

<!DOCTYPE html>

<html>
    <head>
        <title>The Title</title>
        <link type="text/css" rel="stylesheet" href="stylesheet.css">
    </head>
    <body>
        <div id="header">
            <h1>My Name</h1>
        </div>

        <div id="menu">
            <p>
                <a href="index.html">Home</a>
                &nbsp&nbsp
                <a href="publications.html">Publications</a>
                &nbsp&nbsp
                <a href="contact.html">Contact</a>
            </p>
        </div>

        <div id="content">
            <p>
                This is my academic webpage.
            </p>
            <p>
                I am a 15th year PhD student in Filmmaking at Mickey's Institute of Technology (MIT).
            </p>
        </div>
    </body>
</html>

Contents of publications.html:

<!DOCTYPE html>

<html>
    <head>
        <title>The Title</title>
        <link type="text/css" rel="stylesheet" href="stylesheet.css">
    </head>
    <body>
        <div id="header">
            <h1>My Name</h1>
        </div>

        <div id="menu">
            <p>
                <a href="index.html">Home</a>
                &nbsp&nbsp
                <a href="publications.html">Publications</a>
                &nbsp&nbsp
                <a href="contact.html">Contact</a>
            </p>
        </div>

        <div id="content">
            <ol>
                <li>Snow White and the Seven Dwarfs, 1937</li>
                <li>Pinocchio, 1940</li>
                <li>The Reluctant Dragon, 1941</li>
            </ol>
        </div>
    </body>
</html>

Contents of stylesheet.css:

    body {font-size: 16px; line-height: 20px; font-weight: light; color: #250517; /*gray*/}
    a:link {text-decoration: none; color: #003399; /*blue*/}
    a:visited {text-decoration: none; color: #003399; /*blue*/}
    a:active {text-decoration: none; color: #003399; /*blue*/}
    a:hover {text-decoration: none; color: #003399; /*blue*/}

    #header {
        width:800px;
        margin-left:auto; 
        margin-right:auto;
        text-align:center;
        margin-top:50px;
    }

    #content {
        width:730px;
        margin-left:auto; 
        margin-right:auto;
        height:410px;
    }

    #menu {
        width:800px;
        margin-left:auto; 
        margin-right:auto;
        text-align:center;
        margin-bottom:50px;
        clear:both;
    }

Answer №1

It is not possible to achieve this using just HTML and CSS, as it goes beyond their intended purpose.

There are two alternative methods you can consider:

  1. If you are working with a server-side language like PHP, you can utilize the libraries' syntax for including content from other files inline through functions such as include(). You can find more information on this in the PHP documentation, or by exploring options like Server Side Includes.

  2. Another approach is to use JavaScript or jQuery to fetch content from other pages and inject them into your current page at a specific location. This can be easily achieved using jQuery's load() method, which is explained further here.

Answer №2

I encountered a similar issue and found a solution. I decided to create a separate file specifically for storing the HTML code of the navigation bar.

This new file, which I named navbar.html, contained all the necessary navigation bar code. To incorporate this into my main HTML file, I utilized jQuery.

$(document).ready(function() {
    $('#navigation').load('navbar.html');
});

To integrate the navigation bar into your desired location within the webpage, simply add this line of code:

<div id="navigation"></div>

Answer №3

One way to achieve the same result is by utilizing PHP code, such as the following example: include "navbar.html;

Answer №4

@Abhinav's solution is not entirely accurate. It will fail to load properly in Chrome because the file navbar.html is using the type file:// instead of http:// as required by the jQuery load function. To rectify this issue, try the following:

$(document).ready(function() {
    $('.navigation').load('navbar.html');
});

Make sure to reference this code in your base HTML file using a similar div method as demonstrated above:

<div class="navigation"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resolved issue with background image display on Mac Chrome

I'm currently in the process of developing a website that incorporates fixed background images for smooth transitions between sections. The implementation is entirely CSS-based and has been effective in all browsers except for one: Chrome on Mac (Vers ...

Unable to display tags with /xoxco/jQuery-Tags-Input

I'm experimenting with the tagsinput plugin in a textarea located within a div that is loaded using the jquery dialog plugin. The specific plugin I am using is /xoxco/jQuery-Tags-Input. After checking that the textarea element is ready, I noticed th ...

What is the best way to add a bottom border to each row in a textarea?

I am currently exploring methods to include a border-bottom line for each row in a <textarea>, but so far I have only been able to achieve this on the very bottom row. Is there any way to make this happen? .input-borderless { width: 80%; bord ...

md-datepicker incorrect input- AngularJS

When selecting a non-existent date manually in the input, the calendar shows the date I expect but the input does not. Here is the code snippet: <md-datepicker name="startDate" class="custom-datepicker" ng-model="models.startDate" md-placeholder="Ente ...

In IE11, the property of table.rows.length is not functioning as expected

In my HTML page, I have the following script: var table = document.getElementById(tableID); var rowCount = table.rows.length; While this script works fine in IE8, in IE11 it does not return the exact row count. Instead, it only returns "0" when the actua ...

Can the meta viewport be eliminated using a script?

I currently do not have access to the HTML file on the server side, but I can make changes by adding additional HTML to the website. My issue is with a non-responsive site listed below, which now requires me to zoom in after it loads due to the new viewpor ...

Sending JavaScript functions to PHP files via Ajax

My Current Project: I am currently developing a script that provides users with choices and generates new options based on their selections. To achieve this, I have created two scripts - one for the HTML structure of my page and another for fetching serve ...

javascript - Alternate text colors several times within specified color ranges

Seeking assistance with changing the text color multiple times from #627CA9 to #FFFFFF and vice versa. Here's what I've tried: function changeColor(id) { var x = document.getElementById(id); if (document.getElementById(id).style.color = " ...

Obtain user input and showcase it on the screen using a combination of Ajax and PHP

Whenever a user inputs a value in the textbox, it should dynamically display on the screen. I have successfully implemented this feature once, but I am encountering difficulties with implementing it for a second input. Your help is greatly appreciated. fu ...

Sharing resources between different origins and the file:// protocol

I have been developing an HTML5 application that is collecting data from various sources using JSONP. So far, everything involving a GET request has been functioning perfectly. However, I have encountered a hurdle while attempting to make a POST request. T ...

The drawback of using a datepicker within an Angular input field

When attempting to open the Angular Material datepicker, it was appearing above the input field. How can I make it open below the input field instead? <mat-form-field appearance="outline" class="w-100 date-class"> < ...

Step-by-step guide to activating vertical scrolling for select tiers in a multi-layered fly-out navigation

Currently working on a multi-level flyout menu to be integrated into a daterange picker for selecting time periods. Check out the jsfiddle link here: https://jsfiddle.net/6t72hd4x/1/ I attempted to set overflow-y: auto; in the .inner-list class to handle ...

Loading screen preceding page change

I've been searching everywhere and can't find a good tutorial on how to smoothly transition pages when clicking a link on my website. I want the page to fade out on click, display a preloader, then fade in the new page once the preloader finishes ...

What is the process of editing a webpage to affect the display on a different webpage?

I am currently working on creating two webpages. One will serve as a customization page where users can upload images and input text, which will then be displayed on another page. I am wondering if it is possible to achieve this functionality using CSS and ...

Define a specific Css class within an ID

Is there a way to target classes within an id in my CSS file? In my html file, I have a simple label and textbox setup: <div id="User"> <div> <div class="left"> <asp:Label ID="Label1" runat="server" Text="User ...

Removing gaps around rounded corners in jQuery UI accordions is a common issue that often arises when styling web elements

I am currently using jQuery UI Accordion to create collapsible sections that expand when clicked. In order to enhance the appearance, I have applied a background image to the header of each section like this: Check out my jQuery UI Accordion Demo here Up ...

What is the best way to vertically center text within a div?

After successfully centering my text horizontally in my div using text-align: center, I'm now faced with the challenge of centering it vertically. Despite my attempts with vertical-align: middle;, I haven't been able to achieve the desired result ...

Ways to choose an option from a drop-down menu without the select tag using Selenium

I'm looking to perform automated testing with Selenium using Java but I'm facing an issue when trying to select an item from a dropdown list. The HTML structure does not include a select tag for the drop-down menu items (such as Auth ID and Corre ...

Creating personalized labels with Vue.js and Element UI radio button components

When using Element UI radio buttons on a form, I am attempting to personalize the label for each selection. Each radio button includes a prop called "label" where I can input my label text successfully. The problem arises when I need to apply a different s ...

Replicate the actions of CSS using jQuery

I am trying to create a button that will have the same behavior using jQuery as it does with CSS. HTML: <br> <button class="pure-css">Test</button> <br> <br> <br> This is jQuery : <br> <button class="pure-jQue ...