BloG

Getting Began with HTML Tables — SitePoint

Mar 28, 2023

HTML tables are intended for displaying tabular data on an internet page. They’re great for displaying information in an organized way, and will be styled with CSS to match the appear and feel of our website. On this tutorial, we’ll cover the fundamentals of making HTML tables and adding styles to make them responsive and mobile-friendly.

Creating an HTML Table

To create an HTML table, we’d like to make use of the

tag. Contained in the

tag, we’d like to create a number of

tags, which define each row of the table. Inside each

tag, we will create a number of

tag. So as to add latest cells to the table, we will create a latest

tag. Here is an example of a table with 4 rows and three columns:

<table>
<tr>
<td>Cell 1td>
<td>Cell 2td>
<td>Cell 3td>
tr>
<tr>
<td>Cell 4td>
<td>Cell 5td>
<td>Cell 6td>
tr>
<tr>
<td>Cell 7td>
<td>Cell 8td>
<td>Cell 9td>
tr>
<tr>
<td>Cell 10td>
<td>Cell 11td>
<td>Cell 12td>
tr>
table>

This can create a table with 4 rows and three columns.

Styling HTML Tables

HTML tables will be styled using CSS to vary their appearance. A few of the most typical CSS properties used to style tables include border, padding, and background-color. Here’s an example of the best way to style a table with a border and a background color:

table {
border: 1px solid black;
background-color: #f2f2f2;
}
td {
padding: 8px;
}

This can create a table with a black border and a light-weight gray background color, with each cell having a padding of eight pixels.

See the Pen
A basic HTML table by SitePoint (@SitePoint)
on CodePen.

Making Tables Responsive and Mobile-friendly

One in every of the challenges of using HTML tables is making them responsive and mobile-friendly. One strategy to accomplish that is to make use of CSS to regulate the layout of the table based on the dimensions of the screen. One approach is to make use of the display property to vary the layout of the table from a set layout to a responsive layout. This will be done using media queries to focus on specific screen sizes. Here’s an example of the best way to make a table responsive:

table {
border: 1px solid black;
background-color: #f2f2f2;
}

td {
padding: 8px;
}

@media only screen and (max-width: 600px) {
table {
display: block;
}
td {
display: block;
}
}

This can make the table layout change from a set layout to a responsive layout when the screen width is lower than 600 pixels.

See the Pen
A responsive HTML table with basic styling by SitePoint (@SitePoint)
on CodePen.

Adding Captions and Summaries

One other vital aspect of using HTML tables is making them accessible to non-visual users. One strategy to do that is so as to add captions and summaries to the table. The

tags, which define the cells of the table. Here’s an example of a basic HTML table:

<table>
<tr>
<td>Cell 1td>
<td>Cell 2td>
<td>Cell 3td>
tr>
<tr>
<td>Cell 4td>
<td>Cell 5td>
<td>Cell 6td>
tr>
table>

This can create a table with two rows and three columns, with each cell displaying its contents.

See the Pen
A basic HTML table by SitePoint (@SitePoint)
on CodePen.

Adding Rows and Columns

So as to add latest rows to the table, we simply have to create a latest

tag inside an existing

tag will be used so as to add a caption to the table, which describes the content of the table. Here’s an example of the best way to add a caption to a table:

<table>
<caption>Sales by Monthcaption>
<tr>
<th>Monthth>
<th>Salesth>
tr>
<tr>
<td>Januarytd>
<td>$1000td>
tr>
<tr>
<td>Februarytd>
<td>$1500td>
tr>
table>

This can add a caption to the table, stating that it displays sales by month. The

tag will be used to supply a summary of the table for screen readers and other assistive technologies. Here’s an example of the best way to add a summary to a table:

<table summary=Sales by Month>
<tr>
<th>Monthth>
<th>Salesth>
tr>
<tr>
<td>Januarytd>
<td>$1000td>
tr>
<tr>
<td>Februarytd>
<td>$1500td>
tr>
table>

This can provide a summary of the table for screen readers and other assistive technologies, stating that it displays sales by month.

Is Using Tables Bad?

No! Tables are a crucial a part of HTML. They’re are essential for displaying tabular data in a semantic and accessible way. Within the early days of the Web, before the appearance of CSS, tables offered a method to put out web page designs, but this wasn’t their intended purose. Thankfully, those days are way behind us (well, mostly, but for some email clients!), and we will now deal with the true — and intensely vital — role of HTML tables for displaying data.

Conclusion

HTML tables are a robust tool for displaying tabular data on an internet page. With CSS, tables will be styled to match the appear and feel of our website, and made responsive and mobile-friendly for users on different devices. Adding captions and summaries to tables can assist improve accessibility for users with disabilities. With these techniques, we will create effective tables which can be each visually appealing and functional.