DEV Community

websilvercraft
websilvercraft

Posted on

How to Have the Look and Feel of A4 in 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>
Enter fullscreen mode Exit fullscreen mode

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)