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.

Thanks said,

May 24, 2008 @ 1:41 pm

Great tips! Here’s one for you: it’s “ado” not “adieu,” as in “Much Ado about Nothing.” “Adieu” is French for “Good Bye.” “Ado” is English for “fuss, delay.”

Steve said,

May 24, 2008 @ 4:14 pm

Id love to see a video of you using FB and WDT together. The site I maintain was built for me using adobe contribute and is riddled with tables and nested tables. I’d love to redesign using nothing but css, but have troubles with margins, and columns alike. Any more tips you have would be greatly appreciated.

Jad said,

May 25, 2008 @ 2:40 am

I am using firefox beta 3 :( and Firebug is for an older version …

Kezzer said,

May 29, 2008 @ 5:23 pm

A few comments:

1: Why? You don’t say why, you just say to do it, there’s no justification there. This is actually an issue to do with IE more-so than anything.

3: Obsessing about validation IS important, but I think you’re interpreting it with the wrong meaning. Validation nazi’s shouldn’t do it because it’s the “right thing to do”, they do it because standardisation of code, as any developer will know, is important for understandability, not just to meet validation so they can sit on their pedestal gloating about their web standards. Not meeting validation is like saying “we don’t really care about not only users, but maintenance teams either”.

As for the use of * in your CSS stylesheets, this is actually called CSS reset, something which Eric Meyer has been heavily involved with for years, and a great number of individuals throughout the world do this, expect on a far more practical scale.

Markus said,

May 29, 2008 @ 7:24 pm

@Kezzer. Yes #1 is mostly about avoiding problems with IE6.

#3 has nothing to do with maintenance, so I don’t really understand your rant.

I never claimed to have invented the CSS reset, so I’m not sure what your issue is.

Ash said,

June 3, 2008 @ 8:06 am

You are so right about number 3, at the the end of the day it’s about getting things done.

But I can understand that websites that may be maintained by groups of people may require clean code, but hey what the hell!

:)

Lindsey said,

June 8, 2008 @ 6:25 pm

I disagree with #1. Then in your CSS if you have a div and within it several p tags, images, other divs, etc – you have to continuously add padding/margins to each one of these things. Just add the padding margin to the first div, and if IE is giving problems fix the margin/padding with an IE conditional comment.

Also, to add for #2 one of my favorite tools to use in combination with Web Developer Toolbar and Firebug is Aardvark.

As for #3 I’m not that bad of a code nazi, but I definitely wouldn’t say “don’t worry about validation”. Validating code is one of the easiest ways to find out WHY its breaking. I’m not against empty divs or code that isn’t 100% semantic – but I’m a big proponent of validating code. Less chances of it breaking for cross browser compatibility and a good way to find out what your doing wrong with the code.

@Martin – totally agreed on all points – it’s definitely not hard to have good, valid code. If you understand what you are doing there shouldn’t be all that much of a fuss to validate and correct (mostly) IE6’s crappy CSS issues.

elvis said,

June 12, 2008 @ 12:43 am

Could you give more details example including html for the first tip please?
Thank you for your sharing.

Brad Richardson said,

June 17, 2008 @ 1:24 pm

I am a designer out of South Carolina, and have been coding websites with CSS for the past 3 years. i love the way that css handles layout, but have shot myself in the foot several times before learning a few tricks.

1. You CAN put padding on a div when you declare its width. Just know that you have to nest another div on the inside of that one to stay away from strange errors. In other words, if you declare a div to be 220px wide and put a padding on it like this, 0px 10px 0px 10px;, then know that the div is actually 240px wide. Simply put that div inside of a div that is 240px wide, and then put another div inside of the one with padding that is 220px. It sounds like a lot of nesting (and coding) but if you know how to write CSS, you just put all three declarations together and mark the changes below.

The gentleman is right about never using margin. Stay away from it. No browser treats margins the same way.

2. Learn the Forgotten Four. When nesting divs you need to include these four declarations: float, clear, overflow, display. You float to whatever side that you would like to be on. You clear to the same side that you are lined up on unless you want divs side by side. in that case, only put the clear declaration in the first div. Overflow controls how content acts inside of the div. Generally speaking you want to use hidden. But if you are considering using ToolTips, then you will need to put visible, it allows the tooltip to “break” the barriers of the div, without realigning content. Finally, display controls how a div reacts to the other divs around it. display: block; allows divs to rest one on top of the other. Normally those divs have a clear declaration. display: inline; will allow you to have divs side by side.

Email me if you have any questions.

Markus said,

June 17, 2008 @ 4:34 pm

Thanks for writing up that explanation, Brad.

Shawn Adrian said,

June 21, 2008 @ 2:51 am

Cool man, nice post. I swear by firebug… Interesting about the not adding margins / padding on divs. I often do that, but never thought about how easy it made things.

Jenny said,

June 22, 2008 @ 4:40 pm

I don’t obsess over CSS validation but I try to get it right with the xhtml. I don’t know. I have a friend that got me into that and I haven’t been able to stop since. xD

Liam said,

June 25, 2008 @ 1:55 am

