AU Interactive

3 CSS Coding Tips That Will Save You Hundreds of Wasted Hours

CSS example codeI got into CSS-based design sometime around 2002-2003 and have wasted literally hundreds of hours of my life dealing with various CSS and browser-compatibility issues. Back then we had far fewer tools at our disposal so the process of testing was a lot more tedious. Over the past 5 years I’ve picked up some tips and tricks that have made my job 100 times easier.

So without further adieu I’d like to share some of these tips:

  1. On any given div, if you specify a width, do NOT specify padding or margins (and vice versa). Specify padding and margins on the elements contained in that div instead. Ex: leftcolumn {width:200px} lefcolumn p {padding:10px;width:20px;}
  2. Use Firebug and Web Developer Toolbar together. In Firebug, use the inspect button to see element names, styles, and inheritances. Then use Wed Developer’s CSS > Edit CSS panel to change the properties in real time. If anyone wants I can probably make a video screencast and show you some examples of what I mean.
  3. Don’t obsess about validation or having perfect code. I know some coding Nazis might hang me for this one, but if you need to stick a clearing div here or use a IE6 hack for a specific issue there, do it. I used to have CSS OCD once too, it’s ok. But if I can save myself an hour of tearing my hair out trying to fix one small issue just in the name of perfect semantic markup - it’s not worth it (especially if your users will never notice or know about it). Life is short. Don’t spend it obsessing about CSS.

If I had known these things earlier, I would probably have saved a lot of hours - literally hundreds - over the years. Those are probably my top 3 that have made the biggest difference in my coding career. Bonus: I also like using * {padding:0;margin:0} at the top of my CSS sheets to clear everything out, then specify margins and padding explicitly for divs and classes. Bonus #2: Faux columns can also be a lifesaver.

For more helpful tips, check out these posts:

Feel free to share any of your own time saving CSS tips in the comments.

FredAtMicrosoft said,

April 21, 2008 @ 2:44 pm

Great tips. I know I’ve been guilty of abusing div attributes before, and then spending hours scratching my head as to why my tables weren’t lining up correctly.

——————————————————
Fred Reckling
Microsoft Security Outreach Team
http://www.microsoft.com/hellosecureworld/level7

Pete W said,

April 22, 2008 @ 10:50 am

Why not just change the CSS/code etc in Firebug?

Markus said,

April 22, 2008 @ 11:05 am

@Pete: freeform typing is a lot easier in WDT, firebug restricts you more or makes you switch into “Edit HTML…” mode and confines you just to that section. So using both for their specific functions is a lot easier for me.

Dallas Web Design said,

April 23, 2008 @ 7:39 am

Thanks for the info. Padding and margins seemingly always throw off the designs I work on. Do you tpyically create specific styles for each div you work in? (paragraph formatting for each div if different)

Markus said,

April 23, 2008 @ 8:49 am

@Dallas: Yes, for the main layout divs. Otherwise I specify styles only when they’re necessary (or try to anyway).

Ryan said,

April 28, 2008 @ 11:01 am

A note about your padding/margin issue with div’s. Something I usually do is use the padding/margin of a nested p tag to achieve the same result.

Markup:

CSS:

div.wrapper { margin: 0; padding: 0; }
div.wrapper p { margin: 10px; padding: 10px; }

edie said,

May 11, 2008 @ 12:01 am

when I have an I.E. goof, I just use a conditional comment that fixes the problem. I learned about conditional comments here: http://www.cssplay.co.uk/menu/conditional.html

My code is valid and works in I.E.

Martin said,

May 11, 2008 @ 8:12 am

Well, concerning the perfect code issue, it is not that hard to make your code valid according to the specification. Just use separate stylesheet for IE6 and the problem is solved.
Since modern browsers interpret width of DIV elements correctly, it is not necessary to avoid setting padding or margin (IE6 use separate stylesheet).
Firebug is a life-saver :)

daniel said,

May 11, 2008 @ 10:46 pm

The Web Developers Toolbar is a great tool and will definately make your lie as a CSS Developer easier

isparkle said,

May 11, 2008 @ 11:09 pm

cool! especially the first item.

Leave a Comment