How to Have the Look and Feel of An HTML on Screen devices
How to Have the Look and Feel of A4 in HTML on Screen Devices
I started building my resume using an old-fashioned HTML page and quickly realized that it doesn't work out of the box when opened on a screen. The content just flows endlessly without any page breaks, and it certainly didn’t look anything like an A4 document. To fix this, I had to apply a few tricks, including some CSS and a bit of JavaScript.
My goals were:
- To make my resume look the same on screens as it does when printed.
- To display separate pages on the screen, in the same way a PDF or Google Docs document does it.
Challenges When Building a Resume in HTML
When I began writing my resume, I quickly realized that browsers on screen devices don’t respect page dimensions like A4. Instead, they simply flow the content continuously, which means text and sections can spill over without any page breaks. To create distinct pages based on A4 dimensions, I had to manually handle page breaks whenever the content exceeded the height of a typical A4 page.
The primary solution involved using CSS @media print and page-break properties to control how content behaves when it’s too long for one page, and to apply JavaScript to handle dynamic content on screens.
1. Automatic Page Breaks with CSS (for Print)
To ensure content flows correctly when printed, I used the page-break-inside: avoid property. This property prevents splitting important elements like headings or large paragraphs between two pages, allowing the browser to automatically break content into pages as needed.
Example HTML for Printing:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>A4 Styled Document</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="page"> <h1>Title of Page 1</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p> </div> <div class="page"> <h1>Title of Page 2</h1> <p>Content will automatically break into new pages for printing...</p> </div> </body> </html>
CSS for Printing:
/* Define page layout for A4 size */ @media print { body { margin: 0; padding: 0; } .page { width: 210mm; height: 297mm; margin: 0; padding: 20mm; page-break-after: always; /* Automatically breaks after each page */ box-shadow: none; overflow: visible; /* Ensures content flows properly when printing */ } /* Prevent page breaks inside headers or large paragraphs */ h1, h2, h3, p { page-break-inside: avoid; } }
How This CSS Works:
- page-break-after: always; ensures content will break into a new page after each .page div in print mode.
- page-break-inside: avoid; prevents headers or large sections from being awkwardly split between two pages, which is essential for maintaining a clean, professional look when printed.
2. Handling Page Breaks on Screen with JavaScript
While CSS can handle page breaks for printing, screen devices don’t work the same way. Browsers do not automatically break content into distinct pages on screen, so I needed JavaScript to detect when content exceeds the A4 page size and split it into new pages dynamically.
Here’s how I managed that:
JavaScript for Dynamic Page Splitting:
window.onload = function () { const pages = document.querySelectorAll('.page'); pages.forEach(page => { if (page.scrollHeight > page.clientHeight) { splitPageContent(page); } }); }; function splitPageContent(page) { const newPage = document.createElement('div'); newPage.classList.add('page'); const contentToMove = page.innerHTML.slice(page.clientHeight / 2); newPage.innerHTML = contentToMove; page.innerHTML = page.innerHTML.slice(0, page.clientHeight / 2); // Insert the new page into the DOM after the current page page.parentNode.insertBefore(newPage, page.nextSibling); }
Why JavaScript Is Needed:
On screen, unlike print, text doesn’t automatically flow from one .page div to the next when it overflows. The browser simply lets the content spill beyond the page boundaries. With the JavaScript code above, I detect when content overflows and split it into a new page, simulating the multi-page experience you’d expect in a PDF or Google Docs.
3. Ensuring Consistent Page Flow on Screen and Print
With this approach, the content behaves consistently across devices:
On Screen: JavaScript detects content overflow and creates additional .page divs as needed. This simulates the experience of viewing an A4 document, where you can scroll between neatly divided pages.
In Print: CSS @media print and page-break properties handle automatic page breaks for printing, so your document will look just as polished on paper.
Conclusion
Building an HTML document that mimics the look and feel of A4 pages on screen devices requires a combination of CSS and JavaScript. While CSS alone can handle the printing aspect effectively, JavaScript is essential for managing page breaks on screen. By combining both, you can ensure a seamless experience, with text neatly flowing across pages, both on screen and in print.
The above is the detailed content of How to Have the Look and Feel of An HTML on Screen devices. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's
