Five CSS tricks that repeatedly saved my a**e, why need more?

Getting a lot of hits from the 53 CSS-Techniques You Couldn’t Live Without post on Smashingmagazine I realised I don’t really need a lot of those (although a lot are cool). Instead let me share with you CSS techniques that saved my a*** whenever I had to create a layout using CSS or proving those wrong that claimed layout tables cannot be replaced.

None of these are new, and many will be the comment that people knew about this, but I can safely say that knowing about the following and some scripting allows me to create almost any of the fancy 53 techniques mentioned.

Tabula Rasa – the global whitespace reset

In October 2004 I found Andrew Krespanis’ global whitespace reset on his still (to me ) beautiful site leftjustified. The idea behind it is so simple, it is ingenious: as every browser out there has an own mind when it comes to rendering styles with a built-in stylesheet, you can overcome all the differences with a simple * selector that undoes everything the browser did. You reset margins and paddings to zero, remove list-type and even set a standard font size. After using global whitespace reset you have a clean slate to start from.

Making the absolute relative

Absolute positioning can be dead handy when you need to align different elements horizontally. You just position one to right and give the other a right padding of the width of the first one, voila – columns. It gets problematic when you need to have other content above the multi column one and you don’t know how high that one will be. The trick is to have both in a parent element and position this element relative – the absolute element will be positioned in relation to this one and not the browser window. Douglas Bowman is the man to thank for this one and that it doesn’t work on MSIE 5.02/Mac is not really causing me any headache.

Containing the float

The problem with the second trick is that if the absolutely positioned column is not the longer one of the two it’ll spill out of the containing element, which is why it is better in a case where you have no clue about the column length to float both of them. The problem when you float them is that the containing element has no height whatsoever. The workaround is to set 100% width on the container and float it to the left, too. Make sure to put a clear:both on the following element on the page

Sliding doors of CSS

Another Bowman trick was Sliding doors or in short using a large background image and clever positioning to allow for expandable navigation elements. As I am not caring much for browsers that should not be supported any longer, I normally tend to use one massive background image and position it in the different elements accordingly as shown in the flexible CSS menu tutorial.

Using background-position to cut down on images and avoid caching issues

Another permutation of this solution is using one background image for several page elements and use dimensions and background-position to only show the relevant part of the image. This technique, christened CSS Sprites (rubbish, they aren’t 24×21 pixels like real sprites) by Dave Shea allows you to cut down load time (as you don’t need to resolve the URL and load lots and lots of images) and avoids nasty flickering on rollovers that MSIE had.

Out of the screen, out of the mind with off-left

Sometimes you need to show and hide content or you need to have content that is only available to visitors that have CSS disabled (the latter should never become a habit, it is a dirty hack and you know it). This is where the off-left technique comes in handy. Instead of using display:none you position the element far off the screen with a negative horizontal and vertical value. Screenreaders will still read them out, but CSS enabled browsers don’t show them at all.

I love CSS, I like most browsers, I just don’t think that we need to overcomplicate matters.

