Try Tuts+ Premium, Get Cash Back!

Are You Making These 10 CSS Mistakes?

Working with CSS can seem like a constant battle. Browsers are always changing they way they read the code (*cough* Internet Explorer *cough*), and it seems that there are lots of tiny little CSS “gotchas”. While it’s an incredibly powerful language, it can easily be used incorrectly, which will doom your development to a lifetime of imperfections.

1. Ignoring Browser Compatibility

This is probably the number one problem with web development, as you have to have layouts that look the same, no matter what browser the site visitor is using to navigate the page. This fact may sometimes seem like the bane of your existence: there are fundamental differences in the way Internet Explorer renders a page, versus Firefox. While it’s not as bad as they used to be, there’s still quite a difference between browsers.

It’s easy for web developers to layout the page in their favorite browser and not worry about how it looks in other major browsers, as you most likely won’t see the differences. (I’m a major offender of this. I’ll sometimes design the site in Firefox and forget about checking in IE until after it’s done!) While there are some tried and true methods to help safeguard your layouts for different browser rendering, the best way to ensure everything looks constant is to just use Browsershots. Browsershots gives an accurate snapshot of what your site looks like across many different platforms and browsers, allowing you to make sure nothing looks funky in a browser.


Photo by lazlo-photo.

2. Not Accounting for Smaller Browser Resolutions

While a good number of us web developers have large computer monitors–and are quite proud of the fact–a good portion of the visitors to your site might not. You can check your analytics programs to see your visitor’s browser resolutions and how widely they range (Google Analytics does this wonderfully). However, there is a world of difference in the way a design looks in an 1024×768 resolution as opposed to a 800×600 resolution. It can make a beautiful design almost useless.

I recently came across this realization as I was tweaking the design for Trendfo. A large chunk of the visitors to the site were using smaller browsers causing an image to partially block some ads, giving me less clicks and ultimately less revenue from the site.

Accounting for smaller browsers means that all of your visitors are happy and finding the information they need.


Photo by Petteri Sulonen.

3. Not Considering Frameworks

If you’re developing a CSS layout from scratch, you might want to ask yourself why. Just as nothing is new under the sun, the same is true with CSS. There are plenty of CSS frameworks out there like Blueprint framework and the 960 CSS Framework. These are created to help you make bulletproof layouts, without having to start something from scratch. These layouts have cross-browser compatibility and have been rigorously tested. Really, unless you’re doing something totally radical that needs pantloads of custom code, just use a CSS framework.

Why reinvent the wheel?


Photo by nestor_galina.

4. Not Utilizing Generic Classes

It can be quite easy to not think about the future when we’re developing websites. Often times we’ll name our CSS classes something different each time we develop a site, as opposed to making a simple CSS class that we can reuse repeatedly throughout our design, without having to dig up what we used before. This will ensure that our site design remains constant throughout the changes of a design.

You might use a generic class like:

(Editor’s note: the double ‘rights’ are the result of a bug in our code viewer, correct code is of course .right{float:right}.)

.right{float:right}

To keep things floating in the right direction when you want them to. So you could style DIV ID like:

I usually use at least three generic classes when I’m building a site design:

	.clear{clear:both}
	.right{float:right}
	.left{float:left}

Photo by cnynfreelancer.

5. Not Validating the HTML

I bet you didn’t know that validating your HTML could also affect your CSS, did you? Well, it can. First and foremost, you can’t validate your CSS until you have valid HTML. Secondly, there are many instances that your HTML could be causing your problems, instead of your CSS. Often times we think that our CSS altering the layout when in fact it’s a bit of malformed HTML that’s making the layout funky. An unclosed DIV here, a mislabeled CSS class… it could be anything. Make sure that your HTML checks out before you even try to diagnose a CSS problem.


Photo by Focal Intent.

6. Not Validating the CSS

I used to constantly bother a friend for help with CSS problems as I tried to wrangle my designs. He’d patiently ask me every time “Does the CSS validate? If not, what are the errors?” It wasn’t long before I learned to validate the CSS before I’d ask Thomas for help later on, and usually validating fixed the problem for me.

If you have valid CSS code, you’re much more likely to have a CSS that is much more compatible across browsers and is less likely to break.

7. Using Mammoth Background Images

Often times new designers will use oversized background images in their layouts. For example, using a 3,000px X 5,000px background image to account for any possible browser size. Instead of using a really big image, they could be using a really tiny repeating image with a touch of CSS magic. The difference is huge: Instead of loading a 200kb image, you could load an image only a few bytes in size and have the CSS repeat it across the background.

If you have a background image that is very large, you’re losing two ways:

  1. It means using unnecessary bandwidth
  2. You’re making the visitor wait longer for the image to load

Sometimes large background images are unavoidable, especially with the recent trend of designing illustrative web layouts. However, using repeating images or solid colors in the background is a much better practice.


Photo by Petteri Sulonen.

