Tailwind CSS Tables
Tailwind CSS provides a convenient set of utility classes for creating and customizing tables.
Through these utility classes, you can easily control table styling, including borders, spacing, alignment, font style, etc.
Next, we will introduce in detail how to use Tailwind CSS to create and customize tables.
Basic Table Styles
Tailwind CSS provides some basic classes for controlling the appearance and layout of tables. Common utility classes include table-auto and table-fixed, which are used to set the table layout.
table-auto: Default layout, where column widths are automatically determined by content.table-fixed: Fixed table column widths. All columns are equally divided based on the total table width, suitable for tables with uniform column widths.
Using table-auto allows the table to automatically adjust column widths to fit cell content.
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 »
Using table-fixed allows the table to ignore content and use fixed widths for columns. The width of the first row sets the column widths for the entire table.
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 »
Borders and Spacing
Tailwind provides many utility classes to control table borders, inner and outer margins, and other layout details.
You can use classes such as border, border-collapse, border-spacing to control table border styles.
border: Add a border to the table.border-collapse: Control whether table cell borders are collapsed.border-spacing: Set the spacing between cells.
Example
|
Header 1 |
Header 2 |
Header 3 |
|---|---|---|
|
Row 1, Col 1 |
Row 1, Col 2 |
Row 1, Col 3 |
|
Row 2, Col 1 |
Row 2, Col 2 |
Row 2, Col 3 |
Try it »
Inner and Outer Margins
You can use the px-, py- class to set the padding of table cells, and the m-* and p-* classes to set the margins of the table.
Example
|
Header 1 |
Header 2 |
Header 3 |
|---|---|---|
|
Row 1, Col 1 |
Row 1, Col 2 |
Row 1, Col 3 |
|
Header 1 |
Header 2 |
Header 3 |
|---|
Try it »
Alignment and Text Styles
Tailwind provides some classes to control the alignment and font style of table cell content, such as:
text-left,text-center,text-right: Control text alignment.font-semibold,font-bold: Control font weight.uppercase,lowercase,capitalize: Control text capitalization.
Example
|
Header 1 |
Header 2 |
Header 3 |
|---|---|---|
|
Row 1, Col 1 |
Row 1, Col 2 |
Row 1, Col 3 |
Try it »
Merging Cells
Tailwind CSS supports setting merged cells through HTML attributes such as colspan and rowspan.
Example
|
Header 1 & 2 |
Header 3 | |
|---|---|---|
| Row 1, Col 1 & 2 | Row 1, Col 3 | |
Try it »
Responsive table
You can also use Tailwind CSS's responsive design classes to control table layout at different screen sizes.
For example, use the sm:, md:, lg: prefixes to set the display at different screen sizes.
Example
|
Header 1 |
Header 3 |
|---|---|
|
Row 1, Col 1 |
Row 1, Col 3 |
Try it »
Table Caption
Use caption-top to position the caption element at the top of the table.
Example
|
Wrestler |
Signature Move(s) |
|---|---|
|
"Stone Cold" Steve Austin |
Stone Cold Stunner, Lou Thesz Press |
|
Bret "The Hitman" Hart |
The Sharpshooter |
|
Razor Ramon |
Razor's Edge, Fallaway Slam |
Try it »
Use caption-bottom to position the caption element at the bottom of the table.
Example
|
Wrestler |
Signature Move(s) |
|---|---|
|
"Stone Cold" Steve Austin |
Stone Cold Stunner, Lou Thesz Press |
|
Bret "The Hitman" Hart |
The Sharpshooter |
|
Razor Ramon |
Razor's Edge, Fallaway Slam |
Try it »