Display the footer of the table on the bottom of the final page

I have implemented a table as a footer on all my pages, which seems to work fine in Firefox. You can view the project on JS Fiddle: https://jsfiddle.net/j9k2xzze/

If you want to see how it looks properly, right click on the output pane, select "This Frame," and then choose "Open Frame in New Tab." This will allow Print Preview to work correctly.

<table id="wrapper">
    <thead>
    <tr>
        <td id="header"></td>
    </tr>
    </thead>

    <tfoot>
    <tr>
        <td colspan="0" id="footer">
            <img src="footer.jpg"/>
        </td>
    </tr>
    </tfoot>

    <tbody>
    <tr>
        <td id="content">
            <?php echo $html; ?>
        </td>
    </tr>
    </tbody>
</table>

However, I am facing an issue where on the last page, the table's footer is appearing directly below the text. If the text content is shorter than the page, the footer sticks to it.

I would like the footer to always be at the very bottom of the last page. I tried using the @page extension to achieve this, but it doesn't seem to work in Firefox. Here's what I attempted:

@page:last {
  #footer {
    position: absolute;
    bottom: 0;
  }
}

Answer №1

If you are exclusively supporting Firefox, the solution is quite straightforward. (To view an alternative technique that also functions in IE but with limited versatility, skip to the edit section. At the time of this answer, Chrome and other webkit browsers did not support repeating footers)

All you need to do is add a substantial bottom margin at the end of your content. The exact size does not have to be precise, but it should be large enough to extend past the end of the page. It is recommended to make it at least as big as the maximum paper size your users might use.

Do not worry, adding this margin will not result in a blank page at the end of your document. Margins behave differently from other white space forms (like padding and <br> tags) as they cancel out when surpassing the page boundary (see spec, section 13.3.3). While both Firefox and IE discard the margin, Firefox still generates a footer at the page's bottom as if a page break has occurred. On the contrary, IE acts as if the margin never existed, hence why this method fails in that browser.)

To keep your HTML clean, you can place the margin on a pseudo-element. Additionally, you can utilize @media print to prevent it from displaying on the screen.

Below is the code snippet. To witness its functionality in Firefox, access this jsfiddle, right-click on the output, choose This Frame > Show Only This Frame, and initiate a print preview.

@media print {
  #content:after {
    display: block;
    content: "";
    margin-bottom: 594mm; /* must exceed largest paper size supported */
  }
}
<table>
  <thead>
    <tr>
      <th>PAGE HEADER</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td>PAGE FOOTER</td>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td id="content">
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>...
      </td>
    </tr>
  </tbody>
</table>


EDIT

An alternate approach that is effective in both Firefox and IE involves placing the footer in a separate <div> fixed at the bottom of the page, utilizing the repeating <tfoot> as a spacer. However, there are some minor drawbacks to this method (explained below the snippet).

The following code illustrates this technique. To observe it functioning in Firefox, visit this jsfiddle, right-click the output, select This Frame > Show Only This Frame, and proceed to a print preview. In IE, click within the output frame, press CTRL+A, open a print preview, and switch "As Laid Out On Screen" to "As Selected On Screen".

@media print {
  #spacer {height: 2em;} /* footer height + extra space */
  #footer {
    position: fixed;
    bottom: 0;
  }
}
<table>
  <thead>
    <tr>
      <th>PAGE HEADER</th>
    </tr>
  <thead>
  <tfoot>
    <tr>
      <td id="spacer"></td>
    </tr>
  <tfoot>
  <tbody>
    <tr>
      <td>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content<br>
        content<br>content<br>content<br>content<br>content<br>content<br>content...
      </td>
    </tr>
  </tbody>
</table>

<div id="footer">
  PAGE FOOTER
</div>

A limitation of this method is that it places an identical footer on every page within the print job, meaning different footers or no footer for certain pages are not possible. Also, since the spacer's height depends on the footer's height, adjustments will be required if the footer height changes.

Answer №2

If you're looking to have a footer only on the last page without using absolute or fixed positioning, there is no straightforward way to achieve this. However, you can implement a sticky footer technique that doesn't rely on absolute or fixed positioning. Here's an example:

html, body {
  height: 100%;
  margin: 0;
}
.wrapper {
  min-height: 100%;
  margin-bottom: -50px;
}
.footer,
.push {
  height: 50px;
}

If you absolutely need a footer at the bottom of each page, my suggestion would be to generate a PDF first and then print it. Printing complex content with just CSS can be quite limiting, so generating PDFs may be the best solution in such cases.

Answer №3

Hey there! I've just implemented a new media query in your CSS file. And guess what? It's SUCCESSFULLY WORKING

@media print {
 #footer {
    position: absolute;
    bottom: 0;
  }
}

Take a look at the demo below:

https://jsfiddle.net/jjsqgaza/

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

Creating multiple thumbnails and storing them in various folders using CodeIgniter