8. Using CSS For Everything

When people first learn about a technology, they become excited about it and want to use it everywhere, even when it can actually go against what will work best for the project.

As web developers we can sometimes get caught up in wanting the technology to do something advanced and specific, when in fact we can use a different technology much easier. For example, sometimes it’s much easier to use a table to organize data than it is to make complicated CSS-based layout with floating DIV’s and the like. We have to remember, the reason why we use technologies like CSS is because it should speed up development time. When it starts to slow us down, then maybe we’re going a little overboard.


Photo by Timm Williams.

9. Using Inline CSS

This is a cardinal sin for web developers, and it happens more than you’d like to know. If you’re building a design, you’ll almost always want to keep your CSS and HTML separate. This ensures that when (notice I didn’t say “if”) you decide to change the site design, you won’t have to dig through the HTML of each layout and find the rogue CSS attached to an inline element.

Instead of using this:

link

You should move the style business to an external style sheet like this:

link

Inline CSS should almost always be avoided. It’s easy to use and great for testing, but other than that you probably wouldn’t want it in your production code.


Photo by nestor_galina.

10. Using Too Many Files

Have you ever seen a design with 12 different CSS files attached to it? It’s a nightmare for anyone trying to make changes to your layout. Not only that, it slows down the time processing each file, as the browser has to make a request for every single one. It’s better to use a simple CSS schema that uses 1 or 2 files. The time spent parsing 12 files versus a single file is quite significant. Not only that, you’ll save the next guy who has to make changes on your layout a lot of hassle.

Nobody wants to open 12 files to make a simple, site-wide change!


Photo by lightmatter.
  • Subscribe to the NETTUTS RSS Feed for more daily web development tutorials and articles.

Glen Stansberry is a web developer and blogger who’s struggled more times than he’d wish to admit with CSS. You can read more tips on web development at his blog Web Jackalope.

