{"id":14164,"date":"2018-03-20T00:40:35","date_gmt":"2018-03-20T06:40:35","guid":{"rendered":"http:\/\/casadeblundell.com\/jonathan\/?p=14164"},"modified":"2018-03-20T00:40:35","modified_gmt":"2018-03-20T06:40:35","slug":"responsive-tables-with-or-without-bootstrap","status":"publish","type":"post","link":"http:\/\/casadeblundell.com\/jonathan\/responsive-tables-with-or-without-bootstrap\/","title":{"rendered":"Responsive Tables (with &#8230; or without Bootstrap)"},"content":{"rendered":"<figure id=\"attachment_14167\" aria-describedby=\"caption-attachment-14167\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table-500x143.png\" alt=\"An HTML\/CSS only responsive table example\" width=\"500\" height=\"143\" class=\"size-large wp-image-14167\" srcset=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table-500x143.png 500w, http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table-300x86.png 300w, http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table-768x220.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-14167\" class=\"wp-caption-text\">An HTML\/CSS only table on a desktop.<\/figcaption><\/figure>\n<p>I&#8217;ve never been a big fan of HTML tables. They take forever to code, they tend to have accessibility issues and they&#8217;re typically horrible on smaller screens.<\/p>\n<p>However, sometimes you just can&#8217;t get around them.<\/p>\n<p>If you&#8217;re using Bootstrap, there&#8217;s a <a href=\"https:\/\/getbootstrap.com\/docs\/3.3\/css\/#tables-responsive\" target=\"_blank\">out-of-the box fix<\/a>, using <code>.table-responsive<\/code>. This utilizes an overflow and lets the table scroll left to right on smaller screens &#8230; but &#8230; I&#8217;ve never been a fan.<\/p>\n<p>It&#8217;s not the most user friendly option and if you try to print the table, it almost always gets cut off (since you can&#8217;t scroll on a piece of paper).<\/p>\n<p>There are javascript options as well &#8230; but &#8230; I&#8217;ve never been a fan of those either. I always prefer to keep things simple with HTML and CSS if possible.<\/p>\n<p>So I&#8217;ve put together a basic option that uses a simple CSS solution, <code>with:100%<\/code> and <code>display:block<\/code>.<\/p>\n<figure id=\"attachment_14168\" aria-describedby=\"caption-attachment-14168\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2-500x454.png\" alt=\"\" width=\"500\" height=\"454\" class=\"size-large wp-image-14168\" srcset=\"http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2-500x454.png 500w, http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2-300x272.png 300w, http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2-768x698.png 768w, http:\/\/casadeblundell.com\/jonathan\/wp-content\/uploads\/2018\/03\/responsive-table2.png 1242w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-14168\" class=\"wp-caption-text\">An HTML\/CSS responsive table on a mobile device<\/figcaption><\/figure>\n<p>You can set it up with a single media query or I&#8217;ve modeled my solution to match the Bootstrap breakpoints.<\/p>\n<p>So for each breakpoint here&#8217;s the code for screens below 575px wide:<\/p>\n<p><code><br \/>\n@media screen and (max-width: 575px) {<br \/>\n.table-xs-responsive th, .table-xs-responsive td {<br \/>\nwidth: 100% !important;<br \/>\ndisplay: block !important;<br \/>\nbox-sizing:border-box;<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Note: I include border-box on the CMS I&#8217;m working with as it defaults to box-sizing: content-box<\/p>\n<p><a href=\"https:\/\/codepen.io\/jdblundell\/pen\/zWNRYY\" target=\"_blank\">View the code on Codepen<\/a><\/p>\n<p data-height=\"265\" data-theme-id=\"0\" data-slug-hash=\"zWNRYY\" data-default-tab=\"html,result\" data-user=\"jdblundell\" data-embed-version=\"2\" data-pen-title=\"Responsive Table (with Bootstrap)\" data-preview=\"true\" class=\"codepen\">See the Pen <a href=\"https:\/\/codepen.io\/jdblundell\/pen\/zWNRYY\/\">Responsive Table (with Bootstrap)<\/a> by Jonathan Blundell (<a href=\"https:\/\/codepen.io\/jdblundell\">@jdblundell<\/a>) on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p><script async src=\"https:\/\/static.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<p>Of course the one downside to this solution is the table headers don&#8217;t follow the content as you scroll on your mobile device but for most of our tables that&#8217;s typically not an issue.<\/p>\n<p>What do you think? Have you found a better solution for responsive tables?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve never been a big fan of HTML tables. They take forever to code, they tend to have accessibility issues and they&#8217;re typically horrible on smaller screens. However, sometimes you just can&#8217;t get around them. If you&#8217;re using Bootstrap, there&#8217;s a out-of-the box fix, using .table-responsive. This utilizes an overflow and lets the table scroll &hellip; <a href=\"http:\/\/casadeblundell.com\/jonathan\/responsive-tables-with-or-without-bootstrap\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Responsive Tables (with &#8230; or without Bootstrap)<\/span><\/a><\/p>\n","protected":false},"author":84,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[469],"tags":[4669,4533,4668],"class_list":["post-14164","post","type-post","status-publish","format-standard","hentry","category-design","tag-bootstrap","tag-responsive","tag-tables"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pEnSo-3Gs","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/posts\/14164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/users\/84"}],"replies":[{"embeddable":true,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/comments?post=14164"}],"version-history":[{"count":3,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/posts\/14164\/revisions"}],"predecessor-version":[{"id":14169,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/posts\/14164\/revisions\/14169"}],"wp:attachment":[{"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/media?parent=14164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/categories?post=14164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/casadeblundell.com\/jonathan\/wp-json\/wp\/v2\/tags?post=14164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}