Andrew Jaswa

Markup Conditionals

I loathe conditional comments and browser targeted code. But there comes a time when you have to support an old and out dated browser (I’m looking in the direction of IE6 at the moment). So what do you do? You have to make it look the same and function the same as other browsers. You also shouldn’t use browser hacks to target them. I’ve touched on this subject before and figured conditional comments were the best route to go. Even if I don’t like them.

I’ve recently started working with a new company and have picked up on some of the bright things they do. I’m not sure why I never thought of this before since it seems so simple (and I can’t take credit for it).

Let say you have your standard body element and a few other things on a page:

<body>
    <div>
        <p>Some text</p>
    </div>
</body>

Typically to target the paragraph element for, say IE6, you might use a simple hack like the underscore hack. So you might write some CSS like so:

p {
    width:150px;
    _width:200px;
}

I mean you might do that, some other hack or you might use conditional comments to provide a style sheet just for IE6. I’m sure you’ve seen code like this before:

<!--[if IE 6]>
    <link rel="stylesheet" type="text/css"
       href="ie6.css" />
<![endif]-->

That’s all fine and dandy, but do you see what you did there? I did and I don’t like it. You’ve just created another HTTP request. Shame on you. And to think about how many CSS rules are typically in a file like that? 10? 50? It also creates a maintenance headache. You’ve now split up rules for one element into two files. Yes yes I know that is what the cascade is for… But you could make it easier on yourself and people that come behind you.

What I’ve found is to use conditional comments to specify a class on the body element when the browser is IE6 or 7 or even 8.

So taking our markup example from above I’ve modified it to have conditional comments in it:

<!--[if IE 6]><body class="IE6"><![endif]-->
<!--[if !IE ]><!--><body><!--<![endif]-->
    <div>
        <p>Some text</p>
    </div>
</body>

You’ll need all those extra comments to make the page valid. It is also pretty easy to add as many combinations as you like as long as they can be read by the browsers…
And our CSS from above:

p {
    width:150px;
}
.IE6 p {
    width:200px;
}

Of course this only works with IE but really you shouldn’t need this at all since you build awesome websites that are already cross-browser compatible, right? And there is no chance ever that a client will want a site to be usable in IE5.5, right?

category code, websites
tags: , ,
May 13, 2009

February’s Article on 13things

13things br
The second 13things article is now published. Zach Young gives a great explanation as to the uses and mis-uses of an often neglected html element. You should go check it out.

You can also follow @13things on twitter to get updates about new articles and new developments on the site.

category 13things, code, websites
tags:
February 14, 2009

Feeding the addiction

So I have a twitter addiction. That’s right I like tweeting. I recently surpassed my 2000th tweet and my 100th follower (that wasn’t a spam bot). In fueling this addiction I’ve created 2 twitter bots: Acropoll and SandCTweet. Last night I put the finishing touches on a plug in for Wordpress that I started a long while ago. This morning I submitted it to Wordpress.org.

The point of Bird Feeder is simple. Nearly all the twitter plug ins I’ve tried do more then I’ve needed. So Bird Feeder does the one thing I wanted: tweet about a new blog post. That’s it nothing else. It probably won’t ever do anything else. Though I’m sure I’ll keep it updated with bug fixes or better ways of doing things when I get the time.

Speaking of bugs, one of the first users found something I over looked: It tweeted every time a post was updated. Even if it was already published. It was a simple mistake that has now been remedied.

Jeremy Sanchez, owner of Metaicon also runs a URL shortening service called shortna.me. He set up a API for me to call when tweeting so that it doesn’t use the full URL in the tweet. Of course this is to help keep the character count down.

So head over to Wordpress.org and check out Bird Feeder.

category code
tags: , ,
December 30, 2008
I build crappy websites every day!
Andrew Jaswa
Support me on Amazon