是否有可能使一个HTML页面的行为,例如,像一个a4大小的页面在MS Word?
实际上,我希望能够在浏览器中显示HTML页面,并以A4大小页面的尺寸概述内容。
为了简单起见,我假设HTML页面只包含文本(没有图像等),例如,不会有<br>标记。
另外,当HTML页面被打印出来时,它将显示为a4大小的纸质页面。
是否有可能使一个HTML页面的行为,例如,像一个a4大小的页面在MS Word?
实际上,我希望能够在浏览器中显示HTML页面,并以A4大小页面的尺寸概述内容。
为了简单起见,我假设HTML页面只包含文本(没有图像等),例如,不会有<br>标记。
另外,当HTML页面被打印出来时,它将显示为a4大小的纸质页面。
当前回答
A4尺寸为210x297mm
所以你可以用CSS设置HTML页面来适应这些大小:
html,body{
height:297mm;
width:210mm;
}
其他回答
强制web浏览器以与A4相同的像素尺寸显示页面是相当容易的。然而,在渲染时可能会有一些怪癖。
假设你的显示器显示72 dpi,你可以添加如下内容:
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 842px;
width: 595px;
/* to centre page on screen*/
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
</body>
</html>
<link rel="stylesheet" href="/css/style.css" type="text/css">
<link rel="stylesheet" href="/css/print.css" type="text/css" media="print">
在你的normal style.css中:
table.tableclassname {
width: 400px;
}
在你的print.css中:
table.tableclassname {
width: 16 cm;
}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"/>
<title>DIN A4 Page</title>
<style type="text/css">
@page { size: 21cm 29.7cm; margin: 2cm }
p { line-height: 120%; text-align: justify; background: transparent }
</style>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
</body>
</html>
我在谷歌上搜索“A4 CSS页面模板”(codepend .io)后看到了这个解决方案。它使用<page>标记在浏览器中显示A4 (A3,A5,也是纵向)大小的区域。在这个标记中显示内容,但绝对位置仍然相对于浏览器区域。
身体{ 背景:rgb (204204204); } 页面{ 背景:白色; 显示:块; 保证金:0自动; margin-bottom: 0.5厘米; 箱影:0 0 0.5cm rgba(0,0,0,0.5); } 页面(=“A4大小){ 宽度:21厘米; 高度:29.7厘米; } 布局页面(大小= " A4 "][=“肖像”]{ 宽度:29.7厘米; 高度:21厘米; } @media print { 正文,页{ 保证金:0; 不必:0; } } <页面大小= >“A4 A4页面< / > <page size="A4" layout="portrait">A4 portrait</page>
这适用于我没有任何其他css/js库包括在内。适用于当前的浏览器(IE, FF, Chrome)。
我从1998年开始在商业报告中使用680px打印A4页。700px不能正确地拟合取决于边缘的大小。现代浏览器可以缩小页面以适应打印机上的页面,但如果您使用680像素,几乎在任何浏览器中都可以正确打印。
运行代码片段并查看结果:
window.print (); <table width=680 border=1 cellpadding=20 cellspacing=0> < tr > < th > # < / th > < th >名称< / th > < / tr > < tr对齐=中心> < td > 1 < / td > < td >唐纳德·特朗普< / td > < / tr > 2 < tr对齐=中心> < td > < / td > < td >巴拉克•奥巴马(BARACK OBAMA) < / td > < / tr > 表> < /
JSFiddle:
https://jsfiddle.net/ajofmm7r/