Tailwind CSS Break Inside
The break-inside
utility in Tailwind CSS is used to control the behavior of column breaks inside multi-column elements, such as those created using the grid
or flex
layout. This utility can come in handy especially when working with long lists or tables that need to be split into multiple columns.
Syntax
The break-inside
utility can be added to any element in your HTML code to specify the behavior of column breaks inside it. The basic syntax of the utility is as follows:
<div class="break-inside-{value}">...</div>
Here, {value}
represents one of the following options:
auto
: Use the default behavior of the browser.avoid
: Avoid column breaks inside the element.column
: Always start a new column inside the element.avoid-column
: Avoid column breaks inside the element and force a new column before it.
Examples
Let's take a look at some examples of using the break-inside
utility in Tailwind CSS.
Example 1: Avoid column breaks
The following code will prevent any column breaks from occurring inside the ul
element:
<ul class="break-inside-avoid grid grid-cols-2 gap-4">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
</ul>
Here, we've used the grid
layout to create a two-column grid, and added the break-inside-avoid
utility to prevent column breaks inside the ul
element. This will ensure that all the list items appear in the same column.
Example 2: Force a new column before element
The following code will force a new column to start before the ul
element:
<div class="break-inside-avoid-column grid grid-cols-2 gap-4">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Nullam blandit felis in turpis fringilla, eget malesuada enim fermentum.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
</ul>
</div>
Here, we've used the break-inside-avoid-column
utility to avoid column breaks inside the ul
element and force a new column to start before it. This can be useful in cases where you want to start a new section or group of elements in a new column.
Output
Depending on the value of the break-inside
utility, the output of your code will be different. For example, if you use break-inside-avoid
to prevent column breaks inside an element, the output will be a single column with all the child elements appearing in it. On the other hand, if you use break-inside-column
, the output will be a multi-column layout with each column containing some of the child elements.
Explanation
The break-inside
utility is used to control the behavior of column breaks inside multi-column elements, such as those created using the grid
or flex
layout. By default, browsers will split elements into columns based on their size and positioning, which can sometimes lead to unwanted behavior, especially when dealing with long lists or tables.
With the break-inside
utility, you can specify whether column breaks should be allowed inside an element or not, and whether a new column should be started before it. This gives you greater control over the layout of your content and can make it easier to create complex multi-column designs.
Use
The break-inside
utility can be used in a variety of situations, such as:
- Preventing column breaks inside long lists or tables.
- Forcing certain elements to start in a new column.
- Splitting content into multiple columns for better readability.
It can be used with any element that uses the grid
or flex
layout and supports multi-column behavior.
Important Points
Here are some important points to keep in mind when using the break-inside
utility:
- The utility only affects column breaks inside an element, not page breaks or column gaps.
- The
break-inside
utility is not supported in all browsers, so be sure to test thoroughly. - The
avoid-column
value is not supported in all browsers and should be used with caution.
Summary
The break-inside
utility in Tailwind CSS is a useful tool for controlling the behavior of column breaks inside multi-column elements. By using this utility, you can prevent unwanted column breaks, force new columns to start, and create complex multi-column layouts with ease. Be sure to test your code thoroughly and use the utility with caution, especially when dealing with older browsers or unsupported values.