当前位置:网站首页>浏览器打印边距,默认/无边距,占满1页A4
浏览器打印边距,默认/无边距,占满1页A4
2022-07-06 09:28:00 【梦想身高1米8】
长页面打印的时候,分页是通过给每页内容的外层div设置高度实现的,以达到一页内容刚好占据1页A4纸。
(至少我遇到的情况是这样)
但是,如何设置宽高以及边距,真是个讲究活。
1 设置宽高为210mm*297mm
<html>
<head>
<style> body {
margin: 0; } div {
width: 210mm; height: 297mm; } div:nth-of-type(1) {
background-color: cadetblue; } div:nth-of-type(2) {
background-color: blueviolet; } </style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
210*297mm是A4纸的尺寸,但是如果加上打印默认的边距,效果就有点不对劲了。
如果这个边距不是必要的,那么可以考虑去掉边距。
@media print {
// 媒体查询,打印时样式生效,不用也可以
@page {
// 用于在打印文档时修改某些 CSS 属性
margin: 0;
}
}
}
或者打印设置里改成无边距(不推荐这种办法,需要客户多干一个步骤,不如设置css直接)
如果一定要边距,那么设置宽高的时候,就把边距减掉。
<html> <head> <style> body {
margin: 0;
}
div {
width: 190mm;
height: 277mm;
}
div:nth-of-type(1) {
background-color: cadetblue;
}
div:nth-of-type(2) {
background-color: blueviolet;
}
@media print {
@page {
margin: 10mm;
}
}
</style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
细看其实分页有一丝丝错位,不知道为啥。
以及,既然在css中设置了边距,就不要去调整浏览器打印边距,样式会乱。
设置宽高为a:b
px
在无边距时,这个a:b
=210:297
就能达到占满a4纸的效果了
(其实就是a4纸的比例了)
<html>
<head>
<style> body {
margin: 0; } div {
width: 1024px; height: 1448px; } div:nth-of-type(1) {
background-color: cadetblue; } div:nth-of-type(2) {
background-color: blueviolet; } @media print {
@page {
margin: 0mm; } } </style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
1024:1448差不多就是A4纸的比例了。
无边距时,宽高比需要调试。
<html>
<head>
<style> body {
margin: 0; } div {
width: 1024px; height: 1471px; } div:nth-of-type(1) {
background-color: cadetblue; } div:nth-of-type(2) {
background-color: blueviolet; } @media print {
@page {
margin: 20px; } } </style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
1024:1471
是调整出来的,我也不知道要怎么算。
边栏推荐
- [exercise-4] (UVA 11988) broken keyboard = = (linked list)
- 【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
- 1013. Divide the array into three parts equal to and
- [exercise-2] (UVA 712) s-trees
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- 读取和保存zarr文件
- b站 實時彈幕和曆史彈幕 Protobuf 格式解析
- QNetworkAccessManager实现ftp功能总结
- Is the sanic asynchronous framework really so strong? Find truth in practice
- 605. Planting flowers
猜你喜欢
【练习-7】Crossword Answers
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
QT模拟鼠标事件,实现点击双击移动拖拽等
Write web games in C language
树莓派4B安装opencv3.4.0
Penetration test (3) -- Metasploit framework (MSF)
Information security - Analysis of security orchestration automation and response (soar) technology
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
1323. Maximum number of 6 and 9
QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
随机推荐
[exercise-3] (UVA 442) matrix chain multiplication
[exercise -10] unread messages
Maximum product (greedy)
628. Maximum product of three numbers
Borg maze (bfs+ minimum spanning tree) (problem solving report)
【练习-9】Zombie’s Treasure Chest
Penetration test (8) -- official document of burp Suite Pro
1005. Maximized array sum after K negations
[exercise-6] (UVA 725) division = = violence
最全编程语言在线 API 文档
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
860. Lemonade change
Codeforces Round #802(Div. 2)A~D
1605. Sum the feasible matrix for a given row and column
Sanic异步框架真的这么强吗?实践中找真理
[exercise 4-1] cake distribution
If you want to apply for a programmer, your resume should be written like this [essence summary]
[exercise-7] crossover answers
Socket communication
[exercise-2] (UVA 712) s-trees