Liked this post? Vote for it on Digg below. Thanks!


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • BM

    nice. I fond only Virtual PC can give a true indication of IE6. All these standalone browsers are no good.

  • http://creativepayne.com Aaron Payne

    Good info. .. Thanks!

  • Pingback: 10 CSS Fehler » Gif-Bilder.de - Blog

  • http://belinkddesign.com Kim H

    No on frameworks – I’ve used them, and I’ve hated them. As it is, for most layouts I generally fly through the coding. After all – not every design needs to be intricate. Sometimes, a nice two-column layout with a header and a footer is all that a client needs – and wants. I’ve played with CSS frameworks, and frankly I find that I enjoy implementing it myself much better.

    Another no on resolutions – while it is true that a header image should never be larger than 250 pixels to account for smaller resolutions, the general rule of thumb, as I have come to learn, is to design for 1024, as another commenter has pointed out. Of course mammoth resolutions should not be designed for, as office equipment rarely accommodate this and I doubt that users pay the fortune that we do for massive monitors, but I haven’t used a computer that utilized a 600 x 800 resolution since 1995 – over 10 years ago.

    You do, however, get kudos for the points about validation. Even I have been “bad” with forgetting to validate my code. And as far as mammoth images go, it also depends on the image, the site, and the compression level of the image. I’ve seen some sites with large background images that load fine on a slower connection because the rest of the site was written in text. Now, if it is an image-heavy site with a large background image, I would have to protest against this sort of debauchery :)

  • http://tsauri.co.cc tsauri28

    asa kasindiran..heu..

  • http://www.intensetitanium.com Nandini

    It is an useful post for website developers. Hey, Glen. I am pretty impressed. You have done a wonderful job.

  • http://www.fiercetungsten.com/ Tungsten Jewelry

    Hey Glen, you have incorporated great pictures in your post. This has really increased the visual appeal of this post. It is also a good one for website developers. I am sure, similarly like this work, you will rock us with your future projects. All the best.

  • http://www.spmmf.com MMF

    Good info

    Thanks

  • http://www.as7ap4you.com kareem

    this is wonderful tutorial i will put acopy of this lesson on
    my site here
    http://www.as7ap4you.com

  • greg

    Honestly, I think these lists need to stop. Instead of just rehashing the same old CSS to-do or not-to-do lists, how about something a little more useful? Practical? Everyone does their CSS a little bit differently… who cares how they go about doing their styles and stuff as long as it gets the job done?

    And to all of you who say that we should let IE die.. get over yourselves. Ooh, you’re some young hot-shot web developers, but you always run into problems with IE. Well guess what? Most people still use IE. Deal with it. I’m never going to force some grandmom to download some new program that does the same thing the old one does.

    On the same note, people should stop flipping out about how people code their websites. I for one never learned tables, so I don’t really know what the deal is there. But if someone is comfortable making websites with tables, and can make them efficiently that way, who cares? When that same grandmom is visiting a website, she’s not going to look at the source and get pissed about the fact that the code ‘isn’t semantic’. Just make the website look good and usable. That’s all that matters.

  • http://www.greenarch.co.uk Nandini@HIPs Consultant

    Glen, Your selection of images is really excellent. Keep up the good work.

  • Joe

    Good article, but I believe frameworks are useless. A thorough knowledge of CSS and browser compatibility will lead to better layouts and quicker rendering of code. Frameworks introduce extraneous elements (eg. many websites never utilize the h5 tag).

  • http://www.playpharma.com/ Nandini

    Hi, Glen. Your post is really an impressive one. The pictures you have used in this post are also great. Keep up the good work man. All the best.

  • Pingback: ¿Estás cometiendo estos 10 errores en CSS?

  • N a m e

    nice pictures

  • Pingback: 31 Incredible Resources And Inspirations For Designers Like You To Discover The Best Of The Web In November - Opensource, Free and Useful Online Resources for Designers and Developers

  • Pingback: MyInkTrail: Best of the Web, November 2008 | My Ink Blog

  • Pingback: 31 Incredible Resources And Inspirations For Designers Like You To Discover The Best Of The Web In November | virology.tv

  • Lars

    Dear Glen,

    Thank you for this nice list of images…
    But I think there’s a bit too much text in between.

  • cman

    Here is another “do it my way css” article..
    You know if you going to preach then validate this page.

  • http://www.dystopiapop.com Dan Black

    Great article. It is only in the last point [using only one or two CSS files] that my opinion varies slightly. I typically have four CSS files: reset, layout, type, graphics. Each is separated for modularity.

  • http://www.dystopiapop.com Dan Black

    Also it makes it easy to copy parts from old projects and reuse them on new projects.

  • http://dmiessler.com Daniel Miessler

    What tool are you using for your syntax displaying? It’s very slick.

  • http://joel-llamaduck.blogspot.com Joel

    The photos distract from the article. One or two was okay, but then it just got old.

    But those are some way good tips. Thanks!

  • homer

    CSS frameworks are anti-semantic. And it will make your css bloated with unnecessary excess codes. Worst is, if you look at Blueprint code, they suck at inheritance. Too much inheritance would somehow slow your rendering time. Bad idea!

    I do not entirely disagree with generic classes but should only be used if possible for main layouts. All contents should stay away from it. The reason?, separation of content from presentation.

  • http://www.maycash.com/ Juni

    Hey Glen, great work. I am impressed. I must say your selection of pictures is excellent. Keep it up.

  • Pingback: Del.icio.us op 3 december 2008 | Michel Vuijlsteke's weblog

  • Pingback: Best Resources Of The Web Christmas 2008 | Design Development Magazine

  • artd

    about ietester or multple ie’s: The behavior is not exactly like a real ie6, so I prefer virtual machines, one with ie6 and one with ie7.

    css frameworks are ok but you really have to understand them so hard as you start to think its your own.

  • Pingback: 31 Incredible Resources And Inspirations For Designers Like You To Discover The Best Of The Web In November | SulVision

  • Pingback: 20 Ultimate CSS Tutorials That Will Help You Master CSS | DWSmg.com

  • Pingback: JadeArrow.Com » Blog Archive » 18 Incredible Resources And Inspirations For Designers

  • Pingback: 10 conseils en CSS | Romain Luyten

  • Pingback: CSSBlog ES - ¡CSS y nada más!

  • Pingback: alceste’s blog » Blog Archive » About Browser Compatibility

  • Pingback: Web Administration devBlog - Favorite Links from November

  • Pingback: Are You Making These 10 CSS Mistakes? - NETTUTS | Best Web Gallry

  • http://webusabilityhelp.blogspot.com sam

    This is great! Thanks so much. Hope it will continue.

    you can get interesting post about web usability and technology on my Web Usability Blog.

  • Jono

    Very interesting and very true at times… but most of the time it wont cause what you showed in those pictures, but some of the time it does.

  • david.milewski

    Nice article ;)

  • Pingback: Top ten CSS articles

  • Pingback: Hojas de referencia css | Uy perdón

  • http://www.enfold.in Shubhodeep Ghosh

    Very nice article. :)

  • Pingback: First Steps from Print to Web Designer « Sean Camden

  • http://www.kaplang.com/blog Michelle

    amazing article, thank you :)

  • http://flax.ie/ Ciarán

    Very nice article I have to say I am guilty of sometimes using Inline CSS and not reusing classes. Just makes more work in the long run, have to get out of the habit.

  • http://www.artikelverzeichnis24.net Michael

    thank you for this nice list of images

  • Pingback: lindanisbett.com » Blog Archive » Common Mistakes Made By New CSS Users

  • http://www.boingboing.net/2011/05/06/nyt-magazine-on-the.html#comments Kelvin Colletti

    I actually liked the submit and terrific tips..even I also think that tough perform will be the most crucial feature of acquiring achievement maintain it up :)