首页 web前端 css教程 How to Have the Look and Feel of An HTML on Screen devices

How to Have the Look and Feel of An HTML on Screen devices

Sep 21, 2024 am 06:26 AM

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.

以上是How to Have the Look and Feel of An HTML on Screen devices的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1675
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
静态表单提供商的比较 静态表单提供商的比较 Apr 16, 2025 am 11:20 AM

让我们尝试在这里造成一个术语:“静态表单提供商”。你带上html

使Sass更快的概念证明 使Sass更快的概念证明 Apr 16, 2025 am 10:38 AM

在一个新项目开始时,Sass汇编发生在眼睛的眨眼中。感觉很棒,尤其是当它与browsersync配对时,它重新加载

每周平台新闻:HTML加载属性,主要的ARIA规格以及从iframe转移到Shadow dom 每周平台新闻:HTML加载属性,主要的ARIA规格以及从iframe转移到Shadow dom Apr 17, 2025 am 10:55 AM

在本周的平台新闻综述中,Chrome引入了一个用于加载的新属性,Web开发人员的可访问性规范以及BBC Move

带有HTML对话框元素的一些动手 带有HTML对话框元素的一些动手 Apr 16, 2025 am 11:33 AM

这是我第一次查看HTML元素。我已经意识到了一段时间,但是尚未将其旋转。它很酷,

纸张形式 纸张形式 Apr 16, 2025 am 11:24 AM

购买或建造是技术的经典辩论。自己构建东西可能会感觉更便宜,因为您的信用卡账单上没有订单项,但是

托管您自己的非JavaScript分析的选项 托管您自己的非JavaScript分析的选项 Apr 15, 2025 am 11:09 AM

有很多分析平台可帮助您跟踪网站上的访问者和使用数据。也许最著名的是Google Analytics(广泛使用)

'订阅播客”链接应在哪里? '订阅播客”链接应在哪里? Apr 16, 2025 pm 12:04 PM

有一段时间,iTunes是播客中的大狗,因此,如果您将“订阅播客”链接到喜欢:

See all articles