{"id":453,"date":"2011-12-04T23:15:13","date_gmt":"2011-12-04T23:15:13","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=453"},"modified":"2011-12-04T23:15:13","modified_gmt":"2011-12-04T23:15:13","slug":"styling-tables-rowspan","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/styling-tables-rowspan\/","title":{"rendered":"Styling tables with rowspan"},"content":{"rendered":"<p>Tables have got to be the most awkward set of elements offered by the HTML spec. Both JavaScript and CSS have their issues in dealing with tables, yet they still remain the most popular way to not only present data, but to render a sites layout.<\/p>\n<p>Without CSS 3 and JavaScript, you can get by using :first-child and sibling selectors to style most basic table structures. But what happens, when attributes such as rowspan are thrown into the mix?<\/p>\n<p>This presents an issue for both <em>:first-child<\/em> and <em>sibling<\/em> selectors, as it doesn&#39;t always accurately return the first child or sibling on the rendered table.<\/p>\n<p>Take, for example, the following table:<\/p>\n<p><code>&lt;table&gt;<br \/>\n\t&lt;tbody&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t&nbsp; &lt;td rowspan=&quot;2&quot;&gt;x&lt;\/td&gt;<br \/>\n\t&nbsp; &lt;td&gt;x&lt;\/td&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;tr&gt;<br \/>\n\t&nbsp; &lt;td&gt;x&lt;\/td&gt;<br \/>\n\t&lt;\/tr&gt;<br \/>\n\t&lt;\/table&gt;<br \/>\n\t<\/code><\/p>\n<p>\n\tOn the rendered table,<em> tr td:first-child<\/em> will return the first row and column, but will also select the first-row and column of the second row (visually). :first-child will also select the second row, second column. This presents an issue when we want to use CSS to style our table.<\/p>\n<p>jQuery to the rescue. By using the <em>[rowspan]<\/em> selector, we can add a class on all table cells that make use of the rowspan attribute:<\/p>\n<p><code>$(&#39;td[rowspan]&#39;).addClass(&#39;hasRowSpan&#39;);<\/p>\n<p>\t<\/code><\/p>\n<p>This then makes our CSS a little more flexible:<\/p>\n<p><code>table tr td.hasRowSpan { }&nbsp; \/* first column (visually) *\/<br \/>\n\ttable tr td:first-child, table tr td.hasRowSpan + td { } \/* second column (visually) *\/<br \/>\n\ttable tr td:first-child + td + td { } \/* third column (visually) *\/<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tables have got to be the most awkward set of elements offered by the HTML spec. Both JavaScript and CSS have their issues in dealing&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[12],"tags":[145],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/453"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/453\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}