CSS Tables


You can enhance the appearance of an HTML table using CSS styling.

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Berglunds snabbköp Christina Berglund Sweden
Centro comercial Moctezuma Francisco Chang Mexico
Ernst Handel Roland Mendel Austria
Island Trading Helen Bennett UK
Königlich Essen Philip Cramer Germany
Laughing Bacchus Winecellars Yoshi Tannamuri Canada
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

Table Borders

To define the borders of a table using CSS, employ the 'border' property within a border property.

The following example sets a solid border for <table>, <th>, and <td> elements:

Firstname Lastname
Peter Griffin
Lois Griffin

Full-Width Table

The table you see above might appear small sometimes. If you want a table to cover the whole screen width, you can do so by adding width: 100% to the <table> element:

Firstname Lastname
Peter Griffin
Lois Griffin

Collapse Table Borders

The border-collapse property decides whether the borders of a table should be combined into one single border or not.

Firstname Lastname
Peter Griffin
Lois Griffin

If you just want a border around the table, you should only set the border property for the <table> element:

Firstname Lastname
Peter Griffin
Lois Griffin

Table Width and Height

The size of a table is determined by the width and height properties, which are specified using HTML tags and can also have associated classes.

The following example makes the table as wide as the available space (100% width) and sets the height of the header cells ( elements) to 70 pixels.

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

To make a table that covers only half of the page, you can use the HTML code with the following style attribute: width: 50%:

By default, the text inside <th> tags is placed in the middle, while the text inside <td> tags is positioned to the left.

To make the content inside   <td> elements appear in the center, apply the style text-align center.


Horizontal Alignment

The text-align attribute positions the content horizontally within <th> or <td>. elements, determining whether it should be aligned to the left, right, or center.

Typically, the text inside <th> elements is centered, while the content within <td> elements is aligned to the left.

To make the content inside <td> elements centered, apply the style text-align: center.

To make the content align to the left, ensure that the alignment of <th> elements is set to left-aligned using the property text-align: left


Vertical Alignment

The vertical-align feature determines how content inside <th> or <td> is aligned vertically, such as at the top, bottom, or middle.

The content in a table is usually centered vertically by default, whether it's in a <th> or <td> element.


Responsive Table

If the screen is too small to show all the content, a responsive table will have a horizontal scroll bar.