26 Mar 2023

Building a responsive feature comparison table with HTML and CSS

Comparison tables are a great way to showcase and compare different features of products or services. These tables are a popular choice for websites that compare products or services, such as software review sites, e-commerce websites, or even comparison sites.

In this blog, we'll walk you through the process of building a responsive feature comparison table using HTML and CSS. By the end of this tutorial, you'll have a fully functional and responsive comparison table that looks great on all devices.

Step 1: Planning the Table

Before we start coding, we need to plan out the structure of our comparison table. In this tutorial, we'll be building a simple comparison table that compares the features of two products. The table will have three columns: Features, Product A, and Product B. The first row of the table will contain the product names, and the subsequent rows will contain the feature names and their corresponding values.

Here's a sketch of what our table will look like:

  Product A Product B
Feature 1 Value 1A Value 1B
Feature 2 Value 2A Value 2B
Feature 3 Value 3A Value 3B
Feature 4 Value 4A Value 4B
Feature 5 Value 5A Value 5B

Step 2: Creating the HTML Structure

Now that we have planned out the structure of our table, we can start coding the HTML. We'll start by creating a container div that will hold our table.

<div class="comparison-table">
  <!-- Table goes here -->
</div>

Inside the container div, we'll create a table element with a thead and tbody element. The thead element will contain the first row of the table, which will include the product names. The tbody element will contain the subsequent rows, which will include the feature names and their corresponding values.

<table>
  <thead>
    <tr>
      <th>Features</th>
      <th>Product A</th>
      <th>Product B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Feature 1</td>
      <td>Value 1A</td>
      <td>Value 1B</td>
    </tr>
    <tr>
      <td>Feature 2</td>
      <td>Value 2A</td>
      <td>Value 2B</td>
    </tr>
    <tr>
      <td>Feature 3</td>
      <td>Value 3A</td>
      <td>Value 3B</td>
    </tr>
    <tr>
      <td>Feature 4</td>
      <td>Value 4A</td>
      <td>Value 4B</td>
    </tr>
    <tr>
      <td>Feature 5</td>
      <td>Value 5A</td>
      <td>Value 5B</td>
    </tr>
  </tbody>
</table>

Step 3: Styling the Table

Now that we have created the HTML structure of our table, we can start styling it using CSS. We'll start by adding some basic styling to our container div and table element.

.comparison-table {
  max-width: 100%;
  overflow-x: auto;
}

ttable {
  border-collapse: collapse;
  width: 100%;
  text-align: center;
}

The max-width and overflow-x properties set on the container div ensure that the table is responsive and horizontally scrollable when viewed on smaller screens.

The border-collapse property set on the table element removes the default spacing between table cells. The width property set to 100% ensures that the table takes up the full width of its container. Finally, the text-align property set to center aligns the text in the table cells.

Next, we'll add some basic styling to the thead element, which will contain the first row of the table.

thead {
  background-color: #f2f2f2;
}

th {
  padding: 10px;
  font-weight: bold;
  text-align: center;
}

The background-color property set on the thead element gives the row a light gray background color. The padding property set on the th element adds some space between the text and the border of the cell. The font-weight property set to bold makes the text in the table headers stand out. Finally, the text-align property set to center aligns the text in the table headers.

Now, we'll style the tbody element, which contains the rest of the rows in the table.

tbody tr:nth-child(even) {
  background-color: #f2f2f2;
}

td {
  padding: 10px;
}

The nth-child selector set on the tbody tr element gives every other row a light gray background color. The padding property set on the td element adds some space between the text and the border of the cell.

Finally, we'll add some responsive styling to our table using media queries. These media queries will adjust the table layout as the screen size changes.

@media (max-width: 768px) {
  table {
    display: block;
    overflow-x: auto;
  }
  thead {
    display: none;
  }
  tbody tr {
    display: block;
    margin-bottom: 10px;
  }
  tbody td:first-child {
    font-weight: bold;
  }
  tbody td:before {
    content: attr(data-label);
    display: inline-block;
    width: 100px;
    font-weight: bold;
  }
}

In the media query above, we set the table display to block and add horizontal scrolling to ensure that the table fits on smaller screens. We also hide the thead element since it is no longer needed on smaller screens.

For the tbody element, we set the display to block and add a margin-bottom to create space between the rows. We also add some styling to the first td element in each row to make it stand out.

Finally, we use the ::before pseudo-element to add labels to each td element. These labels are based on the data-label attribute set on each td element. The content property is used to insert the label text, and the width and font-weight properties are used to style the labels.

Step 4: Adding Content

Now that we have our table styled and responsive, we can add our own content to the table. Simply replace the sample content in the HTML with your own content, and the table will adjust accordingly.

<table>
  <thead>
    <tr>
      <th>Features</th>
      <th>Product A</th>
      <th>Product B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Price</td>
      <td>$49</td <td>$69</td>
    </tr>
    <tr>
      <td>Storage</td>
      <td>250 GB</td>
      <td>500 GB</td>
    </tr>
    <tr>
      <td>RAM</td>
      <td>8 GB</td>
      <td>16 GB</td>
    </tr>
    <tr>
      <td>Processor</td>
      <td>Intel Core i5</td>
      <td>Intel Core i7</td>
    </tr>
    <tr>
      <td>Screen Size</td>
      <td>13 inches</td>
      <td>15 inches</td>
    </tr>
  </tbody>
</table>

In the example above, we've added five rows to our table, each containing information about two different products. The table adjusts to the content, and the responsive layout ensures that the table remains legible on smaller screens.

Conclusion

In this tutorial, we've shown you how to build a responsive feature comparison table using HTML and CSS. We started by setting up the basic table structure and then used CSS to style the table and make it responsive. Finally, we added content to the table to see how it adjusts to different amounts of content.

With this knowledge, you can create your own feature comparison tables for your website or product pages. Remember to keep the design simple and easy to read, and use responsive design principles to ensure that your tables look great on any device.

By creating a well-designed and informative feature comparison table, you can help your users make informed decisions and choose the product that best suits their needs. This can also help increase conversions and sales on your website.

Remember to keep the table updated as new products are released or as features change. A stale or outdated comparison table can lead to confusion and frustration among your users.

In conclusion, building a responsive feature comparison table with HTML and CSS is a valuable skill for web developers and designers. With some basic HTML and CSS knowledge, you can create a visually appealing and functional table that helps your users make informed decisions.