I just take the padding off of the div width, this is an easier option as I don’t have to add padding to every element in the div now.

Drew said,

June 25, 2008 @ 6:18 am

Adhering to web standards is extremely important, its not a waste of time validating your code, it should be a top priority of all serious designers and developers. If more people spent more time validating their code, the internet would be better off.

Just my take.

Regards,

Drew

Jake said,

July 14, 2008 @ 5:28 pm

Good tips.

Here is my tip: check out the Blueprint CSS framework. It resets the CSS so that pages look the same in all browsers. It also enables you to build pages faster and update them easier. I’ve been using it for 9 months and it has saved me hours already.

http://code.google.com/p/blueprintcss/

Zeljko Prsa said,

July 15, 2008 @ 1:20 pm

@Liam “I just take the padding off of the div width, this is an easier option as I don’t have to add padding to every element in the div now.”

Right on Liam, that’s what I do and never spend a minute more on IE6 fixing.

Marjan Davorin said,

August 25, 2008 @ 7:21 am

Nice tips, but as most of the development time is spent for debugging IE, you should probably follow the tips in this article: http://scarfoo.com/archives/16

Hosting said,

August 29, 2008 @ 12:51 pm

but as most of the development time is spent for debugging IE, you should probably follow the tips

festival said,

August 29, 2008 @ 12:59 pm

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

Schuhe said,

August 29, 2008 @ 1:01 pm

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

Markus said,

August 29, 2008 @ 3:28 pm

@Schuhe: you could, but it’s not freeform typing, so I prefer to use both in conjunction.

Film izle said,

September 1, 2008 @ 6:02 am

You are so right about number 3, at the the end of the day it’s about getting things done.

Takasel said,

September 6, 2008 @ 10:09 am

I am using firefox beta 3 :( and Firebug is for an older version …

sağlık said,

September 27, 2008 @ 4:33 pm

but as most of the development time is spent for debugging IE, you should probably follow the tips

partner said,

September 29, 2008 @ 11:57 am

but as most of the development time is spent for debugging IE, you should probably follow the tips
———-
I am using firefox beta 3 :( and Firebug is for an older version …

araba oyunları said,

October 25, 2008 @ 12:06 pm

I am using firefox beta 3 :( and Firebug is for an older version …

dizi izle said,

October 28, 2008 @ 5:43 pm

Nice tips.

its my tip: check out the Blueprint CSS framework. It resets the CSS so that pages look the same in all browsers. It also enables you to build pages faster and update them easier. I’ve been using it for 9 months and it has saved me hours already.

sağlık said,

October 29, 2008 @ 2:50 pm

I just take the padding off of the div width, this is an easier option as I don’t have to add padding to every element in the div now.

Online Film İzle said,

November 2, 2008 @ 3:38 am

@Liam “I just take the padding off of the div width, this is an easier option as I don’t have to add padding to every element in the div now.”

Right on Liam, that’s what I do and never spend a minute more on IE6 fixing.

Karadeniz Haber said,

November 2, 2008 @ 2:31 pm

“Don’t obsess about validation or having perfect code” Its difficult to say so:)

Suleyman Hilmi Tunahan said,

December 22, 2008 @ 11:58 pm

Cool man, nice post. I swear by firebug… Interesting about the not adding margins / padding on divs. I often do that, but never thought about how easy it made things.

Evcil Market said,

December 31, 2008 @ 4:27 am

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

Elazığ Haber said,

January 13, 2009 @ 6:06 am

Cool man, nice post. I swear by firebug… Interesting about the not adding margins / padding on divs. I often do that, but never thought about how easy it made things.

vin said,

February 17, 2009 @ 12:58 am

yep agreed with you about that perfect code thingy. just finished making a new design and its a real pain trying fix those annoying bugs.

ediziizle said,

March 4, 2009 @ 4:37 am

I just take the padding off of the div width, this is an easier option as I don’t have to add padding to every element in the div now.
thanks

sohbet said,

March 9, 2009 @ 8:45 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 :)

diyet said,

March 17, 2009 @ 11:35 am

Adhering to web standards is extremely important, its not a waste of time validating your code, it should be a top priority of all serious designers and developers. If more people spent more time validating their code, the internet would be better off.

site ekle said,

March 23, 2009 @ 7:58 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.

giydirme said,

April 3, 2009 @ 1:19 pm

Adhering to web standards is extremely important, its not a waste of time validating your code, it should be a top priority of all serious designers and developers. If more people spent more time validating their code, the internet would be better off.

cleanvac said,

April 22, 2009 @ 5:42 am

It resets the CSS so that pages look the same in all browsers. It also enables you to build pages faster and update them easier.
its for evrything tenks
bye

firma rehberi said,

April 23, 2009 @ 8:33 am

I don’t obsess over CSS validation but I try to get it right with the xhtml. I don’t know. I have a friend that got me into that and I haven’t been able to stop since. xD

Andreas said,

May 8, 2009 @ 10:53 am

I’m not really obsessed with the CSS validation, because it looks good everywhere, it gotta be good, but the (x)html has to be right because that too can save a lot of shit