I am one of the biggest advocates for CSS. That being said, I’ve always been disappointed in it’s layout capabilities. There looks to be some promising new features in CSS3. However, being that we’re just starting to think about bailing on IE6, it’s going to be quite a while before we can even start to rely some browsers *ehem IE ehem* to support CSS3 fully. That being said, I’d like to introduce a technique for a column divider that extends to the longest of the two columns (and is purely CSS driven).
How many times have you built a two column layout? How many times have you found it damn near impossible to get them to line up at the bottom without using some Javascript? It’s a crappy situation to be in. “Rely on Javascript, do without..?”
In certain situations, you don’t have to decide. Assuming both columns are above the same background, and all that is needed between them is a one-pixel divider, then we’ve got the ticket. The general dilemma with this sort of layout is that you really only get to choose one side to have the divider on. Say you have a basic “Content/Sidebar” layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <html> <head> <style> #wrap { width: 800px; padding: 10px; border: 1px solid black; background: lightyellow; } #main { width: 600px; float: right; } #secondary { width: 200px; float: left; } .clear { clear: both; } </style> </head> <body> <div id=“wrap”> <h1>Site Header</h1> <div id=“main”> <h2>Page Header</h2> <p>Lorem ipsum dolor sit amet…</p> <p>Pellentesque scelerisque…</p> <p>Nunc quam eros, vulputate…</p> <p>Curabitur malesuada augue…</p> <p>Integer vitae tellus non…</p> </div> <div id=“secondary”> <h3>Navigation</h3> <ul> <li><a href=”#”>Home</a></li> <li><a href=”#”>Page #2</a></li> <li><a href=”#”>Page #3</a></li> <li><a href=”#”>Page #4</a></li> </ul> </div> </div> </body> </html> |
If you add the divider to the left column, then you chance the content column being longer than the divider. If you put the divider to the right column, you chance it being shorter than the left column, etc. It’s just a big mess.
The solution? Add the divider to both columns.
1 2 3 4 5 6 7 8 9 10 11 12 | #main { width: 599px; float: right; border-left: 1px solid black; } #secondary { width: 199px; float: left; border-right: 1px solid black; margin-right: –1px; } |
If you noticed, we pretty much did the same thing to both of the columns. We added the dividing line on the sides that touched, as well as compensated the 1px border in the width. The only thing that we did differently was added a negative margin of one pixel to the secondary column on the joining side. This method basically overlaps both columns by one pixel. Luckily, both of these columns share a one pixel border on that side, causing it too look like there’s only one border between them.
You can see how finding this has saved me tons of grief, and time searching for the best column Javascript code..
Hope this helped somebody, as it was a huge help to me!