HTML DOM Style pageBreakBefore 属性
htmldomweb developmentfront end technology更新于 2024/1/12 22:04:17
HTML DOM Style pageBreakBefore 属性返回并修改 HTML 文档中 HTML 元素之前的打印或打印预览的分页行为。
语法
以下是语法 −
1. 返回 pageBreakBefore
object.pageBreakBefore
2. 修改 pageBreakBefore
object.pageBreakBefore = “value”
此处,值可以为负号。
| 值 | 说明 |
|---|---|
| Initial | 将此属性值设置为默认值。 |
| inherit | 从其父元素继承此属性值。 |
| auto | 如有必要,在 HTML 文档中的元素前插入分页符。 |
| always | 始终在 HTML 文档中的元素前插入分页符。 |
| avoid | 避免在 HTML 文档中的元素前插入分页符。 |
| left | 如果在元素前插入一个或两个分页符,则下一页可视为左页。 |
| right | 如果在元素前插入一个或两个分页符,则下一页可视为右页。 |
| Empty string(“”) | 在 HTML 文档中,元素前不会插入分页符。 |
让我们看一个 HTML DOM Style pageBreakBefore 属性的示例 −
示例
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
background-color: #8BC6EC;
background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
p {
text-align: center;
}
</style>
<body>
<h1 style="text-align:center">DOM Style pageBreakBefore Property Demo</h1>
<p>
Hi! I'm a para element with some dummy text. Hi! I'm a para element with some dummy text.
</p>
<p class="page-break">
Hi! I'm second para element with some dummy text. Hi! I'm a second element with some dummy text.
</p>
<button onclick="set()" class="btn">Break Page Here</button>
<p>
Hi! I'm another para element with some dummy text. Hi! I'm another para element with some dummy text.
</p>
<script>
function set() {
document.querySelector(".page-break").style.pageBreakBefore = "always";
}
</script>
</body>
</html>
输出

现在打开打印预览,观察我们的 html 页面会如何显示。

现在点击网页上的"红色"按钮,然后再次打开打印预览,再次观察我们的网页。在这里,您可以清楚地看到我们的网页被拆分成了两个页面。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

