Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Layout Classes

Tailwind CSS Layout Classes

Tailwind CSS provides a rich set of layout-related classes, allowing developers to control every aspect of a page in an extremely flexible way.

Next, we will explain some key layout-related classes in detail.

1. Aspect Ratio

The Aspect Ratio classes can help you set the aspect ratio of elements, avoiding stretching or compression in responsive design.

** Main Classes:**

Class Names CSS Property and Value Description
aspect-auto aspect-ratio: auto; This class keeps an element at its natural aspect ratio without any forced adjustment. It is suitable when you want the element to size automatically according to its content rather than being forced to fill or shrink to a specific aspect ratio.
aspect-square aspect-ratio: 1 / 1; This class forces an element's aspect ratio to 1:1, meaning width and height are equal, commonly used to create square or circular elements.
aspect-video aspect-ratio: 16 / 9; This class forces an element's aspect ratio to 16:9, a common video aspect ratio, suitable for elements that display video or simulate a video player.

Example

 

Video

Try it »

2. Container

The Container class can make a container element adapt responsively to different screen sizes, commonly used to set the maximum width of content.

** Main Classes:**

Class Names CSS Property and Value Description
container width: 100%; The container width defaults to 100%, occupying the entire width of the parent element.
sm:container max-width: 640px; When the screen width is greater than or equal to 640px, the container's maximum width is 640px.
md:container max-width: 768px; When the screen width is greater than or equal to 768px, the container's maximum width is 768px.
lg:container max-width: 1024px; When the screen width is greater than or equal to 1024px, the container's maximum width is 1024px.
xl:container max-width: 1280px; When the screen width is greater than or equal to 1280px, the container's maximum width is 1280px.
2xl:container max-width: 1536px; When the screen width is greater than or equal to 1536px, the container's maximum width is 1536px.

When using these classes, you can create containers with responsive maximum widths, which is very useful when creating layouts, especially when you want to limit content width for better readability.

Example

 

 

In these examples, the first

will always occupy 100% of the width, while the second
on large screens (lg) and larger screen sizes, its maximum width will be limited to 1024px.mx-autoclass is used to center a container horizontally.

3. Columns

The Columns classes are used to implement multi-column layouts.

You can control the number of columns and the spacing between them.

Main Classes:

  • columns-{n}: Set the number of columns.
  • space-x-{size}: Set the spacing between columns.
