What they didn’t tell you about CSS

The frustrating thing about learning new Web technologies is the lack of “why didn’t someone tell me this before I began” need-to-know information. Especially with CSS, you’ll get tiny tips, tricks, hacks and everything in between, but for fear of lynching in the Web community, the last thing you’ll get is the bad with the good. I’ve been using CSS professionally now for a few years, and although I enjoy using it, there are definitely some points of contention worth noting. If you’re new to CSS, read through the list carefully, and remember to do your homework. (FYI - any statement of “this can’t be done” is a reference to purity - i.e. - it can’t be done without a hack.)

Vertical Alignment

There is no concept of vertical alignment in CSS. Oh, there is a vertical-align property in CSS, but it isn’t for what you might think it’s for — you know, vertically aligning the way grandma taught you. If you’re used to the valign attribute in table data cells, you’re out of luck. Two DIV elements, floating next to each other, will float to the top, and only to the top, you know, like it’s flooooooating.

Centering Floats

It doesn’t take long to figure out you can only float elements left or right. How suspiciously convenient - most top-level navigation on CSS sites is either left or right justified. Hey, just like this blog! Unless you know the width of your navigation (and if it’s dynamic, you won’t), forget about centering several block level elements that sit next to each other.

Clearing Floats

Clearing floats is like clearing out the penguin pen at the zoo on a 120 degree day. Don’t waste your time researching all the different methods to clear a float (without adding extra space). There is only one method that works well, and it’s:

XHTML:
<div style="clear: both;"><!-- --></div>

Other than that, you’re stuck with the old standby:

XHTML:
<br style="clear: both;" />

Understanding and implementing the float and clear properties is the most difficult concept to digest with CSS and it’s inconsiderably inconsistent.

Site Degradation

I still can’t figure out why anyone would want their site to “degrade gracefully”. Although I understand the Utopian idea of access for all, building primarily with that mindset can be prohibiting to your primary pool of users. Also, using CSS in order to achieve this effect only works in limited circumstances. The more complicated the build, the more unlikely it becomes your site will degrade any more gracefully than if you had used tables.

Pixel Precision

You know that unit of measurement, you know the one I’m talking about; everyone is using it these days. I believe it’s called the pixel, and it’s wonderfully precise. Don’t let anyone tell you that if you’re developing a site with CSS, pixel precision gets the boot. This is not freedom from constraints, but laziness. In most cases, if it can be done, it can be done with CSS, and to pixel precision.

CSS = Accessibility

I can’t believe how many developers/designers spin the tale that if you use CSS, your site will be more accessible. This is an incredibly naive statement that continues to rear its ugly head. In the W3C Checklist of Checkpoints for Web Content Accessibility Guidelines 1.0, Priority 1 checkpoints, there isn’t a single reference to CSS. The aural media type and associated properties, which were developed specifically for the visually impaired by CSS aren’t even supported by a single mainstream browser.

Hacks

You’d think with all the attention hacks get, with numerous books, articles and blogs written on the subject, that it must be okay to use them. From day one, I’ve shied away from this approach, and I’m glad that I did. Learn the structure and syntax for writing pure CSS and stick to it, regardless of how frustrating it might seem. You’ll find that in the future as browsers are updated, the stability of your code will be maintained. Don’t be fooled by the “well, just hack it” mentality.

Leave a Comment

I'm sorry, but comments are now closed. If you have further questions or comments, please feel free to email them.

5 Comments

#01, Jan 31 2007

Anil

Hi Brian

The web URL is not mine, btw…

Appreciate your site.

1. Vertical align does work inside table cells. We do still use tables, don’t we? I believe it’s worth knowing for a new starter in CSS that you can vertically align text in table cells. I often use vertical-align:bottom for table header cells that have mutliple lines of text that differ from column to column.

2. Took me a while to hit on margin:auto. This does achieve some centering effect. Haven’t fully tested it yet since I just started using it.

3. Is that different from ?

7. I hate hacks. I can’t stand the complexity that developers are prepared to put up with sometimes for a simple little feature. I take the approach that if it requires a hack it’s just not available. This may be the lowest common denominator but I don’t want to put up with these added complexities in an already complex game. I do make exceptions when really required but most of the time: no hacks.

Thanks
Anil

#02, Feb 01 2007

Brian

Hi Anil,

Great comments, especially regarding using vertical-align for tables. I should have stated more clearly there is no vertical-align for the DIV tag.

As far as clearing floats is concerned, there is a “new” method developed by Tony Aslett of csscreator.com that some developers prefer. That would be one alternative.

Brian

#03, Feb 01 2007

Anil

Oops my HTML code got sucked into the page.
What I meant was:

3. Is your float breaker:
<div style=”clear: both;”><!– –></div>
any different from:
<div style=”clear:both” />
Is there any reason to add an empty comment inside the break div?

#04, Feb 02 2007

Brian

Hi Anil,

If I remember correctly, it had to do with adding styles to the clearing element. Without the empty comment, it would be considered empty, and would not register padding or margins, or something of that sort. Occassionally, you may need to do that, and so it is better to add the comments for when the situation arises.

It is actually best to plan around that altogether, and allow containers that actually have content to become your clearing element. For instance, a floated column left next to a floated column right, with a footer that sits beneath that clears them. I find this to be the most intuitive technique.

#05, Jun 06 2007

marinheiro

concerning the floats, i always use a div with a class called clear, that has clear:both and heigth:1px in the css and aply it where i need it.