The viewport meta tag tells the browser how to scale and render the content of your web page according to the device's screen size. By adding this tag to the <head> section of your HTML document, you can ensure that your tables will not overflow or shrink too much on smaller or larger screens. For example, you can use the following tag to set the initial scale to 1 and the width to the device's width:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Media queries are a powerful CSS feature that allows you to apply different styles to your elements based on certain conditions, such as the screen size, orientation, or resolution. You can use media queries to adjust the layout, font size, padding, or visibility of your tables depending on the screen size. For example, you can use the following media query to reduce the font size and padding of your table cells on screens smaller than 600 pixels:
@media screen and (max-width: 600px) {
table, th, td {
font-size: 12px;
padding: 5px;
}
}
-
Many mobile devices are wider than 600px these days. Using "@media (orientation: portrait)" has been more reliable for me, but I am sure there is a better solution still.
Another way to make your tables more responsive and adaptable is to use relative units for your width, height, and margin properties, instead of absolute units like pixels or points. Relative units, such as percentages, ems, or rems, are based on the parent element's size, the font size, or the root element's size, respectively. This way, your tables can resize and adjust according to the available space and the user's preferences. For example, you can use the following CSS rule to set the width of your table to 80% of its parent element's width:
table {
width: 80%;
}
-
Utilize relative units like percentages, ems, and rems for responsive HTML tables. Set table width as a percentage for scalability. Use ems for padding and text size based on parent font-size; rems for consistent sizing. For layout flexibility, try modern units like fr in CSS Grids. Combine with responsive font sizes (e.g., 3vw) for readability across devices. This ensures tables adapt well to various screens and content sizes. It's essential to test on different devices to ensure optimal performance.
Sometimes, you may have tables that contain too much data or content to fit in the available space, especially on smaller screens. In this case, you can use the overflow property to control how the extra content is handled. The overflow property can take values such as hidden, scroll, auto, or clip, which will hide, add a scroll bar, adjust automatically, or clip the content that overflows the element's box. For example, you can use the following CSS rule to add a horizontal scroll bar to your table if it overflows its parent element's width:
table {
overflow-x: scroll;
}
Another technique to make your tables more responsive and adaptable is to use the display property to change how your table elements are displayed on different screen sizes. The display property can take values such as block, inline-block, flex, grid, or none, which will affect the layout, alignment, and visibility of your table elements. For example, you can use the following CSS rule to hide your table headers on screens smaller than 600 pixels and display them as block elements on larger screens:
@media screen and (max-width: 600px) {
th {
display: none;
}
}
@media screen and (min-width: 600px) {
th {
display: block;
}
}
-
Leverage the display property for responsive tables. Besides hiding elements, explore flexbox for rows that adapt and wrap content on smaller screens. Consider using CSS Grid for complex, flexible layouts. Also, can't forget content adaptation; show summarized data on small screens and detailed data on larger ones. The display property is a gateway to powerful layout strategies for top-notch tables on any device.
Finally, if you want to save some time and effort, you can use responsive frameworks that provide ready-made solutions for creating responsive and adaptable tables. Responsive frameworks are libraries of CSS and JavaScript code that offer predefined styles, grids, components, and utilities for building responsive web pages. Some examples of popular responsive frameworks are Bootstrap, Foundation, and Materialize. These frameworks usually have classes and attributes that you can apply to your table elements to make them responsive and adaptable. For example, you can use the following HTML code to create a responsive table with Bootstrap:
<div class="table-responsive">
<table class="table">
<!-- Your table content here -->
</table>
</div>
-
I would also mention Tailwind CSS, as it's getting more popular and big players are using it. In this specific case, Tailwind CSS provides pseudo-classes directly used in the HTML acting as media queries. They can even be chained with others, e.g. for dark mode.
-
HTML tables are a real challenge to make responsive as the number of columns increase. A table with 3-4 columns and cell data that is relatively small may lend itself well to responsive design with media queries - but if you’re trying to make a 10 column table fit onto a screen with a width of 450px (or even twice that), you’ll need to consider allowing side-scrolling (which I know for tables can be a nuisance), or a framework/library that handles the responsiveness for you. PrimeNg (Angular) has a table component that is responsive out-of-the-box, changing your table into a scrollable set of cards that contain 1 row of data per card. It’s an ingenious way to make a table responsive and adaptable to smaller screens.
-
The responsive grid will add up to 12 horizontally for each line during desktop view. However, these same elements that added up to 12 from left to right now stack. Those on the left stack on top automatically when the screen is smaller than 768 pixels in width. Think about whatever is on the left will be on top within mobile view.