HTML / HTML Tables
Creating Responsive Tables
In this tutorial, you'll learn how to create responsive tables that adapt to different screen sizes. This is essential for making your website mobile-friendly.
Section overview
5 resourcesCovers the creation and styling of tables in HTML.
Introduction
In this tutorial, we are going to learn how to create responsive tables that can adapt to different screen sizes, an essential feature for a mobile-friendly website.
You will learn:
- How to create tables using HTML and CSS.
- How to make these tables responsive using media queries.
- Tips for optimizing your table's design for better usability.
Prerequisites:
- Basic knowledge of HTML
- Basic knowledge of CSS
Step-by-Step Guide
Understanding Tables in HTML
Tables in HTML can be created using the <table> tag. To create rows and data cells in the table, we use <tr> and <td> tags respectively.
Making Tables Responsive with CSS
We can make our tables responsive by using CSS media queries, which allow us to apply different styles depending on the screen size.
Code Examples
Basic HTML Table
Here is a basic HTML table:
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
This would create a table with two rows and two cells in each row.
Making the Table Responsive
To make the table responsive, we will use the CSS overflow property, which specifies what should happen if content overflows an element's box, and media queries.
.table-responsive {
width: 100%;
overflow-x: auto;
}
@media screen and (max-width: 600px) {
.table-responsive table {
width: 100%;
}
.table-responsive td {
word-wrap: break-word;
}
}
In the above CSS, we first make the table width 100% and set overflow-x property to auto which will add a horizontal scroll bar when the content is too wide. Then, we add a media query for screens with a maximum width of 600px. For these screen sizes, we ensure the table width is 100% and words in the data cells break and wrap to the next line if they are too long.
Summary
In this tutorial, we learned how to create tables in HTML and make them responsive using CSS. The key points covered include:
- Creating tables in HTML using
<table>,<tr>, and<td>tags. - Making tables responsive using CSS
overflowproperty and media queries.
Practice Exercises
- Create a table with 5 rows and 3 cells in each row.
- Make the table responsive for screens with a maximum width of 800px.
Solutions
- Creating a table with 5 rows and 3 cells in each row.
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
<td>Row 1, Cell 3</td>
</tr>
<!-- Add more rows here -->
</table>
- Making the table responsive for screens with a maximum width of 800px.
.table-responsive {
width: 100%;
overflow-x: auto;
}
@media screen and (max-width: 800px) {
.table-responsive table {
width: 100%;
}
.table-responsive td {
word-wrap: break-word;
}
}
For further practice, you can experiment with different table styles and layouts, and try making them responsive for different screen sizes.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article