Google Advertisement

How to Create a Fully Responsive HTML Table with Headers and Rows

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn step-by-step how to build a responsive HTML table with column headers and row data using clean code and modern design practices. Perfect for beginners!

Published on 03 May 2025
By Vandu

🧩 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.

πŸ”₯ Read with Full Features on Our Website

Let’s dive in with clear steps and code examples — perfect for beginners!


πŸ“Œ What You’ll Learn:


πŸ› οΈ Step-by-Step: Creating a Basic HTML Table

βœ… Step 1: Basic HTML Table Structure

Start by creating a simple table using HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Responsive Table</title>
</head>
<body>

  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alice</td>
        <td>24</td>
        <td>New York</td>
      </tr>
      <tr>
        <td>Bob</td>
        <td>30</td>
        <td>Los Angeles</td>
      </tr>
    </tbody>
  </table>

</body>
</html>

🧠 Explanation:


🎯 Step 2: Style the Table with CSS

Now let's add some basic CSS to make the table look nice and readable.

<style>
  table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
  }

  th, td {
    border: 1px solid #ccc;
    padding: 12px;
    text-align: left;
  }

  thead {
    background-color: #f4f4f4;
  }
</style>

🧠 Explanation:


πŸ“± Step 3: Make the Table Responsive

Let’s now make the table mobile-friendly using CSS.

<style>
  @media (max-width: 600px) {
    table, thead, tbody, th, td, tr {
      display: block;
    }

    thead {
      display: none; /* Hide headers */
    }

    tr {
      margin-bottom: 15px;
    }

    td {
      position: relative;
      padding-left: 50%;
    }

    td::before {
      content: attr(data-label);
      position: absolute;
      left: 10px;
      top: 12px;
      font-weight: bold;
    }
  }
</style>

🧠 Explanation:


πŸ“‹ Step 4: Add data-label to Each Table Cell

Now modify your HTML table like this:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>City</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Name">Alice</td>
      <td data-label="Age">24</td>
      <td data-label="City">New York</td>
    </tr>
    <tr>
      <td data-label="Name">Bob</td>
      <td data-label="Age">30</td>
      <td data-label="City">Los Angeles</td>
    </tr>
  </tbody>
</table>

🧠 Explanation:


βœ… Final Complete Code (HTML + CSS)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Table Example</title>
  <style>
    table {
      width: 100%;
      border-collapse: collapse;
      margin: 20px 0;
    }

    th, td {
      border: 1px solid #ccc;
      padding: 12px;
      text-align: left;
    }

    thead {
      background-color: #f4f4f4;
    }

    @media (max-width: 600px) {
      table, thead, tbody, th, td, tr {
        display: block;
      }

      thead {
        display: none;
      }

      tr {
        margin-bottom: 15px;
      }

      td {
        position: relative;
        padding-left: 50%;
      }

      td::before {
        content: attr(data-label);
        position: absolute;
        left: 10px;
        top: 12px;
        font-weight: bold;
      }
    }
  </style>
</head>
<body>

  <h2>Responsive Table with Headers and Rows</h2>

  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td data-label="Name">Alice</td>
        <td data-label="Age">24</td>
        <td data-label="City">New York</td>
      </tr>
      <tr>
        <td data-label="Name">Bob</td>
        <td data-label="Age">30</td>
        <td data-label="City">Los Angeles</td>
      </tr>
    </tbody>
  </table>

</body>
</html>

πŸŽ‰ 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.

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website