π§© Build a Responsive Table with Column Headers and Row Data in HTML
Tables are essential for organizing data in web pages. But traditional tables can break on small screens like mobile devices. In this tutorial, you'll learn how to build a responsive table in HTML that adjusts beautifully across all screen sizes.
Let’s dive in with clear steps and code examples — perfect for beginners!
π What You’ll Learn:
-
How to create a table using HTML
-
Add column headers and row data
-
Make the table responsive using CSS
-
Google Advertisement
Test it on different screen sizes
π οΈ Step-by-Step: Creating a Basic HTML Table
β Step 1: Basic HTML Table Structure
Start by creating a simple table using HTML:
π§ Explanation:
-
<table>
is the main tag for creating a table. -
<thead>
contains the column headers. -
<tbody>
contains the row data. -
<tr>
is a table row. -
Google Advertisement
<th>
defines a header cell. -
<td>
defines a regular data cell.
π― Step 2: Style the Table with CSS
Now let's add some basic CSS to make the table look nice and readable.
π§ Explanation:
-
width: 100%
makes the table expand to the full container width. -
border-collapse: collapse
removes spacing between table borders. -
padding
andtext-align
improve readability. -
Google Advertisement
thead
background helps distinguish headers.
π± Step 3: Make the Table Responsive
Let’s now make the table mobile-friendly using CSS.
π§ Explanation:
-
@media
targets small screen devices (max 600px). -
display: block
transforms table rows and cells into block elements. -
thead
is hidden since we’ll show headers in eachtd
using::before
. -
data-label
is used to show the column name in each cell for small screens.
π Step 4: Add data-label
to Each Table Cell
Now modify your HTML table like this:
π§ Explanation:
-
The
data-label="ColumnName"
is custom data used to show the heading text next to the value on small screens.
β Final Complete Code (HTML + CSS)
π Conclusion
Now you’ve learned how to create a responsive table with headers and rows using only HTML and CSS. This approach works great on both desktop and mobile, and is easy to implement on any website.