Halhelms
SIGN UP FOR MY NEWSLETTER

www.savorgold.com is top on wow gold and runescape gold and World of Warcraft gold provider list for trusted services. Their reputation seems to growing by the minute, which isn't surprising because they are one of the safest sellers of Gold. Delivery speed and customer service are very good. They aslo are giving some bonus items depending on the amount of gold you purchase.

 
 
Halhelms

Recent Comments

Recent Entries

RSS

CSS Sanity from Zed Shaw

Most developers I know have a dislike/hate relationship with CSS. We get that it's necessary, but -- really -- why is it so badly implemented? Design purists may shudder at things like Blueprint, Grid960, etc, but they exist because they serve a valuable purpose: making CSS somewhat intelligible. I've used them and they're much better than pure CSS, but they introduce their own complications. Meh -- it's life, right? Nothing to be done. But I just happened across a post by Zed Shaw that seems something of a miracle -- it restores sanity to CSS.

Zed's post is http://oppugn.us/posts/1287608776.html. If you're not familiar with Zed Shaw, he is the very definition of a brilliant curmudgeon. You'll get the "curmudgeon" part right away -- but what about the brilliance?

I'll show you some things I did with Zed's Oxymoron stylesheet, but first, what exactly is wrong with the present state of CSS? I've said for years that CSS was written by people who wanted to be programmers -- but aren't.

Yippee Kayay Moth...

But maybe I'm being unfair. Maybe it was really written by people who want to create an industry to sell books, tools, and training.

4,240 results for CSS books???

4,240 results for "CSS" in Books? Wow. But again, maybe I'm wrong. Maybe the quote widely attributed to Napoleon has it right: Never attribute to malice what is adequately explained by stupidity.

CSS is, simply, a train wreck. I know -- some wonderful smart and creative designers can make it do wonderful things but, as Zed explains, look at many influential design books and they begin with the idea of a grid.

Tables gave us grids. Easy grids. Understandable grids. But, of course, tables are taboo for layout. What's a poor programmer to do?

Adopt Zed's Oxymoron stylesheet. This is what it looks like:

body {
  width: 500px; /* I adjusted Zed's original to make it smaller */
  margin-left: auto;
  margin-right: auto;
}

.oxymoron { display:table; width: 100%; border-collapse:collapse; border-spacing:10px; }

.notarow { display:table-row; }

.notarow div { display:table-cell; }

How do you use it? Let's say I want to build a very simple "grid". I want "cell" A to be 300 pixels wide and cell "B" to be 400 pixels wide. Here's the HTML:

<html>
  <head>
  </head>
  
  <body>
    <div class="oxymoron">
      <div class="notarow">
        <div class="menu">I'll always be 300 pixels wide, whether 
there's any content in me or not. In fact, even if I have stuff that's 
much wider than 300 pixels, I'll still stay at 300 pixels.</div>
        <div class="main">I'll take up the remainder of what's left, 
the maximum width having been set by the body style. No floats 
required.</div>
      </div>
    </div>
  </body>
</html>

I'll give cell "A" the class attribute of "menu" and cell "B" the class attribute of "main". My CSS:

.menu{
  width: 300px;
  border: 2px solid red;
}

.main{ width: *; border: 2 px solid black; }

And that produces this:

Grid without the pain

Or, this one:

<html>
  <head>
  </head>
  
  <body>
    <div class="oxymoron">
      <div class="notarow">
        <div class="left-menu">I'll always be 110 pixels wide, whether 
there's any content in me or not. In fact, even if I have stuff that's much 
wider than 110 pixels, I'll still stay at 110 pixels.</div>
        <div class="main">I'll take up the remainder of hasn't been specified, 
the maximum width having been set by the body style.</div>
        <div class="right-menu">Hi</div>
      </div>
    </div>
  </body>
</html>

<style> .left-menu{ width: 110px; border: 2px solid red; }

.main{ width: *; border: 2px solid black; }

.right-menu{ width: 110px; border: 2px solid red; } </style>

that produces this:

Thank you, Zed Shaw

Now, we know the evils of using tables for layouts. I confess that, at times, I'm sorely tempted to backslide into temptation.

Evil

So that this starts to look very appealing:

He's a very, very bad man

<table width="500">
  <tr>
    <td class="left-menu"></td>
    <td class="main">
      <table>
        <tr>
          <td>Hello</td>
          <td>World</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

But I manfully resist the temptation. Of course, if I was so loutish as to use that code, it would produce something like this:

No, seriously -- don't do this

Can I have the simplicity of tables -- without the downside?

<div class="oxymoron">
  <div class="notarow">
    <div class="left-menu"></div>
    <div class="main">
      <div class="oxymoron">
        <div class="notarow">
          <div class="td">Hello</div>
          <div class="td">World</div>
        </div>
      </div>
    </div>
  </div>
</div>

That's seriously nice

It appears I can -- thanks to Zed Shaw.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
andy matthews's Gravatar Ooof. Formatting on this blog post is WAY wacked out Hal.
# Posted By andy matthews | 10/28/10 12:32 PM
Hal Helms's Gravatar Thanks, Andy. I tripped myself on the pre tag. Should be fixed now.
# Posted By Hal Helms | 10/28/10 2:49 PM
James, F.E.'s Gravatar I've also heard 'Never attribute to malice what is adequately explained by stupidity' attributed to H.L. Mencken
# Posted By James, F.E. | 10/28/10 2:52 PM
Steve Bryant's Gravatar I have to admit that I don't see the point. It looks to me like you are still using tables, but it takes more code (and less clear code) to do so and browser support is less universal.

I realize that you don't have any table tags in your HTML, but so what?

Despite what the purists might say, sometimes tables are the best solution - even in the modern world. Of course, often there are better ways (positional layout is often superior to floats, for example). I plan to start blogging on these kinds of HTML/CSS questions very soon.
# Posted By Steve Bryant | 10/28/10 7:14 PM
Dan Roberts's Gravatar This has been in css for years but did not work across all browsers (until recently?).
# Posted By Dan Roberts | 11/3/10 11:32 AM
John Quarto-vonTivadar's Gravatar I think the purists say simply to not use tables for layout purposes. Tables are fine for tabular data, which is the actual purpose of the TABLE tag
# Posted By John Quarto-vonTivadar | 11/11/10 2:14 PM
Jonny's Gravatar Have a look at http://html5boilerplate.com/ alot of good css tips there !
# Posted By Jonny | 12/10/10 8:25 AM
Rafael Torres's Gravatar Have you seen stylesheet languages like SASS (http://sass-lang.com) and LESS (http://lesscss.org)? These add advanced syntax capabilities like variables, nesting, mixins (code reuse) and arithmetic on top of regular CSS. At the end you "compile" your SASS or LESS code into a standard CSS file.

I haven't tried them myself, but they look pretty cool, and seem to address some of your complaints.
# Posted By Rafael Torres | 12/26/10 1:14 PM
 
   
Clicky Web Analytics