{"id":730,"date":"2014-01-06T15:47:57","date_gmt":"2014-01-06T21:47:57","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=730"},"modified":"2022-08-17T04:23:57","modified_gmt":"2022-08-17T04:23:57","slug":"simple-table-column-highlighting","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/simple-table-column-highlighting\/","title":{"rendered":"Simple Table Column Highlighting"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-736\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2014\/01\/cssnewbie-table-highlighting1.png\" alt=\"A table with the third column and second row highlighted.\" width=\"800\" height=\"130\" \/><\/p>\n<p>Though they\u2019re often treated like the redheaded stepchild of modern web design, tables are still one of the best ways of displaying concise tabular data. But the more data that gets packed into a table, the tougher they get to read. <a title=\"Styling Zebra Striped Tables With CSS\" href=\"http:\/\/cssnewbie.com\/styling-zebra-striped-tables-with-css\/\">Zebra striping the rows<\/a> helps some, and column striping can help even more.<\/p>\n<p>One design technique I&#8217;ve always enjoyed is highlighting the row and column of any cell that the user hovers over. Highlighting a cell\u2019s row is simple, because table cells are organized into rows: all you have to do is apply a style to tr:hover, and you\u2019re done. Columns are harder because there\u2019s no inherent structure for a column in a table, which means loads of complex JavaScript is inevitable. Or so I thought.<\/p>\n<p>I&#8217;ve been building HTML tables for nearly two decades now. Today I realized two things:<\/p>\n<ol>\n<li>There&#8217;s such a thing as a col tag for tables (who knew?).<\/li>\n<li>It&#8217;s going to make my life easier.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<p>The col tag is a special content-less element that lives inside of your tables. They\u2019re used to specify columns in tables \u2013 columns that can be styled. The general use looks like this:<\/p>\n<table>\n<colgroup>\n<col \/>\n<col \/>\n<col \/> <\/colgroup>\n<tbody>\n<tr>\n<td>col 1<\/td>\n<td>col 2<\/td>\n<td>col 3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre lang=\"html5\"><\/pre>\n<p>Unfortunately, since our table cells aren\u2019t direct descendants of the col tag (like they are to the tr tag) and our columnss have no content, we can\u2019t just use col:hover. However, having columns make the JS we have to write much, much simpler, particularly when using a library like jQuery. Check out this example:<\/p>\n<p class=\"codepen\" data-height=\"268\" data-theme-id=\"0\" data-slug-hash=\"45a262288e909448afed48374a9b8935\" data-user=\"rglazebrook\" data-default-tab=\"result\">See the Pen <a href=\"http:\/\/codepen.io\/rglazebrook\/pen\/45a262288e909448afed48374a9b8935\">45a262288e909448afed48374a9b8935<\/a> by Rob Glazebrook (<a href=\"http:\/\/codepen.io\/rglazebrook\">@rglazebrook<\/a>) on <a href=\"http:\/\/codepen.io\">CodePen<\/a><\/p>\n<p><script async=\"\" src=\"\/\/codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<p>I\u2019m using a few nice jQuery tricks to make this as simple as it is. We&#8217;re using jQuery&#8217;s hover function, which fires both on mouseenter and mouseleave (this is important). When someone hovers (or un-hovers) over a cell, we first move up to the parent table. Then we find the col tag that has the same index as our td tag. (Indexes are zero-based, so the 3rd col in the table will have an index of 2&#8230; as will the 3rd cell in any row.)<\/p>\n<p>Once we\u2019ve found our column, we toggle a class of \u201chover\u201d on it. ToggleClass is a nice shortcut function that adds a class if it\u2019s not present and removes it if it\u2019s already there. This is perfect for us, because jQuery\u2019s hover function fires once when the cursor moves over our cell, and again when the cursor leaves the cell.<\/p>\n<p>Once our JS is in place, the CSS becomes a piece of cake: just write styles for both tr:hover and col.hover. I\u2019m using a semitransparent background color in the example above, so the cell we\u2019re hovering over looks even darker.<\/p>\n<p>And there you have it. Three lines of JS and two CSS rules later, we have an easier-to-read table.<\/p>\n<p>One thing if you ever find yourself in a situation where you have to export an <a href=\"https:\/\/yourblogcoach.com\/export-html-table-to-csv-using-javascript\/\">HTML table to CSV<\/a> using JavaScript you should know it is quite simple and easy to do.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>One design technique I&#8217;ve always enjoyed is highlighting the row and column of any cell that the user hovers over. Highlighting table rows is easy, but columns are hard. Or so I thought. Using table cols vastly simplifies the process. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/simple-table-column-highlighting\/\" title=\"Click to read 'Simple Table Column Highlighting'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":736,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[223,104,84,55,291,360],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/730"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=730"}],"version-history":[{"count":1,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/730\/revisions"}],"predecessor-version":[{"id":3576,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/730\/revisions\/3576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/736"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}