20 Responses to “Five CSS tricks that repeatedly saved my a**e, why need more?”

  1. Five CSS tricks that repeatedly saved my a**e, why need more? « [REF] Says:

    [...] a layout using CSS or proving those wrong that claimed layout tables cannot be replaced. Link [Via Wait Ultil I Come] Entry Filed under: +geek Leave a Comment [...]

  2. Fatih HayrioÄŸlu’nun not defteri » 26 Ocak 2007 Web’den seçme haberler Says:

    [...] isimli makalesini okumanızı tavsiye ederim. Link 5 adet güzel CSS konusu Link Title etiketi hakkında bilinmesi gereken 9 ipucu Link Bir tane daha javascript kütüphanesi Li [...]

  3. Article Feed » Five CSS tricks that repeatedly saved my a**e, why need more? Says:

    [...] . Instead let me share with you CSS techniques that saved my a*** … Original post by Chris and software by Elliott No Comments » No comments yet. RSS fee [...]

  4. Links for the Weekend, 1-27-2006 Says:

    [...] Don’t Want to Make
    ASP.NET AJAX 1.0 Released!
    Firefox Cheat Sheet
    The Big Questions
    Five CSS tricks that repeatedly saved my arse, why need more?
    Book it: del.i [...]

  5. 煎蛋 » 2007 年一月最佳网站æâ€Â¶Ã©â€ºâ€  Says:

    [...] Layout。#sfufoet:下�我就��简�翻译了。直接链接而已。) Five CSS tricks that repeatedly saved my a**e, why need more? Bubble Tooltips CSS support table Good-L [...]

  6. 煎蛋 » 2007 年一月最佳网站æâ€Â¶Ã©â€ºâ€  Says:

    [...] Layout。#sfufoet:下�我就��简�翻译了。直接链接而已。) Five CSS tricks that repeatedly saved my a**e, why need more? Bubble Tooltips CSS support table Good-L [...]

  7. Mark Kirby - Brighton » Blog Archive » links for 2007-02-07 Says:

    [...] gs: CSS) Centering an image Explains 2 ways of centering an image with CSS (tags: css) Five CSS tricks 5 tricks selected from the list of 53 which came out which are all essential to know ( [...]

  8. Web-Sites of the Month: The Best of January 2007 | Smashing Magazine Says:

    [...] asy on the eyes, easy to maintain, and good step in the “standards” direction. Five CSS tricks that repeatedly saved my a**e, why need more? “Instead let me share with you CSS [...]

  9. Good websites that you shouldn’t have missed | TechBlogy Says:

    [...] at’s easy on the eyes, easy to maintain, and good step in the “standards� direction. Five CSS tricks that repeatedly saved my a**e, why need more? “Instead let me share with you CSS tec [...]

  10. dreamer Says:

    Ad “global whitespace reset”: I have run into problems with the * selector when resetting padding, borders and font sizes because they also work on the browser’s default styles on form elements. I have used it, but for some time now dropped in favour of a more specific reset on element selectors (in XHTML there aren’t too many elements used anyway):


    /* Clean slate */

    body, h1, h2, h3, h4, h5, h6, p, blockquote, ul, ol, li, table, tr, th, td, form {
    margin: 0; padding: 0; border: 0; list-style: none; font-size: 100%; font-weight: normal;
    }

  11. Devon Says:

    Great list. I’d add position: fixed, which isn’t really a trick but it’s really useful and seems like a trick to a lot of people because a lot of people don’t even know about it yet (being IE didn’t support it before version 7).

  12. Michel Says:

    Good tips!:-)

    I use 4 of these 5 (everything except “global reset”, I usually don’t need it).

  13. dieter Says:

    Great 5!
    When containing the float, instead of floating the container, I tend to give it value of overflow:hidden, which seems to work too. This has one major downside though: if you want elements to ‘protrude’ from the container, they won’t be visible.

  14. patrick h. lauke Says:

    funnily enough, that’s pretty much the extent of my CSS armoury. it’s rare that i have to resort to anything more complex…

  15. Jim Callender Says:

    totally agree with ‘Making the absolute relative’, a real time saver.

    also theres a great section on ‘using multiple background images’ in the book Transcending CSS - which i wish i’d known sooner!

    understood function for screenreader for off left technique, still yet to find conclusive information on whether off left technique/text indent is good or evil seo.. regards to spamming.. suppose it depends what the nature and amount of text copy..

  16. Rob Says:

    “Out of the screen, out of the mind with off-left”

    This technique does NOT work on Windows Pocket PC Internet Explorer.

    The text is rendered on top of the graphic which is an illegible mess.

  17. Brad Wright Says:

    I feel like the contain floats tip is not as robust as the others. Surely overflow: hidden; and zoom:1; provide more robust layout than floating the same element?

    This avoids worrying about the following element.

  18. JasonM Says:

    Great post … regularly use 3 of 5 CSS tricks.

  19. Adam Messinger Says:

    This is a great shortlist of useful CSS techniques. Another one I can’t do without is Malarkey’s “Z’s not dead” (from the 2005 edition of 24ways.org).

    It’s basically the “relatively absolute” trick, minus float containment, plus a high z-index. I’ve used it on an in-progress site for a veterinary clinic to good effect. Their logo features an abstract cat outline; I have the kitty “sitting” on the navigation toolbar, with its tail drooping lazily over the top of the toolbar. It’s a neat visual flourish, and I didn’t have to resort to image slicing.

  20. css Says:

    Cascading Style Sheets (CSS) web design lessons
    Css link Properties Attributes – examles

    http://css-lessons.ucoz.com/link-css-examples-1.htm
    http://css-lessons.ucoz.com/link-css-examples-2.htm

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).