nicholasjon.com :: a weblog

Hi. I'm Nick Olejniczak and this is where I write about technology, the web and other bits of general geekery. I'm a web developer who loves Ruby on Rails, web standards and personal publishing. Interested in working together? Get in touch.

 

CSS Sleeves

August 12, 2006 at 4:30pm

Sometimes, it’s hard to write good XHTML. I don’t mean it’s hard to write well-formed XHTML, once you start doing that it’s nearly impossible to allow yourself a single unclosed p-tag. I mean sometimes, when there’s a deadline sitting in the chair right next to you, things like semantics get ignored while a few extra bits of markup get thrown in.

Take a deep breath, this sort of behavior is okay.

Sure it’s not ideal, and I hope the standardistas don’t take away my ALA t-shirt for saying so. It is something that happens, and it’s something we need to handle without just whacking something in with a table. (Shudder.)

This is a technique I use in those situations I call “sleeves.” It’s particularly handy when I need to get a few columns or other content containers in place “um, like now.” I’m particularly fond of little trick actually as it involves only a single bit of extra markup per container, and it completely circumvents cross-browser box model inconsistencies. It’s quick to implement, and it’s essentially bulletproof.

You start by “framing” your page — let’s say we’re doing a three column, “wide narrow narrow” layout. We could write some CSS to the effect of:

div.wide { width: 60%; float: left; } div.narrow { width: 20%; float: left; }

… to go along with some XHTML like:

<div id="box1" class="wide"> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris nulla.</p>

Great. But we can’t add borders, margins, or paddings to these boxes because of what the IE box model will do to you. We could apply the well-known Tantek’s box model hack to get around this, but there’s some math to do to make sure everything width-related comes out even — and we’re in a gosh darn hurry.

Simple. Drop in some sleeves.

For each of your containers, drop in another div inside of it:

<div id="box1" class="wide"> <div class="sleeve"> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris nulla. </p>

Ahh. We cringed a little to pop in some extra markup there, but it’s okay. We’re on schedule. Now, just add in a little CSS to finish the job:

div.sleeve { padding: 0px 5px; margin: 0px 5px; border: 1px dotted #999999; }

Our divs are now comfortably bordered and spaced. Nothing broke, wrapped, or otherwise caused us to curse under our breath that it might have been easier to get that physics degree after all. Fabulous.

Sleeves work because they allow you to specify margins, paddings and borders on elements other than the ones on which you specify widths — thereby avoiding box model hacks entirely. Another benefit they give you is the fact you can use any units you like on a sleeve — avoiding potentially painful calculations as you figure out how many ems you have left to make a border in pixels. (Ouch.)

For even greater control, you can target individual sleeves with specific CSS selectors to, say, not have a left-hand margin on box1’s sleeve:

div#box1 div.sleeve { margin-left: 0px; }

You can even use sleeves inside sleeves to create columns that contain columns… but we’ll save that for another example.

This technique has, on more than one occasion, saved me from enduring hours of pixel-tweaking, width-calculating and hacking. It would be nice if we didn’t need to resort to little bits of extra markup to achieve this sort of development speed, and it would be even better if we didn’t even have to worry about things like conflicting box models, but here we are. (And IE7 is coming.)

Here’s a sample HTML file with columns and “sleeves” holding everything together. Let me know in the comments if you find this technique useful, and certainly let me know if you have better ideas or suggestions.

 

Unnecessary weather information at the time of posting

Weather data was not being recorded at this time.
 

 

Comments

ml
According sports jerseys to cheap nfl jersey
nfl football jerseys
football jerseys
cheap nfl jerseys
nfl throwback jerseys
cheap nhl jerseys
discount nhl jersey jerseys the team nhl jersey sales
throwback nba jerseys
throwback jerseys
nba jerseys cheap
new nba jerseysjerseys needs jerseys sale of jerseys biomechanics, nfl can sports wear be nfl jersey divided nfl football jerseys into shock football jerseys absorption wholesale nfl jerseys
china nfl jerseys
2009 nfl jerseys
nfl jerseys cheap
nfl replica jerseyscheap nfl jerseys nfl nfl throwback jerseys shoes,jersey, the stability wholesale nfl jerseys of china nfl jerseys motion 2009 nfl nfl replica jerseys jerseybaseball jerseys
cheap mlb jerseys
authentic mlb jerseys
nba jersey
nba basketball jerseys
basketball jerseys control nhl hockey jerseys Nf jerseyl. Provide shock absorption of nfl , it is cheap nhl jerseys common to have a nfl cheap nhl jerseys jerseys nfl jerseys chea softer discount nhl jersey shoe soles, auxiliary foot dissection in motion even force, help foot nhl jersey sales cushioning.

Posted by cheap jerseys

March 02, 2010 at 5:06pm
 
 

Loading comment form Please wait...