Design Table with CSS

Table css are essential components in web development, used to display data in rows and columns. With CSS, you can style tables to make them visually appealing and enhance their functionality.

What is Table?

Tables are powerful tools for organizing and displaying data on web pages. CSS provides immense flexibility in styling tables to suit various use cases, from simple lists to advanced layouts with merged cells, responsive designs, and fixed headers. By combining CSS properties with best practices, you can create tables that are both functional and aesthetically pleasing.


1. <table>

The <table> tag defines the outer container of a table. All table-related elements must be nested inside this tag.

Attributes:

align: Aligns the table on the page (deprecated; use CSS).

border: Sets the width of the table’s border (deprecated; use CSS instead).

Preview

#NameAgeOccupationCountry
1John Doe28EngineerUSA
2Jane Smith34DesignerCanada
3Michael Brown41ManagerUK
4Emily Davis25DeveloperAustralia
5Chris Wilson37TeacherIndia
6Anna Johnson29PhotographerGermany
7David Martinez33WriterSpain
8Sophia Lee22StudentSouth Korea
9Daniel Evans40ChefItaly
10Lisa Brown27ArtistFrance

Show Code


2. <tr> (Table Row)Row Table

The <tr> tag defines a single row in the table. It contains table header (<th>) or table data (<td>) cells.


3. <th> (Table Header)

The <th> tag is used to define header cells in a table. Content inside <th> is typically bold and centered by default.

Attributes:

  • scope: Specifies whether the header applies to a row (row) or a column (col)

4. <td> (Table Data)

The <td> tag represents standard cells in the table. It is used to add data content.


5. <thead> (Table Head)

The <thead> tag groups the header rows in a table. It is typically used in conjunction with <th> and separates the table header from the table body.


6. <tbody> (Table Body)

The <tbody> tag groups the main content rows of a table. It allows for better organization, especially when used with <thead> and <tfoot>.


7. <tfoot> (Table Footer)

The <tfoot> tag groups footer rows in a table. It is typically used for summary or total rows.


Preview

#NameAgeOccupationCountry
First NameLast Name
1JohnDoe28EngineerUSA
2JaneSmith34DesignerCanada
3EmilyJohnson29Australia
4Chris Wilson37TeacherIndia
5SophiaLee22StudentSouth Korea
Total Entries: 5

Show Code

Preview

First NameLast NameDesignationEmailPhoneActions
JohnDoeManagerjohn.doe@example.com1234567890
JaneSmithDeveloperjane.smith@example.com2345678901
Showing 1-5 of 10 entries

Show Code


8. <caption>

The <caption> tag provides a title or brief description of the table. It is displayed above the table by default.


9. <colgroup>

The <colgroup> tag is used to group columns within a table for applying specific styles or attributes to those columns.


10. <col>

The <col> tag is used within <colgroup> to define the properties of individual columns.


11. <scope> Attribute

Though not a standalone tag, the scope attribute in the <th> tag is essential for accessibility. It specifies whether the header applies to a row, column, or group of rows/columns.


Best Practices for Using Table Tags

  1. Use Semantic Elements: Always use <thead>, <tbody>, and <tfoot> for better readability and accessibility.
  2. Add Captions: Provide descriptive captions using the <caption> tag.
  3. Enhance Accessibility: Use scope attributes in <th> tags for assistive technologies.
  4. Avoid Deprecated Attributes: Use CSS instead of deprecated table attributes like align, border, or bgcolor.

CSS Tips for Tables

Use border-collapse for compact borders

Add hover effects for better interactivity:

Style headers distinctly.

Check out – CSS Position.