Class Names CSS Property and Value Description
columns-1 columns: 1; Set the number of columns of an element to 1.
columns-2 columns: 2; Set the number of columns of an element to 2.
columns-3 columns: 3; Set the number of columns of an element to 3.
columns-4 columns: 4; Set the number of columns of an element to 4.
columns-5 columns: 5; Set the number of columns of an element to 5.
columns-6 columns: 6; Set the number of columns of an element to 6.
columns-7 columns: 7; Set the number of columns of an element to 7.
columns-8 columns: 8; Set the number of columns of an element to 8.
columns-9 columns: 9; Set the number of columns of an element to 9.
columns-10 columns: 10; Set the number of columns of an element to 10.
columns-11 columns: 11; Set the number of columns of an element to 11.
columns-12 columns: 12; Set the number of columns of an element to 12.
columns-auto columns: auto; Set the element's column count to automatic, adjusting column widths automatically based on content.
columns-3xs columns: 16rem; /* 256px */ Set the element's column width to 16rem (256px).
columns-2xs columns: 18rem; /* 288px */ Set the element's column width to 18rem (288px).
columns-xs columns: 20rem; /* 320px */ Set the element's column width to 20rem (320px).
columns-sm columns: 24rem; /* 384px */ Set the element's column width to 24rem (384px).
columns-md columns: 28rem; /* 448px */ Set the element's column width to 28rem (448px).
columns-lg columns: 32rem; /* 512px */ Set the element's column width to 32rem (512px).
columns-xl columns: 36rem; /* 576px */ Set the element's column width to 36rem (576px).
columns-2xl columns: 42rem; /* 672px */ Set the element's column width to 42rem (672px).
columns-3xl columns: 48rem; /* 768px */ Set the element's column width to 48rem (768px).
columns-4xl columns: 56rem; /* 896px */ Set the element's column width to 56rem (896px).
columns-5xl `columns: 64rem; /*

Example

 

Column 1

 

Column 2

 

Column 3

Try it »

4. Pagination

The following classes can help developers better control the pagination and column layout of documents to improve the readability and aesthetics of printed output.

Main Classes:

  • break-after-{value}: Control the behavior after a page or column break.
  • break-before-{value}: Control the behavior before a page or column break.
  • break-inside-{value}: Control the behavior of page or column breaks inside an element.
Class Names CSS Property and Value Description
break-after-auto break-after: auto; Allow content to break naturally after this element into a new page or column.
break-after-avoid break-after: avoid; Try to avoid breaking after this element into a new page or column.
break-after-all break-after: all; Force a page or column break after this element, even if it is not a natural break point.
break-after-avoid-page break-after: avoid-page; Try to avoid a page break after this element, but allow a column break.
break-after-page break-after: page; Force a page break after this element, but do not allow a column break.
break-after-left break-after: left; Force a page or column break after this element, preferring the left page.
break-after-right break-after: right; Force a page or column break after this element, preferring the right page.
break-after-column break-after: column; Force a column break after this element, but do not allow a page break.

The above classes are used to control the page or column break behavior of content in print or column layouts:

  • break-after-auto allows the browser to decide whether to break into a new page or column after the element.
  • break-after-avoid tells the browser to try to avoid breaking into a new page or column after the element.
Class Names CSS Property and Value Description
break-before-auto break-before: auto; Allow content to break naturally before this element into a new page or column.
break-before-avoid break-before: avoid; Try to avoid breaking before this element into a new page or column.
break-before-all break-before: all; Force a page or column break before this element, even if it is not a natural break point.
break-before-avoid-page break-before: avoid-page; Try to avoid a page break before this element, but allow a column break.
break-before-page break-before: page; Force a page break before this element, but do not allow a column break.
break-before-left break-before: left; Force a page or column break before this element, preferring the left page.
break-before-right break-before: right; Force a page or column break before this element, preferring the right page.
break-before-column break-before: column; Force a column break before this element, but do not allow a page break.

The above classes are used to control the page or column break behavior of content in print or column layouts.

  • break-before-auto allows the browser to decide whether to break into a new page or column before the element.
  • break-before-avoid tells the browser to try to avoid breaking into a new page or column before the element.
Class Names CSS Property and Value Description
break-inside-auto break-inside: auto; Allow content inside this element to break naturally into a new page or column.
break-inside-avoid break-inside: avoid; Try to avoid breaking inside this element into a new page or column.
break-inside-avoid-page break-inside: avoid-page; Try to avoid a page break inside this element, but allow column breaks.
break-inside-avoid-column break-inside: avoid-column; Try to avoid a column break inside this element, but allow page breaks.

The above classes are used to control whether content in print or column layouts is allowed to break inside an element into a new page or column.

  • break-inside-auto allows the browser to decide whether to break inside the element into a new page or column.
  • break-inside-avoid tells the browser to try to avoid breaking inside the element into a new page or column.

Example

 

Well, let me tell you something,...

 

Of course, continuing with our content...

 

Maybe we can...

 

If you think this is...

In this example,

The element is set to a two-column layout (columns-2), which contains four

paragraphs. The second

paragraph has a break-after-column class, which forces a column break after that paragraph but does not allow a page break. This means if there is enough content, the content after the second paragraph will start a new column rather than continuing on the next line of the same column. The other paragraphs have no special column-break instructions and will flow naturally according to the content.

5. Text Wrapping and Breaking

The following are utility classes in Tailwind CSS for controlling how fragments of an element are rendered across multiple lines, columns, or pages, and their corresponding CSS properties:

Class Names CSS Property and Value Description
box-decoration-clone box-decoration-break: clone; Specify that when a box is split into multiple boxes (for example, at column or page boundaries), each box should clone the original box's decorations (borders, background, etc.).
box-decoration-slice box-decoration-break: slice; Specify that when a box is split into multiple boxes, each box should "slice" the original box's decorations so that the decorations appear continuous in each box.

The above classes are used to control how an element's decorations (such as borders and backgrounds) behave in multi-column layouts or when paginating.

  • box-decoration-clone makes each box fragment display the complete decoration.
  • box-decoration-slice "slices" the decoration where the box is split, keeping the decoration visually continuous.

These utility classes can help developers better control the visual effects of elements in complex layouts.

Example

  Hello


  World

  Hello


  World

Try it »

6. Box Sizing

Tailwind CSS provides two utility classes to control an element's box model (box-sizing):

  • box-border: Include padding and border when calculating the element's total size.
  • box-content: Exclude padding and border when calculating the element's size.

1、box-border

CSS Property:

box-sizing: border-box;

Use the box-border utility class to set an element's box-sizing property to border-box. This means when you specify a height or width for the element, the browser includes the element's border and padding.

If an element's size is 100px × 100px with a 2px border and 4px padding, the element's total rendered size will be 112px × 112px.

Example

 

2、box-content

CSS Property:

box-sizing: content-box;

Use the box-content utility class to set an element's box-sizing property to content-box. This means the browser adds the border and padding outside the element's specified width or height.

If an element's size is 100px × 100px with a 2px border and 4px padding, the element's actual rendered size will be 112px × 112px, with an inner content area of 100px × 100px.

Example

 

Try it »

display property

The display property is used to control an element's display type, determining how the element is presented on the page.

Tailwind Class Name CSS Property and Value Description
block display: block; Display an element as a block-level element; it occupies a line by itself.
inline-block display: inline-block; Display an element as an inline-block element; it can be displayed inline and have its own width and height.
inline display: inline; Display an element as an inline element; it is displayed side by side with other inline elements.
flex display: flex; Display an element as a flex container, allowing the use of Flexbox layout.
inline-flex display: inline-flex; Display an element as an inline flex container, allowing the use of Flexbox layout.
table display: table; Display an element as a table element.
inline-table display: inline-table; Display an element as an inline table element.
table-caption display: table-caption; Display an element as a table caption.
table-cell display: table-cell; Display an element as a table cell.
table-column display: table-column; Display an element as a table column.
table-column-group display: table-column-group; Display an element as a table column group.
table-footer-group display: table-footer-group; Display an element as a table footer group.
table-header-group display: table-header-group; Display an element as a table header group.
table-row-group display: table-row-group; Display an element as a table row group.
table-row display: table-row; Display an element as a table row.
flow-root display: flow-root; Display an element as a flow-root element, which is a BFC (block formatting context).
grid display: grid; Display an element as a grid container, allowing the use of CSS Grid layout.
inline-grid display: inline-grid; Display an element as an inline grid container, allowing the use of CSS Grid layout.
contents display: contents; Remove an element's box model so that its child elements are displayed directly in its place.
list-item display: list-item; Display an element as a list item.
hidden display: none; Hide an element so that it is not displayed on the page.

Example

   

    When controlling text flow, use the CSS property

   

display: inline

    to make the text inside the element wrap normally.

   


   


    while using the property

display: inline-block

    will wrap the element, preventing its inner text from expanding beyond its parent element.

   


   


    Finally, using the property

display: block

    will make the element occupy a line by itself and fill its parent element.

   

Try it »

Use the table, table-row, table-cell, table-caption, table-column, table-column-group, table-header-group, table-row-group, and table-footer-group utility classes to create elements that behave like their corresponding table elements.

Example

 

   

     

Song

     

Artist

     

Year

   

 

 

   

     

The Sliding Mr. Bones (Next Stop, Pottersville)

     

Malcolm Lockyer

     

1961

   

   

     

Witchy Woman

     

The Eagles

     

1972

   

   

     

Shining Star

     

Earth, Wind, and Fire

     

1975

   

 

Try it »

Floats

float-* controls the position of an element's float, commonly used for wrapping text.

Main Classes:

Tailwind Class Name CSS Property and Value Description
float-start float: inline-start; Float an element to the start of its containing block. In LTR (left-to-right) languages, this is usually the left; in RTL (right-to-left) languages, this is usually the right.
float-end float: inline-end; Float an element to the end of its containing block. In LTR (left-to-right) languages, this is usually the right; in RTL (right-to-left) languages, this is usually the left.
float-right float: right; Float an element to the right of its containing block.
float-left float: left; Float an element to the left of its containing block.
float-none float: none; Prevent an element from floating so that it does not leave the normal document flow.

Example

   

   

   

   

Try it »

Clear floats

Utility classes in Tailwind CSS for controlling how elements clear floats and their corresponding CSS properties:

Class Names CSS Property and Value Description
clear-start clear: inline-start; Clear floated elements on the start side of an element. In LTR (left-to-right) languages, this is usually the left; in RTL (right-to-left) languages, this is usually the right.
clear-end clear: inline-end; Clear floated elements on the end side of an element. In LTR (left-to-right) languages, this is usually the right; in RTL (right-to-left) languages, this is usually the left.
clear-left clear: left; Clear floated elements on the left side of an element.
clear-right clear: right; Clear floated elements on the right side of an element.
clear-both clear: both; Clear floated elements on both sides of an element.
clear-none clear: none; Do not clear any floated elements, allowing floated elements to follow the element.

Example

 

 

 

Test text content...

Try it »

isolate

The following are utility classes for controlling stacking contexts

Tailwind Class Name CSS Property and Value Description
isolate isolation: isolate; This element will create a new stacking context, which means its descendant elements will not overlap with external elements, even if they have a higher z-index values.
isolation-auto isolation: auto; The element's stacking context will be determined by the user agent, usually the same as when no isolation property is set, and descendant elements may overlap with external elements.

These utility classes allow developers to control an element's stacking context behavior, thereby affecting how the element and its descendants participate in stacking order.

The isolate class helps create an isolated environment in which an element's stacking order is not affected by external elements, while isolation-auto allows the browser to handle stacking contexts according to default rules.

Use the isolate and isolation-auto utility classes to control whether an element should explicitly create a new stacking context.

Example

 

‌

object-fit

The following are utility classes for controlling how the content of replaced elements is scaled and their corresponding CSS properties:

Class Names CSS Property and Value Description
object-contain object-fit: contain; Keep the element's aspect ratio and resize it to fit entirely within the container, possibly leaving blank space inside the container.
object-cover object-fit: cover; Keep the element's aspect ratio and resize it to completely cover the container, possibly cropping some parts of the element.
object-fill object-fit: fill; Ignore the element's aspect ratio and stretch or compress it to completely fill the container.
object-none object-fit: none; Keep the element's original size without scaling.
object-scale-down object-fit: scale-down; Keep the element's aspect ratio, similar to contain but choose the smaller size to fit the container, leaving no blank space.

Use object-cover to resize an element's content to cover its container:

Example

 

Try it »