happy astronomy day || MAIN || battery update

April 25, 2004

css tutorials?

So now that I've got this wonderful extension, URIid (and am looking forward to the day that we have @ rules for matching on URLs integrated into Gecko,) I'm doing a lot of customization of the sites I visit regularly.

I don't think I've ever really tried to learn CSS except by experimentation with the specs when I wanted to achieve a specific look here at my weblog or in tracking down a Mozilla bug. What I'm looking for now is a tutorial or other hand-holding document which will give me a better grip on using CSS. I'm not looking for links to the specs. I already have bookmarks to all of the w3c CSS specs.

What I'm really looking for is a guide that will teach me the most efficient ways to select a bit of random web content and modify its display. An example of the type of questions that I need answered woudld be something like "how do I select on and modify HTML table padding and cell spacing attributes using CSS?"

So, do any of you know of any tutorials or other documentation for the practical modification of other peoples' web pages with user style sheets?

Posted by asa at April 25, 2004 11:11 PM
Comments

It sounds like what you really need is a bookmarklet like Computed Styles to show you the best route to the element you want to style.

Posted by: Jemal on April 26, 2004 02:01 AM

Could you give some examples on, what customizations you have been making? I am not really shure, I see the potential, so I would be nice to be enlightened. I imagine that some of the changes should be made by the webmaster (to benefit all) and then a e-mail to the webmaster might be a better way (works for me :) ).

Posted by: Lauritz Jensen on April 26, 2004 02:14 AM

Lauritz: I means that you (as a reader) can change the font on Asa's page, if you happen to hate Verdana for some reason.

Posted by: Rune on April 26, 2004 06:46 AM

Staying close to home, I'd suggest that the DOM Inspector is your friend. It'll allow you to find the correct selector for the element you want to alter (in a clearer way, I think, than the computed styles bookmarklet).

As far as tutorials go I'd probably recommend the w3schools CSS Tutorial (http://www.w3schools.com/css/default.asp) which even has live demo pages - for example modifiyng table padding using CSS: http://www.w3schools.com/css/tryit.asp?filename=trycss_padding-left

I don't think you can go far wrong with these two. :)

Posted by: Phil Wilson on April 26, 2004 07:31 AM

I have not found any really good selector tutorials, it seems to be mostly ignored, since IE doesn't support > or +. (You can do some really crazy stuff with ~ and :not, like creating hierarchical presentation from a flat document structure. See /css/outline.css on my website for an example.)

Posted by: Eric Hodel on April 26, 2004 08:50 AM

I was going to recommend w3schools, but I see that Phil already has. Cool.

Posted by: Bernie Zimmermann on April 26, 2004 11:40 AM

Thanks for the suggestions so far. I already know how to use the DOM Inspector and various bookmarklets to find the elements I'm interested in selecting on. What I really needed was a quick tutorial on advanced selector use, how to get more done with less (combining rules, etc.) and maybe some more on the basics of differences in behavior for block and inline elements. I'll check out the w3schools site, for sure. Thanks, again, for the good suggestions.

Posted by: Asa Dotzler on April 26, 2004 12:51 PM

Here's another example of where I'm deficient. For this example (pulled from a thread below):
#story-news-yahoo-com tr,td,th{line-height: 20px !important;}
I would have used something like:
body[id="story-news-yahoo-com"] tr,
body[id="story-news-yahoo-com"] td,
body[id="story-news-yahoo-com"] tr
{line-height: 20px !important;}
Now, clearly, I need help. Is there a way to, for example, use similar shorthand for something like this:
body[id="www-foo-com"] td[width="160"],
body[id="www-foo-com"] td[width="150"],
body[id="www-foo-com"] tr[bgcolor="#333366"]
{display: none !important;}
so that I don't have to retype all of the preceeding stuff each time? It gets even more painful if I'm selecting on stuff that's really burried, like:
body[id="www-foo-com"] table[width="93%"][cellspacing="6"][cellpadding="7"][border="2"] td[bgcolor="#ccffff"][width="16%"][valign="top"],
body[id="www-foo-com"] table[width="93%"][cellspacing="6"][cellpadding="7"][border="2"] td[bgcolor="#ccffff"][width="18%"]

