You are currently viewing CSS: Page-Break-After – Specifying Page Break After Element

CSS: Page-Break-After – Specifying Page Break After Element

When dealing with printed documents or print previews, controlling how content flows from one page to the next is essential for ensuring a professional and readable layout. The page-break-after property in CSS is a key tool for managing page breaks in printed content. This property allows developers to specify whether a page break should occur after a particular element, ensuring that content is divided logically and aesthetically across multiple pages.

The page-break-after property can significantly enhance the readability and presentation of printed documents by preventing awkward breaks within sections or elements. By mastering this property, web developers can create more polished and user-friendly print layouts. In this article, we will explore the page-break-after property in depth, starting with basic setups and moving on to practical examples.

Basic Setup

Before we delve into the details of the page-break-after property, let’s set up a basic example to demonstrate its functionality. We’ll create a simple HTML structure with some CSS to define our elements and apply page break styles.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Page-Break-After Example</title>

    <style>

        .section {
            margin: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }

        .break-after {
            page-break-after: always;
        }

    </style>

</head>
<body>

    <div class="section">
        <h1>Section 1</h1>
        <p>This is the first section. It should be followed by a page break.</p>
    </div>

    <div class="section break-after">
        <h1>Section 2</h1>
        <p>This is the second section. It will cause a page break after this section.</p>
    </div>

    <div class="section">
        <h1>Section 3</h1>
        <p>This is the third section. It will appear on a new page.</p>
    </div>

</body>
</html>

In this example, we define a .section class with basic styling for the sections of our content. We then add a .break-after class that applies the page-break-after: always; property to ensure a page break occurs after the element it is applied to.

Understanding the page-break-after Property

The page-break-after property in CSS is used to control the placement of page breaks after an element when printing. The property can take the following values:

  • auto: Default value. Neither force nor forbid a page break after the element.
  • always: Always force a page break after the element.
  • avoid: Avoid a page break after the element.
  • left: Force one or two page breaks after the element, so that the next page is formatted as a left page.
  • right: Force one or two page breaks after the element, so that the next page is formatted as a right page.

By using these values, developers can manage how content flows across printed pages, ensuring a logical and visually appealing division of content.

Practical Examples of page-break-after

Let’s explore practical examples of using the page-break-after property with different values.

Example: Forcing a Page Break After an Element

In this example, we will apply the page-break-after property to force a page break after a specific element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Page-Break-After Always Example</title>

    <style>

        .section {
            margin: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }

        .break-after {
            page-break-after: always;
        }

    </style>

</head>
<body>

    <div class="section">
        <h1>Introduction</h1>
        <p>This is the introduction section. It should be followed by a page break.</p>
    </div>

    <div class="section break-after">
        <h1>Main Content</h1>
        <p>This is the main content section. It will cause a page break after this section.</p>
    </div>

    <div class="section">
        <h1>Conclusion</h1>
        <p>This is the conclusion section. It will appear on a new page.</p>
    </div>

</body>
</html>

In this example, the .break-after class applies the page-break-after: always; property to the second section. As a result, a page break occurs after the “Main Content” section, ensuring that the “Conclusion” section starts on a new page.

Using the page-break-after: always; property is useful for ensuring that important sections of content are not split across pages, maintaining the readability and flow of printed documents.

Example: Avoiding a Page Break After an Element

Let’s modify the example to use the page-break-after property to avoid a page break after a specific element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Page-Break-After Avoid Example</title>

    <style>

        .section {
            margin: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }

        .break-avoid {
            page-break-after: avoid;
        }

    </style>
</head>
<body>

    <div class="section">
        <h1>Part 1</h1>
        <p>This is part one of the content. It should not cause a page break after it.</p>
    </div>

    <div class="section break-avoid">
        <h1>Part 2</h1>
        <p>This is part two of the content. It will avoid causing a page break after this section.</p>
    </div>

    <div class="section">
        <h1>Part 3</h1>
        <p>This is part three of the content. It will continue on the same page if possible.</p>
    </div>

</body>
</html>

In this example, the .break-avoid class applies the page-break-after: avoid; property to the second section. As a result, a page break is avoided after the “Part 2” section, ensuring that the “Part 3” section continues on the same page if space permits.

Using the page-break-after: avoid; property is beneficial for keeping related content together on the same page, enhancing the readability and coherence of printed documents.

Example: Forcing Page Breaks for Left and Right Pages

Let’s explore how to use the page-break-after property to force page breaks for left and right pages.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Page-Break-After Left and Right Example</title>

    <style>

        .section {
            margin: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }

        .break-left {
            page-break-after: left;
        }

        .break-right {
            page-break-after: right;
        }

    </style>

</head>
<body>

    <div class="section">
        <h1>Section A</h1>
        <p>This section is followed by a left page break.</p>
    </div>

    <div class="section break-left">
        <h1>Section B</h1>
        <p>This section will be followed by a left page break.</p>
    </div>

    <div class="section break-right">
        <h1>Section C</h1>
        <p>This section will be followed by a right page break.</p>
    </div>

    <div class="section">
        <h1>Section D</h1>
        <p>This section continues after the forced page breaks.</p>
    </div>

</body>
</html>

In this example, the .break-left class applies the page-break-after: left; property to the second section, ensuring that the next page is formatted as a left page. Similarly, the .break-right class applies the page-break-after: right; property to the third section, ensuring that the next page is formatted as a right page.

Using the page-break-after: left; and page-break-after: right; properties allows for precise control over the formatting of printed documents, ensuring that content is appropriately divided across left and right pages.

Conclusion

The page-break-after property in CSS is an essential tool for managing page breaks in printed content. By using this property, developers can control how content is divided across multiple pages, ensuring a logical and visually appealing layout for printed documents.

By experimenting with different values for the page-break-after property and combining it with other CSS properties, designers can create sophisticated and professional print layouts. The examples provided in this article serve as a foundation, encouraging further exploration and creativity in using CSS and the page-break-after property to design visually appealing printed content.

Leave a Reply