24

What are some of the most common mistakes made by CSS-Designers?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Andrew Siemer
  • 10,166
  • 3
  • 41
  • 61

24 Answers24

24

Not realizing till a year into it (like me), that you can apply more than 1 class at a time.

.Center {text-align:center}
.Disco {background: red; text-decoration: blink;}
.Highlight { font-weight: bolder;}

    <div class="Disco Center Highlight"></div>

Is valid and it will combine them all.

JBrooks
  • 9,901
  • 2
  • 28
  • 32
  • 13
    On a related note, are you aware you can create a selector that looks for two or more classes using div.Disco.Center { }? – Joel Jul 27 '09 at 22:43
  • 6
    what happens if you want to change your site design and you no longer want that element centered? now you have "disco center highlight" left aligned! hmmmm... – Jason Jul 27 '09 at 23:19
  • @Joel: I know about using two classes. I did not know about the dual selector. That's cool. – NotMe Jul 28 '09 at 14:51
  • now forget about dual selectors again, because there is always an Inevitable Exception among browsers – n1313 Sep 01 '09 at 14:41
  • “are you aware you can create a selector that looks for two or more classes using `div.Disco.Center`” — doesn’t work in IE 6 though. – Paul D. Waite Feb 18 '10 at 04:10
21

Not using a reset file.

"The goal of a reset stylesheet is to reduce browser inconsistencies in things like default line heights, margins and font sizes of headings, and so on."

- Eric Meyer, Meyerweb.com

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 1
    Agree, resetting standard rendering should be the first step. Saves headaches later. – Grant Palin Jul 27 '09 at 22:18
  • 6
    It's a good idea, but I think it resets too many things. – staticsan Jul 27 '09 at 22:47
  • 1
    @Staticsan, modify the reset to accommodate your needs. My suggestion isn't meant to suggest only Meyer's reset. If you have a smaller, less-involved, stylesheet - use it :) – Sampson Jul 27 '09 at 23:52
  • 3
    I prefer a "set" to a "reset". It's pointless to zero all margins and padding and then add them back again later. There aren't a huge amount of elements that *need* their margins reset (e.g. `div`, `form`, `table`). Often the defaults are fine anyway and browsers mostly adhere to the defaults at http://www.w3.org/TR/CSS2/sample.html – DisgruntledGoat Jul 27 '09 at 23:59
  • 1
    @DisgruntledGoat - Meyers' "reset" doesn't set all margins and padding to zero. You must be thinking of somebody else's reset... – Sampson Jul 28 '09 at 01:07
  • 1
    Reset stylesheets are overrated. I'll concede they have value in projects where it's crucial to maintain absolute consistency between browsers. Having to redefine all those styles you just reset tedious and usually unnecessary. Common mistake to avoid? Hardly. – Tate Johnson Jul 28 '09 at 02:12
  • @Tate, Every project is "crucial to maintain absolute consistency between browsers" to somebody who takes their job serious. I don't get paid to do "good 'nuff" work. – Sampson Jul 28 '09 at 02:23
  • @Jonathon: uh, yes he does - look at the page linked. It's not using the * selector, but zero margins and padding are specified for practically every elements, including html, div, span, and a ton of inline elements which never have those by default. – DisgruntledGoat Jul 30 '09 at 11:34
  • @DisgruntledGoat, I'd welcome you to actually look at the stylesheet. Not *all* elements are 0 on their padding/margins. I know this from application - I use the reset. – Sampson Jul 30 '09 at 11:42
  • Don't be so pedantic ;) - no, it's not *every single* element, hence my qualifier "practically". Anyway my two points were that (1) it's pointless to zero *anything* then add values back a few lines later - just set an appropriate default value to start with; and (2) many of the elements simply don't need to be reset - no browser sets margins on the `div` element or removes bullet points by default. – DisgruntledGoat Jul 31 '09 at 16:09
  • 1
    If I could vote this one up more than once, I would. – Martin Jul 31 '09 at 19:14
  • @Staticsan: I agree. Does anybody know of any reset styles, that do not break the presentation of the h1+, sup, and sub elements (for example)? – olive Jul 31 '09 at 19:24
  • “Does anybody know of any reset styles, that do not break the presentation of the h1+, sup, and sub elements (for example)?” — It’s not exactly hard to write your own. Use Eric’s as a starting point, and amend to your needs. – Paul D. Waite Feb 18 '10 at 04:09
