JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > HTML >

Pagination when printing in HTML

Author:JIYIK Last Updated:2025/04/19 Views:

This article will introduce several ways to force page breaks when printing HTML content.


Set the page-break-after property to always Inside @media print to Page Break in HTML

The @media print rule of CSS lets us apply rules when printing HTML content. We can change the style using this rule.

We can use JavaScript to create an onclick()event to print the specified HTML content. The window.print() method prints the current window.

We can use onclick()the event to call this method.

In CSS, using @media print, we can set the page-break-after property to always so that the HTML element starts breaking at that point.

For example, create two headings using the h1 tag. Name the first heading Before Page Break and the other After Page Break.

Give the first heading a class name of break-page. Then create a button Print and add an onclick listener to the button and call the window.print() method.

Select the pagination class in the @media print rule in the CSS section. Next, set its page-break-after property to always.

The following example shows that when we click the print button, the title before the page break appears on the first page, and the second title and button appear on the second page when printing. We can use the page-break-after property to break pages when printing HTML.

Sample code:

<h1 class="break-page">Before page break </h1>
<h1>After page break</h1>
<button onclick="window.print()">Print</button>
@media print {
    .break-page {
        page-break-after: always;
    }
}

Set the page-break-before property to always Inside @media print to Page Break in HTML

This method is very similar to the previous one. The difference is that we are using the page-break-before property.

It @mediais used similarly in the print rules. This method breaks the page before the HTML content, where we use the page-break-before property.

For example, create two headings like in the first method. This time, give the second heading the class name break-page.

Next, create a window.print()button with an onclick listener that calls the method. In the CSS section @media print, select the break-page class and set its page-break-before property to always.

The page break will appear before the second heading and after it when you click the Print button.

Sample code:

<h1>Before page break </h1>
<h1 class="break-page">After page break</h1>
<button onclick="window.print()">Print</button>
@media print {
    .break-page {
        page-break-before: always;
    }
}

Set the break-after property to pagination to break pages in HTML

We can set the break-after attribute of an element to page to break pages in HTML. It will break the page after the element on which the attribute is used.

window.print()We can then use printHTML page just like the above method . This method is different from the above method because it does not use @media print rule.

For example, create two headings as usual. Between the headings, create an empty div and give it a class name of break-page.

Next, window.print()create a clickable button with an onclick listener that calls the method.

Select the break-page class and set its break-after property to page in CSS.

When printing, the page breaks after the first title. In this way, we can use the break-after attribute in HTML to break the page.

Sample code:

<h1>
Before page break
</h1>
<div class="break-page"></div>
<h1>
After page break
</h1>
<button onclick="window.print();" />Print</button>
.break-page {
    break-after: page;
}

Previous: None

Next:The role attribute in HTML

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Creating a currency input in HTML

Publish Date:2025/04/19 Views:119 Category:HTML

In this article, users will learn to input convert a normal HTML field into a currency input field. We will use HTML and jQuery to achieve our goal. input Convert plain to currency input in HTML using JavaScript toFixed() method In HTML, th

HTML Hide Buttons and Show Them Using Onclick

Publish Date:2025/04/19 Views:178 Category:HTML

This article will show you several ways to hide HTML buttons and make them visible using the onclick event. Using the CSS display property to show hidden buttons in HTML We can first hide the HTML button by setting its display property to n

Right align button in HTML

Publish Date:2025/04/19 Views:136 Category:HTML

Alignment is the positioning of text or buttons in different places like right, left or center on an HTML web page. We can easily use the alignment properties to place our text, images or other elements anywhere on the web page. There may b

Readonly in HTML Select Tag

Publish Date:2025/04/19 Views:176 Category:HTML

TML, Hypertext Markup Language, is widely used to format websites. Also, CSS (Cascading Style Sheets) can be used to style websites. Below are some of the attributes used in HTML and CSS to control user and cursor behavior. select readonly

The role attribute in HTML

Publish Date:2025/04/19 Views:142 Category:HTML

In this article, we will introduce the HTML role attribute and its uses. Introduction to the role attribute in HTML The role attribute in HTML belongs to the WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications) sp

HTML 中的 role 属性

Publish Date:2023/05/06 Views:354 Category:HTML

本篇文章介绍 HTML role属性。HTML中 role 属性介绍,role 属性定义描述语义的 HTML 元素的角色。

在 HTML 中打印时分页

Publish Date:2023/05/06 Views:681 Category:HTML

本篇文章介绍如何在打印 HTML 页面或内容时强制分页。将 page-break-after 属性设置为 always Inside @media print to Page Break in HTML

在 HTML 中显示基于 Cookie 的图像

Publish Date:2023/05/06 Views:187 Category:HTML

本文介绍如何根据 HTML 中的 cookies 显示图像。根据 HTML 中设置的 Cookies 显示图像。问题陈述是我们需要根据网页传递的cookie来显示特定的图像。

在 HTML 中创建下载链接

Publish Date:2023/05/06 Views:382 Category:HTML

本文介绍如何在 HTML 中创建下载链接。使用 download 属性在 HTML 中创建下载链接.。我们可以使用 HTML 锚元素内的下载属性来创建下载链接。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial