CSS Properties
CSS break-before Property
Photo Credit to CodeToFun
🙋 Introduction
The break-before
property in CSS is used to control the page, column, or region break behavior before a specified element.
This property is particularly useful in paged media (such as printed documents) or multi-column layouts, allowing developers to dictate how content should flow across pages or columns.
💡 Syntax
The syntax for the break-before
property is straightforward and accepts several keyword values that dictate the break behavior.
element {
break-before: value;
}
🎛️ Default Value
The default value of the break-before
property is auto, which means no break is forced before the element, and the content flows naturally.
🏠 Property Values
Value | Description |
---|---|
auto | Default value. Neither force nor forbid a break before the element. |
always | Always force a page break before the element. |
avoid | Avoid a page break before the element. |
left | Force one or two page breaks before the element so that the next page is formatted as a left page. |
right | Force one or two page breaks before the element so that the next page is formatted as a right page. |
page | Force a page break before the element. |
column | Force a column break before the element in a multi-column layout. |
region | Force a region break before the element. |
📄 Example
In this example, we'll force a page break before a heading element in a printed document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS break-before Example</title>
<style>
@media print {
h2 {
break-before: page;
}
}
</style>
</head>
<body>
<h1>Chapter 1</h1>
<p>Content of the first chapter...</p>
<h2>Chapter 2</h2>
<p>Content of the second chapter...</p>
</body>
</html>
In this example, when the document is printed, a page break will be forced before the second heading (<h2>), ensuring that Chapter 2 starts on a new page.
🖥️ Browser Compatibility
The break-before
property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.
🎉 Conclusion
The break-before
property is a useful tool for web developers working with paged media or multi-column layouts. It allows for fine-grained control over content flow, ensuring that elements appear in the desired locations across pages or columns.
By understanding and using this property, you can create more professional and well-organized documents and layouts.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS break-before Property), please comment here. I will help you immediately.