21

Thinking that:

<div class="topMargin15"></div>

with

.topMargin15 {
    margin-top: 15px;
}

Is somehow and improvement over writing it directly into the style attribute. You are supposed to be defining what it is in the HTML, and what it looks like in the CSS.

TM.
  • 108,298
  • 33
  • 122
  • 127
  • 1
    Yep, way too many times I've seen something like: .Red { colour: #ff0000; } – Tim Booker Jul 27 '09 at 22:29
  • Most of the time it saves bandwidth. – Andreas Bonini Jul 27 '09 at 22:37
  • 1
    i can argue by saying: .NewsItem is not good either, what if I want to reuse the "style" again, do I have to create a new one with a proper name? I am going to reuse the style that is for sure, I dont want to go back to the stylesheet and create new names every time... to me, .red is more indicative of a "style" than .newsTitle – Ayyash Jul 27 '09 at 22:54
  • 9
    @ayyash - incorrect. something like .newsTitle is way more explanatory than .red. what if you want to change the design of your site a year from now and you want all your headlines to be yellow? now you've got news titles with .red classes that are actually yellow! – Jason Jul 27 '09 at 23:17
  • @Ayyash the goal is not to reduce the number of styles defined... the goal is to make the content distinct from the presentation. – TM. Jul 27 '09 at 23:23
  • It's an improvement if you happen to need that class more than once and then need to modify it with an additional property like setting the padding to 0 or something. Changing it one place sure beats hunting for it everywhere. – Blair Scott Jul 28 '09 at 00:58
  • 1
    using such rules as a mini css framework will save a huge time in the design phase, I think. – sepehr Jul 28 '09 at 01:41
  • @dhulk in that case, you need to use better selectors, not bad class names. For example, something like `.sectionTitle, .sectionBody { margin-top: 15px; }`. This stops the duplication and also doesn't screw up your document. – TM. Jul 28 '09 at 01:58
  • 1
    It's also worth nothing that inline html styles are illegal in strict HTML/XHTML – Andreas Bonini Jul 28 '09 at 18:13
18

Ignorance of selectors.

For example, if you have a bunch of links in your footer that you want to style differently from normal links, don't put a class on each one, use a descendent selector.

// instead of this
.footerlink {
}

// use this
#footer a {
}

You can also group selectors with commas:

#header a, #footer a {
}

