Print-ready guide to CSS and HTML

If I were to embark on the ambitious task of writing a book solely using HTML and CSS, where would I begin in defining a page header and footer with page numbers? How can I ensure that page breaks and margins are displayed in the browser, akin to a preview mode?

I understand this may seem like a request to have the code handed to me, but what I truly seek are pointers to resources that can guide me through this unconventional project. I am at a loss on how to even initiate such a venture.

My vision for the browser preview is to have miniature versions of each page, reminiscent of a PDF viewer, where text overflow triggers a page-break:

While I have explored @media print, I have yet to find a way to incorporate headers and footers within it.

My attempts to implement the code from w3.org have been unsuccessful:

title { position: running(header) }
@page { @top-center {
  content: element(header) }
}

Although I found the code in Boom! helpful for printing purposes, it does not render the same in the browser.

With that said, I am in need of guidance on where I should turn to for a solid starting point on this endeavor. Any recommendations?

Answer №1

Here is the approach I took:

<div class='book'>
    <div class='chapter'>
        <div class='section'></div>
        <div class='content'></div>
    </div>
</div>

All the content was nested within the <div class='book'>. Each .chapter contained a .section as well as the main .content for that chapter.

To ensure the content fit within each section, I utilized JavaScript to trim any overflow from the .content. I then duplicated the .chapter, modified the chapter number within the newly created <div>, and populated the duplicated chapter with the necessary content.

This process continued until I reached around 100 chapters, ensuring that nothing spilled out of the final chapter's <div>.

Answer №2

Assuming that you are working with plain HTML and not incorporating any code in the background.

If there happens to be code behind, the process would be different.

Start by defining a main div with the class of page. Within that main div, create three sub-divs for the header, content, and footer sections.

.page {
   display: block;
   height: 800px; 
   width: 100%;
   /*Adjust margin based on your needs.*/
}

.header {
   display: block;
   height: 50px;
}

.content {
   display: block:
   height: 700px;
}

.footer {
   display: block;
   height: 50px;
}

Add any additional styling as required.

Create a separate stylesheet with a media type of "print".

In that stylesheet, include the following style for the page:

.page {
   display: block;
   height: 800px; 
   width: 100%;
   /*Adjust margin based on your needs.*/
   /*This will ensure a new page when printing*/
   page-break-after: always;
}

The HTML structure for a single page will resemble:

<div class="page">
  <div class="header">HEADER CONTENT</div>
  <div class="content"> MAIN PAGE'S CONTENT</div>
  <div class="footer"></div>
</div>

Repeat the above code as needed for the desired number of pages.

Alternatively, table layout can also be used for this layout purpose.

If code behind is in use, the content can be dynamically generated.

Hopefully, this explanation is beneficial to you.

Answer №3

Using pure HTML and CSS to create a PDF-like left panel may seem like a feasible option, but it could quickly become tedious due to the repetitive nature of coding similar blocks for each new page. To achieve the desired effect, Iframes could be utilized as a more efficient solution. More information can be found here

The process would involve creating a separate .htm page for each page of the book, linking them together via hyperlinks. Once all pages are complete, screenshots could be taken and saved as thumbnails. Another html page could then be created to serve as the panel, to be included in all other pages of the book, as demonstrated in the tutorial provided in the link.

UPDATE

To address page breaks, divs with the same class could be used and styled to mimic individual pages, as exemplified 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

Conceal all div elements except for displaying the initial two

Can an entire div be hidden with only the first 2 entities visible? <div class="inline-edit-col"> <span class="title inline-edit-categories-label">Brands</span> <ul class="cat-checklist product_brand-checklist"> < ...

Arrangement of cards in a column

As someone who is new to working with CSS, Wordpress, and similar technologies, I am seeking assistance. I am struggling with creating card layouts. My goal is to achieve a layout similar to this, where the cards are displayed in two columns instead of ju ...

Discovering the quantity of pagination tabs in Selenium WebAutomation

