Include an HTML header and footer on every page when printing an HTML document

I designed an HTML page with a header, content, and footer. When I attempted to print the page, it spanned across 2 pages with the header on the first page and the footer on the last page.

My goal is to have the header and footer display on every page, similar to how it works in a Word document.

I discovered that using a table format with thead and tfoot achieves this result. It worked in Firefox and IE, but unfortunately not in Chrome. Could this be due to a webkit browser compatibility issue?

Is there another method that is compatible with all browsers?

Update: As of April 2021, the bug remains unresolved . as noted below by SHAKU here

Answer №1

If you want to style your document specifically for printing purposes, you can use the "@print" media query in CSS. This allows you to create custom styles that will only be applied when the document is being printed or in print preview.

Here is a solid starting point for creating print-specific styles:

@media print {

    * {
        color: #000 !important;
        -webkit-text-shadow: none !important;
        text-shadow: none !important;
        font-family: "Times New Roman", Times, serif;
        background: transparent !important;
        -webkit-box-shadow: none !important;
        box-shadow: none !important;
        border: none!important;
        font-weight: normal!Important;
    }

    header, nav, footer {
       overflow:visible;
    }

    .body {
        width: auto;
        border: 0;
        margin: 0 5%;
        padding: 0;
        float: none !important;
    }


    a, a:link, a:visited {

        &[href]:after {
            content: " (" attr(href) ") ";
            font-size: 90%;
        }

        &[href^="javascript:"],
        &[href^="#"] {
            &:after {
                content: "";
            }
        }
    }

    abbr[title]:after {
        content: " (" attr(title) ")";
    }

    pre,
    blockquote {
        border: 1px solid #999;
        page-break-inside: avoid;
    }

    thead {
        display: table-header-group;
    }

    tr,
    img {
        page-break-inside: avoid;
    }

    img {
        max-width: 100% !important;
    }

    @page {
        margin: 0.5cm;
    }

    p,
    h2,
    h3 {
        orphans: 3;
        widows: 3;
    }

    h2,
    h3 {
        page-break-after: avoid;
    }
}

Answer №2

To achieve a fixed position for certain elements based on print media type, you can utilize the following CSS code:

#navbar, #sidebar {
     display: none;
}
@media print
{
    #navbar, #sidebar {
         position: fixed;
         display: block;
         top: 0;
    }
    #sidebar {
         bottom: 0;
    }
}

(In this scenario, it is assumed that there are div elements with id navbar and sidebar).

Answer №3

After encountering a similar issue, I discovered that using basic HTML tags proved to be the most effective solution for me.

<table>
  <thead>
    <tr><td>
      Content to be displayed as the page header
    </td></tr>
  </thead>
  <tbody>
    <tr><td>
      Content that will appear on every printed page consecutively
    </td></tr>
  </tbody>
</table>

While the table appears like a standard table in the web browser, during printing, the content within the <thead> tag is repeated on each page without the need for any CSS styling.

This method has been tested successfully in various browsers including Firefox 32 and IE 11, as well as in Microsoft Word 2010. In Word, the tag isn't converted into traditional page headers, but instead replicates the content at the top of each page (switching to "Page View" may be necessary to observe this distinction).

Answer №4

It seems that none of the previously mentioned responses truly address the question at hand. Based on my own experiences, there doesn't appear to be a straightforward solution currently accessible. It almost appears as though Chrome is intentionally overlooking this issue. You can read more about it here: .

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

How to centrally align text in list items using Bootstrap 5

Incorporating Bootstrap 5 into my project, I am facing a challenge with vertically aligning text in an li element. Despite trying various techniques mentioned in the Bootstrap documentation for middle/center alignment, none of them seem to be effective. I ...

Explore the Shopify assistance on the secondary blog section within a separate page

Hey there, I'm looking for someone who is familiar with Shopify and can assist me. I am currently trying to set up a secondary Blog section on a separate page from my default 'Blog Page', but I'm encountering issues in displaying the a ...

Angular child component displaying of table data is not looping properly

Currently, I am using Angular 13 along with Bootstrap 3 to develop an application that consists of two main components: form-component (dedicated to inputting user details) and list-component (designed to showcase all data in a table format). Within the HT ...

Error: AngularJS: Invalid Argument Error. The argument 'ClientCtrl' is not defined as a function, it is currently undefined

