common css mistakes/putting a stop to them

published on:
June 17, 2010
comments

In the margins of the html5 and css3 rise one can find a whole lot of unpleasantness. Take your eyes away from the dazzling new features for just a couple of seconds and you'll be overwhelmed by a penetrating stench coming from underneath your browser's canvas. But that's not even the worst of it, take some time to look around and notice how badly written css is still very prevalent on the web. Time to get rid of that ugliness first.

It's obvious there still isn't a very good base for css best practices. Newcomers to css continue to make the same mistakes we made 10 years ago and with people flocking to html5 and css3 this group is growing fast. It's a sad state of affairs that needs dealing with, so even though I'm not much of a fan of haphazardly thrown together lists here's my top list of css ugliness I never want to see again.

1. stop unnecessary floating

.selector {float:left; width:100%;}

The code above is something you find quite often. A simple block level element that is floated with a width of 100% to make it span the complete width of its parent. In most cases, removing the float and width will result in exactly the same rendering, only with more flexible use of margins and paddings. So what gives?

What usually happens is people fail to understand the workings of collapsing margins. Floating the element triggers a more understandable use case where margins are not collapsing with margins of surrounding elements. This is okay until you need to add paddings, margins or extra positioning rules. Furthermore, this code isn't very browser-proof and you'll often end up with nasty ie6 troubles.

There are better ways to work with (or around) collapsing margins and besides, floats are never a good way to fix things. Using floats to fake a block level element is completely useless and will definitely result in trouble later on. So no more.

2. margins and paddings

.parent {margin:1em 0.5em 0em 0.5em; padding:0em 0.5em 1em 0.5em;}

Margins and paddings should never be used together to create one single space. Both properties can be placed on leafs (an html element with no children) in certain circumstances, but apart from this use case it's better to stick with either one of the two to define all spacing around an element.

Usually statements like these come forth of laziness or bad planning. They are the result of last minute changes or unfocused code weaving. All understandable excuses, but if you claim to have a professional attitude you just can't leave them in your code.

I don't really care whether you prefer, paddings or margins, but combining them to create one single space is never a good idea. So no more.

3. width abuse

.parent {width:50em;} .parent .child {width:49em;}

