{"id":71911,"date":"2022-11-16T21:26:00","date_gmt":"2022-11-16T10:26:00","guid":{"rendered":"https:\/\/elements.blog-cms.envato.net\/?p=43201"},"modified":"2025-11-11T17:43:09","modified_gmt":"2025-11-11T06:43:09","slug":"how-to-build-a-stylish-responsive-html-table","status":"publish","type":"post","link":"https:\/\/elements.envato.com\/learn\/how-to-build-a-stylish-responsive-html-table","title":{"rendered":"How to Build a Stylish, Responsive HTML Table"},"content":{"rendered":"\n<p>In this tutorial, we\u2019ll learn how to build a stylish table and modify its layout to adapt to various screen sizes.<\/p>\n\n\n\n<p>The data presented in our table will come from the <a href=\"https:\/\/www.imdb.com\/name\/nm0000229\/\" target=\"_blank\" rel=\"noopener\">IMDb platform<\/a> and list a few of Steven Spielberg\u2019s movies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Responsive Tables in HTML<\/h2>\n\n\n\n<p>Tabular data should be structured in one way: with the HTML table elements. This is the semantically correct approach, but it can give us some serious stylistic challenges. Tables are notoriously inflexible where responsive layouts are concerned.<\/p>\n\n\n\n<p>There are several solutions to the issue: scrollable containers wrapping the table, collapsible cells, using different (less semantic) markup. We\u2019re going to use Flexbox to display our table cells in a grid layout on smaller screens.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What We\u2019re Building<\/h2>\n\n\n\n<p>Here\u2019s our table (check out the <a rel=\"noopener\" href=\"https:\/\/codepen.io\/tutsplus\/full\/MWXYXwP\" target=\"_self\">full page version<\/a> to play with the responsive behavior):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codepen.io\/tutsplus\/pen\/MWXYXwP\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"722\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-1-1024x722.jpg\" alt=\"codepen table\" class=\"wp-image-86581\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-1-1024x722.jpg 1024w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-1-300x212.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-1-768x542.jpg 768w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-1.jpg 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>You\u2019ll notice that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>On mobile screens (&lt;700px), all table cells are stacked.<\/li>\n\n\n\n<li>On medium screens (\u2265700px), there\u2019s a two-column layout.<\/li>\n\n\n\n<li>On large screens (\u22651000px), there\u2019s a typical table layout.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Begin With the HTML Markup<\/h2>\n\n\n\n<p>The markup will be straightforward: a table with six rows inside a container.<\/p>\n\n\n\n<p>Here\u2019s how it\u2019ll look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"container\"&gt;  \n  &lt;table&gt;\n    &lt;thead class=\"visible@l\"&gt;\n      &lt;tr&gt;\n        &lt;th&gt;Title&lt;\/th&gt;\n        &lt;th&gt;Year&lt;\/th&gt;\n        &lt;th&gt;Stars&lt;\/th&gt;\n        &lt;th&gt;Plot&lt;\/th&gt;\n      &lt;\/tr&gt;\n    &lt;\/thead&gt;\n    &lt;tbody&gt;\n      &lt;tr&gt;\n        &lt;td&gt;\n          &lt;strong class=\"hidden@l\"&gt;Title:&lt;\/strong&gt; \n          ...\n        &lt;\/td&gt;\n        &lt;td&gt;\n          &lt;strong class=\"hidden@l\"&gt;Year:&lt;\/strong&gt; ...\n          &lt;img width=\"140\" height=\"209\" src=\"...\" alt=\"...\"&gt;\n        &lt;\/td&gt;\n        &lt;td&gt;\n          &lt;strong class=\"hidden@l\"&gt;Stars:&lt;\/strong&gt; \n          ...\n        &lt;\/td&gt;\n        &lt;td&gt;\n          &lt;strong class=\"hidden@l\"&gt;Plot:&lt;\/strong&gt; \n          ...\n        &lt;\/td&gt;\n      &lt;\/tr&gt;\n      \n      &lt;!-- more table rows here --&gt;\n\n    &lt;\/tbody&gt;\n  &lt;\/table&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>Notice the <code>visible@l<\/code> and <code>hidden@l<\/code> classes. These will help us toggle specific elements depending on the viewport size.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Add Styles<\/h2>\n\n\n\n<p>Mostly, we\u2019ll follow a mobile-first approach for our styles. As mentioned above, we\u2019ll need two helper classes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@media (max-width: 999px) {\n  .visible\\@l {\n    display: none;\n  }\n}\n\n@media (min-width: 1000px) {\n  .hidden\\@l {\n    display: none;\n  }\n}<\/code><\/pre>\n\n\n\n<p>At any point, our table will have zebra-striped rows; we\u2019ll use the <code>:nth-child()<\/code> CSS pseudo-class to generate them.<\/p>\n\n\n\n<p>On small and medium screens, the table header will be hidden. At this point, we\u2019ll show a number indicating the movie number. We\u2019ll replace the table headings with some <code>strong<\/code> elements that will appear inside each cell.<\/p>\n\n\n\n<p>So, on small screens, it will look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"850\" height=\"660\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/small_layout.jpg\" alt=\"small layout\" class=\"wp-image-86421\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/small_layout.jpg 850w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/small_layout-300x233.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/small_layout-768x596.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>On medium screens, the table will be a two-column grid:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"850\" height=\"705\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/md.jpg\" alt=\"medium layout\" class=\"wp-image-86422\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/md.jpg 850w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/md-300x249.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/md-768x637.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p><strong style=\"background: #fffeb0;color: #591a1a\"><\/strong><\/p>\n\n\n\n<p>On large screens, all table cells will have a width of 25%. Plus, the table headings will become visible.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"850\" height=\"359\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout.jpg\" alt=\"desktop layout\" class=\"wp-image-86423\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout.jpg 850w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout-300x127.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout-768x324.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>Each time we hover over a row, an associated image will appear. Also, a small black bullet will sit at the center-right position to indicate the active row.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"850\" height=\"206\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout_hovered_state.jpg\" alt=\"desktop hover layout\" class=\"wp-image-86424\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout_hovered_state.jpg 850w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout_hovered_state-300x73.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/desktop_layout_hovered_state-768x186.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>With all the above in mind, here\u2019s a part of those styles. Notice how we begin with our table rows using <code>display: flex; flex-wrap: wrap;<\/code>, with <code>display: table-row;<\/code> kicking in once we hit the 1000px breakpoint:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*CUSTOM VARIABLES HERE*\/\n\ntable {\n  margin: 50px 0 20px;\n  text-align: left;\n  border-collapse: collapse;\n  border: 1px solid var(--table-border);\n}\n\ntable th {\n  color: var(--white);\n  background: var(--darkblue);\n  padding: 20px;\n}\n\ntable td {\n  width: 100%;\n  padding: 10px;\n}\n\ntable tbody tr {\n  display: flex;\n  flex-wrap: wrap;\n  position: relative;\n  counter-increment: counter;\n}\n\ntable tbody tr::before {\n  content: counter(counter);\n  position: absolute;\n  top: 20px;\n  right: 20px;\n  width: 30px;\n  line-height: 30px;\n  text-align: center;\n  border-radius: 50%;\n  font-weight: bold;\n  color: var(--white);\n  background: var(--black);\n  z-index: 1;\n}\n\ntable tbody tr:nth-of-type(even) &gt; * {\n  background: var(--lightblue);\n}\n\ntable img {\n  display: none;\n  position: absolute;\n  top: 20px;\n  left: 45%;\n  max-width: 150px;\n  z-index: 1;\n}\n\n@media (min-width: 700px) and (max-width: 999px) {\n  table tbody {\n    display: flex;\n    flex-wrap: wrap;\n  }\n\n  table tbody tr {\n    width: 50%;\n  }\n}\n\n@media (min-width: 1000px) {\n  table th,\n  table td {\n    width: 25%;\n  }\n\n  table tbody tr {\n    display: table-row;\n  }\n}\n\n@media (hover: hover) and (min-width: 1000px) {\n  table tbody tr:hover {\n    cursor: pointer;\n  }\n  table tbody tr:hover img {\n    display: block;\n  }\n\n  table tbody tr:hover td:first-child::before {\n    display: block;\n  }\n}<\/code><\/pre>\n\n\n\n<p>You can check them all by clicking on the <strong>CSS <\/strong>tab of the demo.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this short tutorial, we covered a way to make a responsive table that will look great on different screens. On small and medium screens, we make it behave like a grid, whereas on larger screens, we have a typical table with cells.<\/p>\n\n\n\n<p>Here\u2019s a reminder of what we built:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codepen.io\/tutsplus\/pen\/MWXYXwP\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"722\" src=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-2-1024x722.jpg\" alt=\"final codepen table\" class=\"wp-image-86582\" srcset=\"https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-2-1024x722.jpg 1024w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-2-300x212.jpg 300w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-2-768x542.jpg 768w, https:\/\/elements.envato.com\/learn\/wp-content\/uploads\/2024\/02\/codepen-table-2.jpg 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>As always, thanks a lot for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to build a beautiful, responsive HTML table in this tutorial.<\/p>\n","protected":false},"author":192,"featured_media":86425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[207],"tags":[],"class_list":["post-71911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-design"],"acf":[],"_links":{"self":[{"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/posts\/71911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/users\/192"}],"replies":[{"embeddable":true,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/comments?post=71911"}],"version-history":[{"count":0,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/posts\/71911\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/media\/86425"}],"wp:attachment":[{"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/media?parent=71911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/categories?post=71911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elements.envato.com\/learn\/wp-json\/wp\/v2\/tags?post=71911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}