A critical element in the table "ContentPlaceHolder1_gvChannelList" is Pagination, located at the last row. I need to determine how many pages/columns are present in this pagination table. The code driver.findElements(By.id("//*[@id='ContentPlaceHold ...

What is causing the collapsed-animation to malfunction in Vue3?

Why won't the transition animation function in vue3js? The animation does not appear to be working for me. I've implemented this library https://github.com/ivanvermeyen/vue-collapse-transition <template> <nav class="navbar color-d ...

Issues with Collapse Feature on Bootstrap 3 Navbar

I need your help with a problem I'm facing. The navigation bar is not collapsing when I click the link in mobile view. Check out this video for more details: https://www.youtube.com/watch?v=2dBpcFMjqY0 ...

Converting JSON into HTML format

Seeking guidance as a developer tasked with rendering JSON to HTML on a webpage. I've taken over for someone else and, despite feeling somewhat in over my head, have managed to build the basic structure using Catalyst/Template Toolkit and Dojo. Now th ...

Searching for a table row that corresponds to the content of a cell using Python and Selenium

If you need to retrieve the row element from an HTML table where the text in a specific cell matches the string 'Mathematik & Informatik'. Here is the structure of the HTML: <table class="views-table cols-4"> <thead> ...

Challenges surrounding jQuery's .before

Currently, I am in the process of creating a simple carousel consisting of 4 divs. The carousel is utilizing 2 jQuery functions to position a div at either the first or last slot. The transitions being used are only alpha transitions as there is no need fo ...

Activate vertical scrolling in JavaScript when an image is clicked

I currently have a collection of button images with different hover effects specified like this: <img src="home.PNG" onmouseover="this.src='homemo.PNG'" onmouseout="this.src='home.PNG'" /> My goal is to make them scroll down to ...

Discovering an HTML Element in a JavaScript Array with Specific Styling: A Step-by-Step Guide

I am in the process of developing a website that consists of different sections. The concept is simple - by clicking on a button located at the bottom of the page, it will reveal the corresponding div section. For styling purposes, I have initially hidden ...

Switching Symbols with JQuery

Hello, I am brand new to utilizing javascript and I'm currently experimenting with the toggle function. My goal is to create show/hide functionality while also toggling arrow icons. Specifically, I want a right arrow next to the link "More -->" and ...

CSS - changing images for the menu based on user interaction

I'm in the process of designing a website and I have a unique challenge - I need to figure out how to create a dynamic triangle below an item on the menu bar using CSS. The catch is that the horizontal position of this triangle will shift based on wh ...

Creating a perpetual loop animation for two divs moving from right to left endlessly

Here is the code I have: HTML <div class="screen screen1"></div> <div class="screen screen2"></div> CSS .screen{ width: 100%; height: 50%; position: absolute; background-color:#001; background-image: radial- ...

What could be causing the alignment issue with cells in Bootstrap?

I'm currently facing an issue with Bootstrap where two columns that are supposed to appear side-by-side on medium resolution end up displaying one below the other. You can view a screenshot of the problem here, and the code can be found at this link. ...

Hover effects in CSS animations can be hit or miss, with some working smoothly while others may

Below is the HTML content: <div class="wrapper"> <figure align="center"><img src="http://www.felipegrin.com/port/or-site.jpg" alt=""><br><span>RESPONSIVE HTML5 WEBSITE FOR <br> OR LEDS COMPANY</span></fig ...

What are some techniques for applying CSS styling directly to a custom element?

Check out this amazing resource from HTML5 Rocks about the process of creating custom elements and styling them. To create a custom element, you can use the following code: var XFoo = document.registerElement('x-foo', { prototype: Object.crea ...

Changing the name of a tab within a p-tabview

Setting up a p-tabview with tabs containing specific content involves the following code: <p-tabView class="tabmain" > <ng-container *ngFor="let tab of tabs"> <p-tabPanel [header]="tab.header" > ...

Place the button inside the form following another block element

Here is the HTML code I am working with: <div class="actions"> <input id="submit-car" name="commit" type="submit" value="Добавить объявление"> </div> and here is a fiddle example: http://jsfiddle.net/348U4/ In ...

Activate a Dropdown Menu by Clicking in a React Application

I have a collapsible feature where you can click to expand or collapse a dropdown. Currently, the dropdown can only be clicked on where the radio button is located. I want the entire area to be clickable so that users can choose the dropdown by clicking an ...