Because I've always just poked at things until they worked, I never really learned any shortcuts or even the "proper" ways to do things. What I'm looking for in a tutorial is something that will give me the basics of how to write css properly and efficiently with a slant towards "coping with poorly designed and implemented sites" (the ones that don't use nice IDs and classes -- or CSS at all -- to display things).

Posted by: Asa Dotzler on April 26, 2004 01:17 PM

i don't know of any tutorials or advice for what you're doing, other than to say it sounds like what theme designers do. instead of creating a layout from scratch, they start with a preexisting theme, then target and restyle the various elements. beyond that, all i can say is that DOMI is priceless for helping you visualize the structure of a page (or theme) and choosing which selectors are appropriate. i also always end up referring to the w3c's chart of css3 selectors to get an idea of how to attack:
http://www.w3.org/TR/css3-selectors/#selectors

http://css.maxdesign.com.au/selectutorial/index.htm
has some diagrams and comparisons of different selectors, with some explanation and examples that might be useful.

Posted by: miahz on April 26, 2004 01:52 PM

usually, you can get away with only specifying one unique value, so for that last example you gave, you might be able to use:

body[id="www-foo-com"] table td[width="16%"][valign="top"],
body[id="www-foo-com"] table td[width="18%"]

but without seeing the actual page, i couldn't say for sure.

one thing i often use in themes is the direct adjacent combinator: +
handy if the element you want to target doesn't have anything "to grab onto" but something preceding it does.
say you're after a table that follows another table and a div with the id "title". you could use:

div#title + table + table {}
div#title + table + table td {}
etc.

another thing i should mention, is that, for me at least, it's just a lot of trial and error.

Posted by: miahz on April 26, 2004 02:17 PM

One place I've seen many regular bloggers rave about is csszengarden.com. I have a feeling that this won't be the place to quickly find answers about how to customize individual sites to your liking with your user style sheet, but it is a good place to go to learn about what CSS is capable of accomplishing.

Posted by: Cameron on April 26, 2004 03:26 PM

The css-discuss mailing list is basically the user support group for css, their website http://www.css-discuss.org/ has archives of all mailing list and there is a wiki there. I think that it's probably a good place to look through.

Posted by: Michael on April 26, 2004 06:02 PM

miahz! thank you! that direct adjacent combinator is going to make a world of difference for me. So much will be so much easier if I can make that work.

So if I have an element that I can "grab onto" but I really want to style its parent or child (which I can't grab onto), is that easily done as well?

Posted by: Asa Dotzler on April 26, 2004 06:19 PM

Asa, the absolute authority on getting more done with less is Eric Meyer. He worked for Netscape as their Evangelical CSS guru - he's the authority. www.meyerweb.com. Go explore his website and look at all his samples. I learned a lot about CSS from him by just looking at his examples and reading his methodology of keeping it as simple as possible, and have to admit I've lifted a line or two from his work... Check the "CSS Edge" section.

Posted by: Brian A. Jenkins on April 26, 2004 06:31 PM

asa:
yeah, children can be targeted. that second rule was for a "td" inside "div#title + table + table". you just add child elements to the selector using a space ("decendant combinator" http://www.w3.org/TR/css3-selectors/#descendant-combinators).

i don't think you can reach parents from children. i'm pretty sure it only works "downstream," altho i seem to remember a bug about it, or maybe something on daniel glazman's blog. ah:
http://webperso.easyconnect.fr/danielglazman/weblog/index.php/2004/02/26/175-CssSelectorsNotEnough

that table of selectors linked in my first comment lists all the "simple selectors" you can use, and gecko supports most of them.
some others that might be useful:

div > table:first-child {}
http://www.w3.org/TR/css3-selectors/#structural-pseudos

table td:not([width="20%"]) {}
http://www.w3.org/TR/css3-selectors/#negation
http://www.w3.org/TR/css3-selectors/#attribute-selectors

i guess it's all about being clever in finding unique elements that are targetable, then figuring out what selectors to use to connect the dots.

Posted by: miahz on April 26, 2004 08:04 PM

http://gallery.theopalgroup.com/selectoracle/

Posted by: bachius on April 27, 2004 05:23 AM

http://www.quirksmode.org/

helped me a lot, not only with CSS

Posted by: Shadow3333 on April 27, 2004 06:41 AM

As mentioned above, Eric Meyer's site is a great resource, along with Tantek's Log. The one site you CAN'T live without is www.alistpart.com, which is perhaps the best web design e-zine ever. Their older CSS articles in particular would cover the kinds of things you're interested in.

Posted by: Jonathan Dobres on April 27, 2004 10:34 AM

A few drive-by comments:

1) "body#foo" and "body[id='foo']" are very different selectors -- different specificity, different meaning, and very differently handled by the style system optimization-wise.

2) Depending on your goals, you may want to read http://www.mozilla.org/xpfe/goodcss.html -- it won't help you write more readable CSS, but it may help you write CSS that won't slow down your pageload as much. Some of the "stick a class on everything" advice obviously won't apply, but apart from that, it's a good readable description of how various selectors get treated by the style system.

Posted by: Boris on April 27, 2004 10:59 PM

thanks boris, that's a very interesting document.

Posted by: Asa Dotzler on April 27, 2004 11:13 PM

ditto.
i've wondered about different rules that "did" the same thing. a part of my theme ended up getting rule-heavy and noticably slowed things down, so i bulk-removed some styles just to see if it would have an affect, and it did speed things back up. at least now i have some guidelines i can use to optimize things.

Posted by: miahz on April 29, 2004 01:26 AM

Post a comment