What is the best way to create a header and footer on each page of a document without having to duplicate the

Having trouble ensuring the header and footer are repeated on each printed page. It doesn't necessarily have to be a table, maybe it could be something else. Does anyone have any ideas on how to achieve this? Also, I'm not sure how many pages there will be, it could be any number of pages.

https://i.sstatic.net/0Gj4k.png

TRY: jsfiddle.net/7ZGVv/111

Answer №1

It appears that no one believes this is a duplicate, so I will share the code here as evidence. You can view a live example here.

Here is the HTML:

<h1>test 1</h1>
<h1>test 2</h1>
<h1>test 3</h1>
<h1>test 4</h1>
<h1>test 5</h1>
<h1>test 6</h1>
<div class="divFooter">
<h1>Footer</h1>
</div>
<div class="divHeader">
<h1>Header</h1>
</div>

CSS: Note that I inserted page breaks between each <h1> to mimic multiple pages

@media print {
  h1 {
    page-break-after: always;
  }
  div.divFooter {
    position: fixed;
    bottom: 0;
  }
  div.divHeader {
    position: fixed;
    top: 0;
  }
  body > h1 {margin-top: 100px;}
}

@media screen {
  div.divFooter, div.divHeader {
    display: none;
  }
}

Answer №2

Unfortunately, HTML and CSS do not inherently support printing page headers and footers. However, you can try to recreate this feature using:

  • tables
  • positioning fixed blocks

Keep in mind that this workaround may have some issues and limitations across various browsers.

For more details, check out: How to print headers and footers on every page using HTML?

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

What is the best way to place a div within another div?

I am facing a challenge where I need to insert a new div within another div before all its existing contents. The issue is that the outer divs do not have unique IDs, only classes as selectors. Unfortunately, I do not have control over the generation of th ...

Tips for adding templates dynamically in Vue 3

I'm working on implementing a Vue 3 component that can dynamically insert rows into a table. This is what I have so far: <template> <table> <tbody> <tr v-for="(item, index) in tableRows" :key=&qu ...

The words are a blank canvas in babel/js and react. How can I bring them to life?

https://i.sstatic.net/IdOon.jpg The words are written in white on this page. I didn't accomplish anything yesterday, but everything seemed fine. Is there anyone who can offer assistance? ...

What is the best way to incorporate an image into an element to maximize the available space?

How can I enlarge the image to fully occupy the space available in an element block, similar to the following image: (the black box represents the element box and the grey area is supposed to be the image.) https://i.sstatic.net/F0E82.png In the current c ...

Ways to apply hover effects to an element located outside of a div

Is there a way to make a sub-menu appear outside of the elements div that I'm hovering over? I've tried various methods, but none have been successful so far. The design requires the submenu to be outside of the links div. What is the most effec ...

Encountering an error when accessing dynamically routed pages in Next JS

When I click on a link within the web app to navigate to a dynamically routed page for a product (http://localhost/1), everything works as intended. However, if I manually input a specific product number in the search bar to navigate directly to that page ...

What is the best way to divide the dev-server.js file in Vue into multiple separate files?

Currently, I am utilizing vue.js to develop an application and have created a mock login api on localhost in dev-server.js. Now, I am looking to refactor the code related to the login api into a separate file. Do you have any suggestions on how I can ach ...

What is the resolution if I need to utilize a property that is untyped?

Transitioning to TypeScript from plain old JavaScript is a step I'm taking because I believe it offers significant advantages. However, one drawback that has come to light is that not all attributes are typed, as I recently discovered. For instance: ...

The concept of dynamic inheritance within Struts2's Tiles plugin adds a new

I am currently utilizing the Struts2 framework in conjunction with the tiles 2.0.6 plugin for my web application. Within my webapp, there is a primary "base" layout that comprises of a header, a footer, and the main page content: <definition name="shar ...

Resolving the issue with ASP.NET and the 'data' parameter in jQuery AJAX

I've been struggling with this code for hours, trying to figure out the correct parameters needed for my web service. After countless attempts, I'm turning to you for help. Here's the code in question: var args = [{ key: 'myId', v ...

Obtain information about a div element while scrolling

Looking to enhance my social feed page by adding a view count feature for each post. The challenge is figuring out how to keep track of views as the user scrolls down the page. Any suggestions? ...

Selenium struggles to update dropdown menu choice

Here is the HTML code snippet in isolation: <span style="position: relative; width: 100%; display: inline-block;"> <select id="0ac88542d16d6200fb983d094f655c76_select" class="form-control"> <option value="display_value">Numbe ...

The unrevealed node that is requesting the module is leading to an undefined outcome

I'm facing an issue where I need to import var server = require('../../index.js'); in my foo-dao.js file. This is necessary for accessing a hapi server plugin without passing it through the hapi request object from controller to dao. The pr ...

Ways to resolve Content Security Policy issue in a Node application

const express = require('express'); const graphqlHTTP = require('express-graphql'); const schema = require('./schema/schema'); const app = express(); app.use('/graphql', graphqlHTTP({ schema, graphiql: true, ...

Remember to follow the camel case convention when utilizing element.tagName(String) with jSoup

Looking to change the name of an HTML tag using Jsoup in the following way - element.tagName("p:panelGroup"); But when I run this code, the output changes the case and I end up with p:panelgroup. Is there a method to preserve the camel case in the resul ...

Creating a network of communication between API routes in NextJS

Can the fetch API be used within an API route in NextJs? I have a large handler and instead of having everything in one handler, I'd like to modularize it so that after completing a specific task (e.g., writing to a database), I can call another handl ...

When the Ionic 5 iOS dark theme is activated, the header casts a subtle shadow when the menu bar is opened or

I have been attempting to determine why there is shadow styling in the header when the menubar is closed in Ionic iOS. After inspecting the elements, I am unable to pinpoint exactly where this style is being applied. It seems to be related to the .toolba ...

React: executing function before fetch completes

Whenever I trigger the ShowUserPanel() function, it also calls the getUsers function to retrieve the necessary data for populating the table in the var rows. However, when the ShowUserPanel function is initially called, the table appears empty without an ...

Real-time charting with JQuery without using comet technology

Are there options for implementing real-time ajax charts with jQuery without requiring a comet server setup? I've come across plugins that promise real-time functionality but ultimately rely on setTimeout(). What are your thoughts on this? I'm l ...

Utilize deferred shading in three.js by employing gl_fragData for optimal rendering performance

Experiencing issues with deferred shading using three.js. Gl_fragData value not received properly. Seeking solutions. a Shader Code //shader1 <script id="vshader1" type="x-shader/x-vertex"> #extension GL_EXT_draw_buffers : require varying vec2 vU ...