I am looking to organize my images into different folders - one for original pictures and another for thumbnails. However, when I try to upload an image to the thumb directory, it shows up as empty. https://i.stack.imgur.com/5Fybz.png Below is the code s ...

What steps can be taken to display database results solely for the user currently logged in and created by them?

Currently, I'm in the midst of a project that involves extracting an HTML list from JSON data using JavaScript. The list is being displayed on the logged-in user's profile, showcasing job listings from the JSON data. While I've successfully ...

Vue 2 does not properly transition the initial element when using the `mode=out-in` attribute

In my Vue project, I am using version 2.6.14 and trying to toggle between two buttons by utilizing v-if and v-else. Despite wrapping everything in a transition element, the first button doesn't transition smoothly, neither when appearing nor disappear ...

Adjust the translateZ value according to the dynamic height of the element

A unique rotating cube effect has been developed using 3D css transformations. To achieve this, the following transform is applied to a child element: transform: translateY(-50%) translateZ(-75px) rotateX(90deg) ; In addition, the parent element contain ...

What is the best way to create divs with no gaps in between them?

I am trying to create a div structure that resembles the one shown below, with one div on top and two divs under it. I would prefer not to use tables, ensuring there is no space between the divs, and if possible, having collapsed borders. _________ | ...

Siblings are equipped with the advanced selector feature to

I'm struggling to understand this setup I have: <div class="container"> for n = 0 to ... <a href="some url">Link n</a> endfor for each link in ".container" <div class="poptip"></div> ...

My email address is not receiving emails from the PHP mail function

After extensive research, I am still unable to figure out why this email form is not sending messages to my address. Below is the HTML form code: <form id="contactMe" name="contact" method="post" novalidate="novalidate"> <fieldset> ...

Expanding HTML Editor's Functionality in Eclipse with Custom Tags

Creating an application involves incorporating custom tags within HTML code, which are processed on the server side before being presented to the end user as valid HTML. An example of using custom tags is shown below: <html> <body> ... < ...

"Owlcarousel Slider: A beautiful way to showcase

Currently, I am working on a project that involves implementing a slider. Since I lack expertise in JavaScript, I opted to use a plugin called owlcarousel. The challenge I'm encountering relates to the sizing of the container for the items. I'm ...

Align the text at the center within a relative div without specifying its size

In the structure of my HTML, I have the following simplified layout: <table> <tr> <td> <div class="PromptContainer"> <span class="Prompt">Prompt text</span> </div> <input type="t ...

Arranging elements within a div using inline positioning

Currently, I am trying to arrange elements within a div. Below is the div that needs positioning: https://i.sstatic.net/5nZUt.jpg This is the desired layout: https://i.sstatic.net/vXSV9.jpg Here is the HTML and CSS code snippet: #channel-header * { ...

code, scripting - modal pop-up

My script currently has a popup window using window.open, but most browsers block this type of popups. I now want to change it to another popup that includes a php script, similar to what I've seen on other sites. It seems like they are using ajax. Ca ...

Open the navigation menu by clicking on either the navigation links or anywhere outside of it

Seeking a solution for my mobile navigation menu that closes when clicking outside the links or on one of them, without using jQuery. How can I achieve this with JavaScript ES6? Current code snippet: const navSlide = () => { const burger = docum ...

Issue with dropdown list removeClass function

I am encountering an issue with the Jquery dropdown list click function. The addClass method is working properly, but the removeClass method is not functioning as expected. When I click on the dropdown list, it does not hide. Here is a live demo: http://j ...

The image filter plugin is functioning properly in one project, however, it is not working in

After successfully using a plugin from w3schools to filter elements in one project, I encountered an issue when attempting to implement it in another project. Here is the link to the problematic code pen: https://codepen.io/zakero/pen/mZYBPz If anyone ca ...

Running JavaScript code on a webpage using Selenium

I'm currently developing an automated script to enter a person's address details on a webpage. It's important to note that I did not create this webpage myself. While filling in the address details, I encountered an option to select the coun ...

Customizing nvd3 chart colors with CSS: A step-by-step guide

Currently, I am faced with a challenge in SugarCRM as I try to modify the colors of nvd3 charts using CSS. The pie chart colors are structured differently compared to other nvd3 charts, making it difficult for me to figure out how to customize them through ...

Vue - Embedding <div> elements in a recursive manner

I am attempting to recursively add a div element that includes a button in the following manner: <div v-for= "column in 25": key="column.id" class ="columna"> <div v-for="row in 10": key="row.id" ...

Attempting to retrieve div measurements in vh/vw units rather than pixels

I'm currently working on an app where I want the divs to be resizable. In order to achieve this, I need to store the user's changes in my database. My main concern is how can I convert the units provided in pixels (px) to a different unit of mea ...

transferring information from a JavaScript variable to an EJS variable

I am currently storing a value in the 'items' variable within a script tag in an HTML document. However, I now need to pass this data from 'items' to the back end in order to store it in a database. I attempted to make 'selectedTea ...