Use widths only when really necessary. People tend to go all math on their css while css is (somewhat) equipped to do most of the work for you. In the example above, the width declaration on the .child is completely unnecessary. It would be much better to simply add a right margin on the .child element (unless you'd really want the child to be 49em wide independent of the width of its parent). Using paddings or margins instead will ensure decent rendering even when the parent would change widths.

I've had several projects where the goal was to widen a site. It's true bliss if you can do this by adapting just a couple of values in the css without worrying to much about browser inconsistencies. A project like that turns into hell on earth when each component has a specific width defined, especially when these components just take up the available width anyway. It really makes the difference between an hour work or two days of hard labor.

For positioning an element relative to its parent, use margins or paddings. Only use widths when an element should explicitly have a fixed dimension (not saying it should always be in px though, it could be ems just as well). Widths are for reserving space, not for spacing. So no more.

4. mending and fixing

.selector {margin-left:-6px;}

There aren't too many things the web design community as a whole agrees on, but when it comes to ie6 most (if not all) people agree that it is an outdated, hard to code for piece of software junk. And while I won't contest that statement, it must be said that most of ie6's problems can be fixed quite consistently. Whether you use inline hacks or conditional comments doesn't even matter, how you actually go about fixing these issues is more important.

Many of ie6's bugs exist of (little) misplacements of elements. Most people try to fix these issues by re-aligning them specifically for ie6. For example, if an extra 6px space is added to the left you can counter it by defining a negative margin of the same size. This is crap coding though, as you're only just mending the problem rather than really fixing it. ie6 might be a true bitch, but most of its bugs can be countered as to make it behave like other browsers.

This is pretty important as these fixes will continue to work when the original css is adapted. Rather than defining a negative margin, make sure ie6 renders the item as it should, so future adaptations won't need extra fixing. Making ie6 behave is not easy, but far from impossible. So no more.

5. reset without rereset

* {margin:0; padding:0;}

I have nothing against reset css, be it coming from a standard script or coming from custom coding. As long as you make sure to set the general styles back, I'm fine with it. I do get furious when encountering a css where simple text paragraphs don't even have a margin (or padding) defined on them. This is simply not done and extremely sloppy coding.

So use these reset css styles as much as you like, just make sure to set the basic styles again. Make sure your paragraphs look like paragraphs and lists feature some kind of list element indications. These quick resets eliminate styles that are important for displaying general data, not setting them back to a good standard is pure amateurism.

conclusion

Basic as these five remarks might be, you'd be surprised how many front-end developers still make mistakes like these. There are many reasons that seem to fuel this state of affairs, like lack of proper schooling or a good fall-back place for css best practices to go to when starting out with css. Most people learn from scattered blogs and snippets found in articles, which is probably not the best way to get to the bottom of what we do. This makes finding a good solution a pretty tough task though.

The list above is more than just my top 5 of commonly seen css ugliness, it's hopefully a wake-up call to whoever feels the passion and energy to create some kind of starter's platform for css best practices. Through all the bling and sex of css3 these issues are once again eclipsed, but they might be even more relevant than two years ago with a whole new group of people starting to learn the tricks and trades of html and css.

So no more, it's time to take css to its next level. And no, I'm not talking modular css or advanced animation, just a good set of best practices and a well considered starters platform for people to get started.

Comments

Mathias

comment number
date
June 17, 2010 13:03

I'd like to add number 6: 0em = 0px = 0ex = 0in = 0cm = 0mm = 0pt = 0pc = 0 So do yourself a favor and just use 0.

Mathias

comment number
date
June 17, 2010 13:04

Hey Niels, why are you forcing display: block on <code> elements in your comments? Isn't that what <pre><code></code></pre> is for? ;)

Niels Matthijs

comment number
date
June 17, 2010 13:32

As for simply writing 0, I'm actually in favor of stating the measure. It gives a little guidance and makes such values a little easier to adapt afterwards. Either way, both options are never harmful so I don't really see it as a big issue

Schmoo

comment number
date
June 17, 2010 14:34

@#2: Separation?

jamEs

comment number
date
June 17, 2010 15:15

I find not learning to CSS shorthand to be a big one for myself. I see code daily that defines 3 or 4 background rules that could be ganged up into 1 declaration. Or declaring margins like margin:10px 10px 10px 10px; Just seems lazy to be employed coding CSS and not actually take the time to learn to make your code more efficient.

John Kary

comment number
date
June 17, 2010 16:10

Margin/Padding are perfectly acceptable to use together if you know how each functions and when to use it. I think its misuse comes from beginners not knowing how each affects the element.

Proper use cases for margin + padding: 1. I want an image with a 1px border, but want to give the photo a 5px matte (padding:5px) and give it separation from my text content (margin:1em).

  1. Creating buttons by styling an <a> tag. Padding increases the hitbox for activating the link, while margin lets me move it around relative to other content.

I would add these to the list:

  1. Mathias' comment on using 0 = 0px. You don't need a unit!

  2. Using long-form property declarations when shorthand will suffice. margin:0px 0px 0px 0px; when margin:0; will suffice. Or worse, margin-top:0px; margin-right:0px; etc...

  3. Using too many effects or different styles, just because you can. This is just in-practice, rather than a coding mistake, but trumps all syntactical errors combined. "Just because you can, doesn't mean you should." There are so many advanced-beginner tutorial sites that are good at showing HOW to do something, but rarely suggest WHEN NOT to use it. High impact, low investment, but in a bad way.

Niels Matthijs

comment number
date
June 17, 2010 17:08

John: your example (image with border) is covered by my leaf exception. Also, the border you speak of (or possibly a background color/image) makes it an example where you can't speak of "one single space". Same goes for the "link as button" example. I'm specifically talking about creating one single white space using both margin and padding properties.

As for shorthands, I wrote about that some time ago. I'm not convinced using the shortest option is always best.

Paul

comment number
date
June 18, 2010 16:50

0 is a unit of nothing. When I see 0px or 0em, I cringe and instantly think "amateur".

Julio Sepia

comment number
date
June 20, 2010 06:48

Refusing to assign margin and padding to the same element leads to "DIVitis", because you end up creating additional containers, one for the margin, one for the padding. Divitis is a common mistake too, and one I'm guilty of.

Niels Matthijs

comment number
date
June 20, 2010 10:31

Divitis is only an issue if the added divs are without any structural relevance or without any possible semantic meaning. These cases are quite rare.

I've never had to add any extra structural elements just for the sake of applying margins/paddings.

Sunny Singh

comment number
date
June 20, 2010 23:29

I understand that many still support IE6, but it's 2010 and really should not be brought as so much, especially to prove reasoning. Even Google is quickly loosing support for IE6 in more of their services.

I completely agree with most of these points though and try to make my code perfect, but not for the sake of proper IE6 rendering as I've dropped that browser a long time ago. I'm even showing a message for IE7 users to upgrade, since a lot of errors are showing up in that browser now too.

The margin: 0; makes more sense to whoever brought that up though. It's really redundant adding a value since it's frickin' nothing.

Maicon Sobczak

comment number
date
June 21, 2010 01:18

Important observations. Mainly about rereset the style.

Eric

comment number
date
June 21, 2010 02:16

Pretty good post.. There are always exceptions to some of these examples (as you mentioned here and there) but generally you are spot on with most of this.

Stephen

comment number
date
June 21, 2010 03:15

margin should always be used instead of padding when both are available due to IE's box model

margin and padding can be both used when there is a background (in which case, you might also need an IE stylesheet if you also set a width)

Toemouse

comment number
date
June 21, 2010 03:21

You are a noob. Keep on writing articles like this for the sake of it!

Missy

comment number
date
June 21, 2010 15:59

I can't agree on the comments made about not supporting IE6. In a commercial environment it is critical to remember that a customer of an ecommerce site MAY HAVE NO CHOICE OVER THEIR BROWSER if they are accessing (say) during working hours.

It is one thing to suggest to users of a blog or portfolio that they upgrade; to suggest to a paying customer that they upgrade is likely to result in loss of business. If I were to suggest to my manager that we drop support for IE6, I would probably be fired. Around 14% of our customers are still using IE6.

Using reliable statistics software should highlight which browsers your customers are using (through choice or not) and which, as a business, must continue to be supported. Reviewing browser usage every 3-6 months (along with your coding and CSS practices) is the way forward.

Melo

comment number
date
June 21, 2010 18:00

I want to thanks to everybody who give the best of their knowledge. I'm a beginner and I use this site for learn.

Krzysztof Kotlarski

comment number
date
June 23, 2010 07:17

Why margin instead padding? IE somtimes forgets about margin-bottom or margin-right, and doublesmargin when combined with float.

* required fields

Leave your data
Leave a comment