Previous Message
Next Message

[css-d] Re: CSS3 Selectors too complex?

Sent by Eric A. Meyer on 18 August 2002 23:11


At 8:02 +0000 8/18/02, Kevin W wrote:

>Also, some of the patterns are just useless.

    I would prefer that we not start arguing about which are not 
sufficiently useful, as it's been my experience that for every piece 
of CSS branded useless, someone will find themselves in a situation 
where the same piece is a lifesaver.  To illustrate my point, I 
thought I'd try to provide some examples of cases where the selector 
types in question might be useful, at least to some authors.

>E:root

    This one makes life a lot easier for implementors, not necessarily 
for authors.  However, I can envision cases where I want to do things 
to the root element without knowing ahead of time what it might be. 
Regardless of whether it's 'body' or 'html' in HTML documents-- and 
it's not clear which it should be-- XML documents will have their own 
root elements.  CSS is looking forward to styling XML as much as it 
is looking back at styling HTML.

>E[foo~="bar"]

    a[class~="warning urgent"] {color: red;}

This was introduced of CSS2, actually.  Since classes, for example, 
can contain values composed of multiple space-separated words, it can 
be useful to select an element based on two or more of those values. 
Also, the following two are exactly equivalent:

    div.example {color: red;}
    div[class~="example"] {color: red;}

If you remove the tilde from the second line, the two are no longer 
the same thing.

>E[foo^="bar"]

    a[href^="mailto"] {color: green;}

See also the use to which it was put in the article "Going to Print," 
found at <http://www.alistapart.com/stories/goingtoprint/>-- possibly 
a rare use, but nonetheless very useful for those who want or need it.

>E[foo$="bar"]

    a[href$=".pdf"] {font-weight: bold;}

>E[foo*="bar"],

    *[src*="doubleclick.net"], *[href*="doubleclick.net"] {display: none;}

>E:nth-of-type(n)

    tr:nth-of-type(2n) {background: yellow;}

That can also be accomplished with ':nth-child' when you're using 
HTML documents, but it's possible that an XML language would permit 
table-row elements to be interspersed with others.  For such 
circumstances, you need the ':nth-of-type' pseudo-class to get the 
alternate rows selected.  (Or every third, or whatever.)

>E:target

    *:target {background: yellow;}

That's one I've wanted for a long, long time.  The ability to style 
the element targeted by a link is really nice.  That way, the user 
doesn't have to guess which piece of the end of a document they're 
supposed to be reading when you point them to it.

>E:selection

    That's another one that implementors will probably find much more 
useful than authors-- but still, it would be nice to set the 
text-selection background color, wouldn't it?  That way, you prevent 
someone's light purple selection scheme from disappearing into your 
light-purple background color.

>But it'll be hard to read someone else's CSS without consulting the
>reference.  SelectORacle[1] is evidence of the unneccesary
>complicatedness that CSS3 is going into.

    When I proposed and Kevin wrote the SelectORacle, it was to 
explain CSS2 selectors, which many authors already found confusing 
and complicated.  I was talking about how nice it would be to have an 
automated tool people could use, and Kevin got that look in his eyes. 
He finished it in less than a week, I slapped some styles onto the 
interface, and there it was.  I wouldn't say this is evidence of 
unnecessary complication in CSS-- I'd say it's evidence that people 
can, and often do, like to have tools around to teach and remind them 
how things work.

--
Eric A. Meyer (http://www.meyerweb.com/eric/), List Chaperone
"CSS is much too interesting and elegant to be not taken seriously."
   -- Martina Kosloff (http://www.mako4css.com/)
Previous Message
Next Message

Message thread: