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:
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.
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.
<!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>
/* 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; } }
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:
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); }
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.
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.
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.
以上是How to Have the Look and Feel of An HTML on Screen devices的详细内容。更多信息请关注PHP中文网其他相关文章!