Other useful selectors include:

  • child selector: E>F
  • sibling selector: E+F
  • attribute selector: input[type="text"]
  • first child pseudo class: :first-child (incredibly useful for headings - you don't want the first heading in a div to have a top margin, but you do for subsequent headings)

Unfortunately, those latter few don't work in IE6 so use for progressive enhancement only (if you support IE6).

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
15

Using CSS hacks instead of conditional comments when writing styles for IE.

thedz
  • 5,496
  • 3
  • 25
  • 29
15

Not checking cross-browser as you develop.

It's best to catch and fix the differences before the whole site is done.

I've lost count of the "My site looks great in Firefox/IE/Safari but it's all screwed up in IE/Safari/Firefox." questions.

Emily
  • 9,926
  • 4
  • 31
  • 39
12

Failing to understand the cascade and inheritance thus ending up with a lot of repeated code.

Tim Booker
  • 2,801
  • 1
  • 25
  • 36
11
  • Not understanding (or even knowing about the existence of) box model

  • Not knowing what selectors are, or how to correctly use them

  • Using words to name colors (not all browsers know all the words)

  • Using invalid styles (padding:auto for example)

  • Writing #ffffff instead of #fff. (It's 3 pairs, which can be condensed into just 3 single values)

  • Not using a # on hex colors

  • Not making sure your page won't break when used at 150%-200% zoom. Old people/Almost blind people use the internet, too.

  • Not using enough contrast or white-space

  • Not validating the markup/css

  • Make sure your page degrades nicely

  • Calling yourself a CSS designer, you aren't designing a stylesheet, you're designing a website, by implementing a stylesheet.

  • Using absolute positioning (it's going to look fucked up on somebody's computer, somewhere)

  • Not keeping the stylesheet neat and organized. Don't listen to these websites that tell you to put everything on one line, because it saves bandwidth. You should keep it the way you find it easiest to read and modify, and then compress it when you're done.

  • Not putting quotes around long font names

Forgot one, my bad

  • Caring about IE6. Every time you argue that it should be supported or that people haven't updated yet, that's their fault, maybe if everything looked fucked up they'd be more motivated to download something that wasn't a heaping PILE OF SHIT. I could write a 20 page essay on how much I hate IE6, and I'm not joking. I once wrote an ex-boss a 5 page essay on why IE is the worst browser to use. He forwarded it to all his friends, and they all use Firefox or Safari now.

(I quit after he told me he wanted his website to look like his industry's leading company's website. It was an excellent website, and even had an original music score that played when you went through the galleries. He was paying me something like $15/hr (I was in highschool) and only let me come into work 9 hours a week.)

Sneakyness
  • 5,305
  • 4
  • 33
  • 39
  • 3
    "It was an excellent website, and even had an original music score that played when you went through the galleries" Bullet point 16; > Making websites with background music :P – RJFalconer Jul 28 '09 at 01:33
  • 6
    This is full of bad advice. Using absolute positioning is fine if you know how to use it (normally within relatively positioned blocks). And your reasoning applies to every single CSS rule. #ffffff vs #fff is a style issue, nothing more (I prefer consistency with the longer style). – DisgruntledGoat Jul 28 '09 at 01:50
  • If you're a multimillion dollar corporation, you're probably paying somebody enough money to put music on your website in a way that it's going to add something of value to the website. Especially in an art-related industry. It's all part of the experience, mannnnnn. – Sneakyness Jul 28 '09 at 01:50
  • *IF YOU KNOW HOW TO USE IT*. If you know how to use it, you wouldn't be making mistakes, now would you? If you are new to css, and you don't understand box model, or how to lay things out fluidly, using absolute positioning is not a viable alternative. – Sneakyness Jul 28 '09 at 01:52
  • @Sneakyness: sure, but the mistake of not knowing how/when to use positioning is not the same as your answer, which was "it's a mistake to use absolute positioning". In other words, the common mistake should be "mis-using absolute positioning" (with examples), rather than simply "using". – DisgruntledGoat Jul 28 '09 at 16:04
  • Every time I've ever used absolute positioning, it's been as a hack to make something like JQuery play nice. So, instead of me giving examples when it's wrong to use it, I'd like to see your examples of when it's right to use it. – Sneakyness Jul 28 '09 at 16:34
  • 1
    Without going into too much detail, drop down menus. Typically you set each `li` to a relative position, then the nested `ul` to an absolute position, moving it across or down. Also vital any time you want to position an element in, say the bottom right of an element. – DisgruntledGoat Jul 31 '09 at 12:49
10

Misunderstanding or not even considering selector specificity.

body div a.mylinkclass { }

is more specific than

a.mylinkclass { }
Joel
  • 19,175
  • 2
  • 63
  • 83
7

Not using Firebug

Andy Ford
  • 8,410
  • 3
  • 26
  • 36
6

not understanding what floats are, how to use them correctly, and how to clear them!

Jason
  • 51,583
  • 38
  • 133
  • 185
4

Not using CSS Sprites effectively, or even at all.

Jordan S. Jones
  • 13,703
  • 5
  • 44
  • 49
2

Not accounting for internet explorers broken box model in quirks mode.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
2

Not setting width for floating divs.

Not cascading the styles.

The following is not good:

body { color:#ff0; }

h2 { color:#ff0; text-decoration:underline; }

This would be better

body { color:#ff0; }

h2 { text-decoration:underline; }

#ff0 will automatically be applied, if not interfered in some other style definitions.

thirtydot
  • 224,678
  • 48
  • 389
  • 349
Thanashyam
  • 120
  • 8
  • But what if they exist in two separate sheets (body controlled by someone other than me) and you want to force #ff0 on the h2? – Joel Jul 28 '09 at 00:19
  • 1
    In that case, we are made to force the extra style. But what I am trying to point here is - we generally tend to forget the concept of Cascading. – Thanashyam Jul 29 '09 at 18:02
1

Not compressing production code with YUI's compressor

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
1
  • Using units that don't work with high dpi screens
  • Not using the full width of the screen
  • Not using a print stylesheet that hides everything but the content the user wants to print
ZippyV
  • 12,540
  • 3
  • 37
  • 52
0

Using class names that are too specific and not generic enough. Eg.

.redLeftNewsHeader

rather than

.header

What happens when you decide to restyle your site with a blue theme? What happens when you want to use the class on items that are not related to news?

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • Absolutely, but on some sites this is necessary. Sometimes CMS (vendors) split things into fairly small chunks and modules so that your CSS ends up looking like this. – dylanfm Jul 27 '09 at 22:40
  • 2
    Usually you can get around this by wrapping a container div around the HTML and giving that a class name and then use inheritance eg.
    content
    .header { color:blue; } .news .header { color:red; }
    – Dan Diplo Jul 27 '09 at 22:48
  • 1
    I'd rather too specific than too generic. It's confusing when css classes act on elements that you weren't expecting them to. – olive Jul 31 '09 at 19:32
  • @lhnz That is down to bad practise and nothing to do with using generic names. I'd rather have a generic class called .list than one called .newslist and not be able to apply it to, say, an events list because the name is confusing. Inheritance is how you sort this out, not naming. – Dan Diplo Jul 31 '09 at 19:56
0

Using "0px" instead of "0". Now I consider myself fairly good with CSS and yet I still do it sometimes...

Example: "padding: 0px" instead of "padding: 0"

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
0
  • Trying to do anything with CSS when the browser is in quirks mode.
  • Not using Firebug's tools to see the layout of your elements and refine your CSS.
  • Making fixed-height containers and not dealing with overflow.
  • Trying to use transparent PNG-24's in IE6. (Adobe Fireworks can make transparent PNG-8's that work in IE6.)
  • Not using units at all (very bad!).
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
0

Not considering your audience.

Arve Systad
  • 5,471
  • 1
  • 32
  • 58
0

I'd have to say using float's improperly and not understanding collumns. You can mutilate that kind of stuff really easily(personal experience.) so yah...float's and clear's.

codedude
  • 6,244
  • 14
  • 63
  • 99
0

Applying font-size, font-family or other properties which should be applied on block level to inline elements like a or span.

To give an example Once that kind of css is in use:

a {font-size:12px} /* NEVER! NEVER DO THAT */

Anytime you want to use something like:

<h2>Something about <a href='...'>this</a> and <a href='...'>that</a></h2>

You will also need to add selectors for:

h2 a {font-size:/*same size as for h2*/}

I know this may seem obvious and basic but I've seen it too many times.. The only point where your span or a should manipulate font sizes is a special css class used to make things look different:

a.different {/* do crazy stuff with your fonts */}
jakubiszon
  • 3,229
  • 1
  • 27
  • 41
-1

Try to use either margin or padding, rather than both, on an element. You can save yourself from some browser issues.

dylanfm
  • 6,292
  • 5
  • 28
  • 29
  • 3
    They have separate and distinct purposes (especially when it comes to backgrounds). You just have to be aware of how they affect the box model. – Gabriel Hurley Jul 28 '09 at 01:51
-1

writing piss poor HTML

Andy Ford
  • 8,410
  • 3
  • 26
  • 36