Zebra striping tables with CSS3

Forums » Dev.Opera » Archived Article Discussions

This topic has been closed. No new entries allowed.

Reason: You can now post comments on articles on Dev Opera

Forum rules and guidelines

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

28. August 2008, 13:12:43

teleject

Posts: 3

Zebra striping tables with CSS3

In this article Christopher Schmitt gives a succinct overview of how to use the nth-child CSS3 property to easily create zebra-striped HTML tables.

( Read the article )

31. August 2008, 17:01:25

floyd1616

Posts: 6

Hi all, Hi Christopher
also this article was translated in Italian: http://www.extrowebsite.com/articolocss.asp?id=81 smile

Giuseppe

1. September 2008, 05:44:27

FataL

Opera freak

Posts: 1443

The question is it possible to make nth-child to count only elements with some class name?
Now seems that the current algorithm is to gather pseudo-classes first...
This can be very usefull when you want to show and hide dynamically groups of rows in a table and want to keep proper "zebra styling" of rows.
tbody tr.zebra:nth-child(even) {background-color:Gray}
See examples here: http://rusrestaurant.com/tests/nth-child/nth-child-hidden.html
Main browser and mail: 9.27 • Secondary: 10.63 (still has annoying UI regressions: inability to detach tab normally, passes source file w/o extension to external editors)
extendopera.orgReport bugs to public BTS

1. September 2008, 15:43:36 (edited)

brucelawson

Posts: 38

Hi FataL

interesting question, and I had to go to the engineering group within Opera to get an answer.

The behavior you document is correct although it feels unintuitive.

Selectors work on the DOM tree, not on the visual tree, so whether or not an item has a display property of none makes no difference to the calcualtion of selectors.

1. September 2008, 16:52:30 (edited)

FataL

Opera freak

Posts: 1443

Thanks for reply Bruce!

Selectors work on the DOM tree, not on the visual tree, so whether or not an item has a display property of none makes no difference to the calcualtion of selectors.

This is true. But I'm talking about different behaviour of nth-child.
Imagine (or look at my examples) that some of rows have class hideit and applied style like
.hideit {display:none}
(let's not talk about accessibility this time wink )
I would expect that this stylesheet rule will paint my table correctly -- taking to account rows w/o class.
tbody tr:not([class]):nth-child(even) {background-color:Gray}
This selector works only with DOM tree, looking for a class attribute befor applying nth-child.

I would like to propose this changes to CSS 3 selectors spec, but first I want to know if it's doable for browser vendor.
Main browser and mail: 9.27 • Secondary: 10.63 (still has annoying UI regressions: inability to detach tab normally, passes source file w/o extension to external editors)
extendopera.orgReport bugs to public BTS

2. September 2008, 15:57:51

brucelawson

Posts: 38

Hi FataL
the response I got yesterday to your initial question was

"Selectors are designed to be invariant of CSS declarations. Also, Selectors is a standalone concept and can be used outside of CSS (eg, in XBL)"

It always evaluates the :nth-child first. Opera and Safari agree on that.

I made a test case to show some of my colleagues, and it was a nightmare to get our brains round. I agree with you that it's not the way a web author would expect it to work at all, particularly if you're used to CSS counters which are incremented with visibility:hiddenn, but not incremented if they take display:none'.

I'm going to write up a blog post for the <a href="http://my.opera.com/ODIN/blog/">Opera Developer Network blog</a> before the end of the week detailing all this.

2. September 2008, 16:30:35

FataL

Opera freak

Posts: 1443

Ok, thank you very much for quick investigation. So I can't use CSS 3 selectors for painting dynamic tables and need to write some smart JS that will reapply classes on visible table rows every time user show/hide something...

CSS counters which are incremented with visibility:hiddenn, but not incremented if they take display:none'

Really? bigeyes
I should take a note about this somewhere. smile
Main browser and mail: 9.27 • Secondary: 10.63 (still has annoying UI regressions: inability to detach tab normally, passes source file w/o extension to external editors)
extendopera.orgReport bugs to public BTS

3. September 2008, 08:44:55 (edited)

brucelawson

Posts: 38

Yeah, here's the item in the CSS 3 generated content spec
http://www.w3.org/TR/REC-CSS2/generate.html#q10 (which is, of course, still being developed and so is something of a moving target - see my blogpost on replacing images with the <code>content</code> property from the same CSS 3 module http://my.opera.com/ODIN/blog/css-3-image-replacement

12. September 2008, 19:21:05

FataL

Opera freak

Posts: 1443

At least Opera should fix this:
http://www.quirksmode.org/css/nthchild.html

Opera has problems when elements are added dynamically.

Main browser and mail: 9.27 • Secondary: 10.63 (still has annoying UI regressions: inability to detach tab normally, passes source file w/o extension to external editors)
extendopera.orgReport bugs to public BTS

13. January 2009, 19:48:47

FataL

Opera freak

Posts: 1443

Originally posted by brucelawson:

I'm going to write up a blog post for the Opera Developer Network blog before the end of the week detailing all this.

Still impatiently waiting smile
Main browser and mail: 9.27 • Secondary: 10.63 (still has annoying UI regressions: inability to detach tab normally, passes source file w/o extension to external editors)
extendopera.orgReport bugs to public BTS

20. May 2010, 04:38:45

dhtmlkitchen

Posts: 3

The article uses ":nth--child" exclusively. Shouldn't that be ":nth-child" instead?

20. May 2010, 08:04:18

Opera Software

chrismills

Posts: 378

Originally posted by dhtmlkitchen:

The article uses ":nth--child" exclusively. Shouldn't that be ":nth-child" instead?



Yup - fixed!
Chris Mills
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com

Forums » Dev.Opera » Archived Article Discussions