{"id":233,"date":"2008-11-03T23:01:10","date_gmt":"2008-11-04T04:01:10","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=233"},"modified":"2008-11-03T23:01:10","modified_gmt":"2008-11-04T04:01:10","slug":"styling-zebra-striped-tables-with-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/styling-zebra-striped-tables-with-css\/","title":{"rendered":"Styling Zebra Striped Tables With CSS"},"content":{"rendered":"<p><a href='http:\/\/cssnewbie.com\/example\/zebra-tables\/'><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/11\/zebra-400.gif\" alt=\"\" title=\"Zebra striped table using CSS\" width=\"400\" height=\"109\" class=\"alignnone size-full wp-image-235\" \/><\/a><\/p>\n<p>I recently spent a bit of time on <a href=\"http:\/\/hundredpushups.com\/index.html\">the OneHundredPushups website,<\/a> and the aspect of the site that most caught my eye (aside from ninja-girl on the front page) was the zebra-striped tables strewn throughout. (For those not in the know, zebra striping tables refers to alternating colored backgrounds on table rows, a la iTunes.) <a href=\"http:\/\/www.alistapart.com\/articles\/zebrastripingmoredataforthecase\">Studies have shown<\/a> that there is (or at least can be) a slight performance increase associated with zebra striped tables (or at least there isn\u2019t a decrease), and that most users prefer zebra striping to traditional lined tables.<\/p>\n<p>I thought the tables on OneHundredPushups were fairly well done (<a href=\"http:\/\/hundredpushups.com\/week1.html\">take a look for yourself here<\/a>), but a glance at the code showed me that the developer had put more work than was necessary into creating his stripes. Here, I\u2019ll show you an easy way to create a table like the one in the image above <a href=\"http:\/\/cssnewbie.com\/example\/zebra-tables\/\">(or on the example page here<\/a>) that doesn\u2019t require you to mess with your XHTML &mdash; in fact, you could easily apply this to tables already on your website!<\/p>\n<p>Of course, before we can dive into the CSS, we\u2019ll need a table to style. Mine looks something like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">\n&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;\n\t&lt;tr&gt;\n\t\t&lt;th&gt;State&lt;\/th&gt;\n\t\t&lt;th&gt;Date of Poll&lt;\/th&gt;\n\t\t&lt;th&gt;% Voting Dem.&lt;\/th&gt;\n\t\t&lt;th&gt;% Voting GOP&lt;\/th&gt;\n\t&lt;\/tr&gt;\n\t&lt;tr&gt;\n\t\t&lt;td&gt;AR&lt;\/td&gt;\n\t\t&lt;td&gt;10\/27&lt;\/td&gt;\n\t\t&lt;td&gt;44&lt;\/td&gt;\n\t\t&lt;td&gt;54&lt;\/td&gt;\n\t&lt;\/tr&gt;\n\t&lt;tr&gt;\n\t\t&lt;td&gt;AZ&lt;\/td&gt;\n\t\t&lt;td&gt;10\/23&lt;\/td&gt;\n\t\t&lt;td&gt;41&lt;\/td&gt;\n\t\t&lt;td&gt;49&lt;\/td&gt;\n\t&lt;\/tr&gt;\n\t... and so on ...\n&lt;\/table&gt;\n<\/pre>\n<p>As you can see, it\u2019s nothing out of the ordinary. I\u2019ve used the old-school cellspacing and cellpadding attributes just to ensure consistent treatment across browsers. And otherwise, I\u2019m using a &lt;th&gt; tag or four to distinguish my table headers from the rest of my code. Otherwise, we\u2019ve just got rows and cells galore. You\u2019ll notice there\u2019s not a single class or ID present here (although if I only wanted to style <em>some<\/em> of the tables on my site, I\u2019d probably give the styled ones a specific class name).<\/p>\n<p>Next, let\u2019s take a look at the basic CSS styles I\u2019m using:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">table {\n\twidth: 100%;\n\tborder: 1px solid #cef;\n\ttext-align: left; }\nth {\n\tfont-weight: bold;\n\tbackground-color: #acf;\n\tborder-bottom: 1px solid #cef; }\ntd,th {\n\tpadding: 4px 5px; }<\/pre>\n<p>I\u2019ve set the table width to 100% to stop it from collapsing to its minimum size (the default behavior), then given it a pale blue border around the outside. I\u2019ve also set the text alignment throughout the table to \u201cleft\u201d &mdash; this is to prevent my table headers from centering themselves, which is the default. I prefer my headers to be aligned with my content, but your mileage may vary.<\/p>\n<p>When it comes to the headers, I\u2019ve done a little styling: I\u2019ve given them a background color (a bit darker than my border), bolded the text, and given the bottom of the headings a border what matches the border around my entire table.<\/p>\n<p>Finally, I\u2019m just adding a bit of padding to the interiors of the table cells, to give them a little breathing room. Personally, I think an appropriate use of white (or at least negative) space can be just as critical to legibility as zebra striping &mdash; perhaps someone out there more statistically inclined than me should look into that!<\/p>\n<p>Those styles give us our basic table layout, which looks something like this:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/11\/prezebra.gif\" alt=\"\" title=\"CSS styled table without zebra striping\" width=\"400\" height=\"107\" class=\"alignnone size-full wp-image-236\" \/><\/p>\n<p>So how do we get the alternating rows? For that, I\u2019m creating a new class in my CSS, specifically for the odd (that is, not-even) rows of the table:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">.odd {\n\tbackground-color: #def; }\n.odd td {\n\tborder-bottom: 1px solid #cef; }<\/pre>\n<p>This class, once applied to alternating rows of my table, would cause every other row to gain a pale blue background color and a slightly darker bottom border. <a href=\"http:\/\/www.alistapart.com\/articles\/zebrastripingmoredataforthecase\">Since this <em>A List Apart<\/em> study by Jessica Enders<\/a> suggested that alternating colored rows and underlines were two of the most aesthetically appealing ways to present tabular data, I figured&#8230; why not use both?<\/p>\n<p>Now if I wanted, all I would need to do is go back through my HTML and apply this class to alternating rows in my tables, like the folks at OneHundredPushups have done. However, you\u2019ll notice that my code above doesn\u2019t have any CSS classes. That\u2019s because I prefer to work smarter whenever I have the chance. Instead of turning back to my HTML, I\u2019m going to look to JavaScript (specifically, <a href=\"http:\/\/docs.jquery.com\/\">jQuery<\/a>) to do my heavy lifting for me.<\/p>\n<p>To apply our class automatically, all the jQuery I need is this:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(document).ready(function(){\n\t$(\"tr:odd\").addClass(\"odd\");\n});<\/pre>\n<p>In layman\u2019s terms, this tiny snippet is saying, \u201conce the document is done loading, cycle through the page, find every \u2018odd\u2019 table row, and give it a class of \u2018odd.\u2019\u201d Then our CSS takes over and does the rest. Oh, and for the record, you could also write a second rule like this:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(\"tr:even\").addClass(\"even\");<\/pre>\n<p>Which would do the exact same thing as above, only apply it to the \u201ceven\u201d rows of the table. And just so you\u2019re not caught off guard by this script\u2019s behavior, it\u2019s good to know in advance that the :odd and :even pseudo-classes start counting at zero, not one. So the first row of your table is, according to jQuery, the \u201c0th\u201d row, and is therefore even.<\/p>\n<p>And there you have it! <a href=\"http:\/\/cssnewbie.com\/example\/zebra-tables\/\">You can see the whole thing in action here.<\/a> And now, go forth and make the web a more beautiful (or at least stripy-er) place.<\/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>In this article, I will show you an easy way to create an easily scanable and aesthetically pleasing table that doesn\u2019t require you to mess with your XHTML. In fact, you could easily apply this to tables already on your website! [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/styling-zebra-striped-tables-with-css\/\" title=\"Click to read 'Styling Zebra Striped Tables With CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168,184],"tags":[199,291,130,384],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/233"}],"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=233"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}