As a newcomer to AngularJS, I am facing an issue while trying to add a controller to my website. Strangely, the other two controllers are functioning perfectly fine, but this particular one is not being recognized. Here is my app.js file: var app = angul ...

Steps for updating the homepage to display a personalized welcome message to the user after logging in and redirecting using node.js and

Upon arriving at the homepage, simply click on the sign-in button. Once redirected back to the home page, you will notice a personalized greeting saying 'Welcome [user]'. Wondering how to achieve this? It's as simple as changing the text fro ...

Having trouble with the JSFiddle dropdown button that is unresponsive to

I am currently in the process of developing a test drop-down menu. The open menu button functions correctly, however, the close button is unresponsive to clicking or hovering actions. Upon clicking the open menu button, it should make the other button visi ...

Tips for selectively applying CSS to a single HTML file

I'm currently working on a website utilizing AngularJS and Plangular to develop a unique Soundcloud Player. This is how I incorporate my template: <body> <div id="container"> <div id="header" ng-controller="HeaderCtrl"><div ...

Problem with JavaScript regular expressions and enumeration

Hello there! I'm facing a challenge on how to transform my text using ul and li tags. Let me explain: let text = "You are the best person"; I want to change the "the best person" part into: <ul> <li>the</li> <li>b ...

Place the HTML onto a fresh page

After rendering a control in asp.net code and converting it to HTML on a button click, the next step is to transfer the HTML to a new page in order to display all the information it contains. Is there a specific control in asp.net that can be utilized in ...

Instructions for changing the background color of a value

I'm working with a code that populates values in table columns, but the text displays in black color. I would like to highlight the text with a blue background. Here is the HTML code snippet: <body> <div class="container"> ...

Ensure that all elements with the :hover event have a text color of #fff in CSS

 Troubleshooting problems with block's child elements during the :hover event. Specifically, I have a pricing block where I want all texts to be of color #fff when hovered over. Currently, when hovering over the <h3> and <p> elements ...

"Selecting options from a range - Bootstrap multiple

Currently, I am in the process of implementing a multiple-choice tick box for PHP. The code I have so far is as follows: <div class="container"> <div class="row"> <div class="col-lg-6"> <div class="input-group"> <span class="inp ...

Activate hover effect on desktop screen (excluding tablets and phones) by utilizing media queries

Applying hover state styling to a div can be done like this: div { &:hover { color: red !important; border: 3px solid red !important; } } To exclude iPads and other tablets from this CSS style, you can util ...

In what way can you reach an unfamiliar form within a controller?

I am working with multiple dynamically generated forms, each associated with a different model. In my controller, I need to iterate through all the errors within the forms. I assign form names based on the models. <form name="{{myForm}}" novalidate> ...

After clicking, the radio button in Bootstrap 4 shifts to the left

I encountered an issue where the radio button I added to a table using Bootstrap 4 moves slightly to the left whenever I click on it. To better illustrate the problem, I have included a GIF below: https://i.sstatic.net/4aj8f.gif Below is the code for one ...

Glitch in Safari 6: Round Corners Issue

Greetings! Upon observing the image provided, an unusual behavior can be noticed in Safari 6 concerning round corners. { border-radius: 5px 5px 5px 5px; } Upon hovering over the elements, a thin line appears that seems to accumulate with each subsequent ...

Hover over an element to trigger the background fading effect on the parent div

I'm looking to create a hover effect on an element within a fullscreen div. When I hover over the element, I want a background image to fade in on the div with the class ".section." While I can easily display the background image, I am unsure of how t ...

Modify the input field for weight in WooCommerce to accommodate alphabetic characters

Is there a way to modify the WooCommerce Shipping weight field so that it can accept alphanumeric values, like: 500g / 1kg / 750ml / 2L I only need the weight for informational purposes and don't require any shipping. The location of the file re ...

Is it possible to impose dimensions on a content-editable div?

Is there a way to make a content editable div act like a text box with a fixed horizontal width and unlimited vertical expansion? Take a look at this jsfiddle, and find the code below: http://jsfiddle.net/seoj7cgq/ <!--How do I force the text to stay ...

What could be the reason for the child elements not being centered in a bootstrap navbar in this basic code example

Take a look at the display output by clicking here -> http://www.bootply.com/VtPTgKeCLO Below is the code snippet: <nav class="navbar navbar-fixed-bottom top-main-navbar"> <div class="container"> < ...