gpt4 book ai didi

html - 分配不重叠的 div

转载 作者:可可西里 更新时间:2023-11-01 13:47:00 26 4
gpt4 key购买 nike

我正在尝试生成一个“A4”html 作为之前保存为 PDF 的模板,我的页面有 5 个 div,覆盖了 100% 的打印区域。

我为每个 div 使用了绝对位置,但不知何故它们有一点重叠,为什么会这样?

 body {
background: rgb(204,204,204);
}
page[size="A4"] {
background: white;
width: 210mm;
height: 297mm;
display: block;
margin: 0 auto;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}
@media print {
body, page[size="A4"] {
margin: 0;
box-shadow: 0;
}
}
.area100{
border:1px solid black;
position:absolute;
width:210mm;
}
.area50{
font-size:9px;
padding:10px;
text-align: justify;
border:1px solid black;
position:absolute;
width:105mm;
height:98mm;
overflow:hidden;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>

</head>
<body>
<page size="A4">
<div id="header" class="area100" style="height:20mm;">
This is header
</div>
<div id="main" class="area100" style="height:129mm; top: 20mm;">
This is main
</div>
<div id="bottom-left" class="area50" style="top: 149mm">
{agreement}
</div>
<div id="bottom-right" class="area50" style="left:105mm; top: 149mm">
right
</div>
<div id="footer" class="area100" style="top: 247mm; height:50mm">
This is footer
</div>
</page>
</body>
</html>

最佳答案

始终为 position: absolute; 设置 topleft 属性,因为浏览器会尝试猜测它,有时这不是您想要的。

此外,元素实际宽度由 width + padding 组成,因此当您设置 width: 105mm; padding: 10px; 比你的实际宽度是 105mm + 20px

body {
background: rgb(204, 204, 204);
}
page[size="A4"] {
background: white;
width: 210mm;
height: 297mm;
display: block;
margin: 0 auto;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
}
@media print {
body,
page[size="A4"] {
margin: 0;
box-shadow: 0;
}
}
.area100 {
border: 1px solid black;
position: absolute;
top: 0;
left: 0;
width: 210mm;
}
.area50 {
font-size: 9px;
padding: 10px;
text-align: justify;
border: 1px solid black;
position: absolute;
width: 99mm;
height: 98mm;
overflow: hidden;
top: 0;
left: 0;
}
<page size="A4">
<div id="header" class="area100" style="height:20mm;">
This is header
</div>
<div id="main" class="area100" style="height:129mm; top: 20mm;">
This is main
</div>
<div id="bottom-left" class="area50" style="top: 149mm">
{agreement}
</div>
<div id="bottom-right" class="area50" style="left:105mm; top: 149mm">
right
</div>
<div id="footer" class="area100" style="top: 247mm; height:50mm">
This is footer
</div>
</page>

关于html - 分配不重叠的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40975623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com