The Top 5 Mistakes of Massive CSS

The Top 5 Mistakes of Massive CSS

Just because you didn’t get to go to that awesome conference doesn’t mean that you can’t still watch the lectures! Each weekend, we’ll feature a recommended web development lecture on Nettuts+.

This presentation, by Stoyan Stefanov and Nicole Sullivan, primarily details the huge performance benefits to using Object-Oriented CSS, as well as a crop of other common CSS hurdles.

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

    güzel bir video olmuş tnx

  • Sherlack

    Awesome, thanks for sharing this!

  • http://www.nerd5.com Bogdanel

    Very nice presentation and very useful! Thanks for share!

  • Laurent

    It’s “really” not Ted video…

  • http://www.videofunk.nl Xander

    Tot ziens en bedankt voor de vis ! thanks !

  • http://360signals.com/about 360Signals Development

    Great video, really helping!

  • http://pixels-and-bits.com neil

    Great video!

    One question on number 3, unpredictable code.

    If you’re not styling the headings based on the module/pattern they are in, are you defining them all in your global heading solution?

    Meaning, your headings.css may include something like (?):
    - h3.loud
    - h3.sidebar
    - h3.not_that_important

    Thanks!

    • Mike Hopley

      That’s correct.

      The basic idea is that you avoid location-dependent styles. When you need to override your default heading styles, “h3.loud” is better than “#sidebar h3″.

      Of course, this does mean you will sometimes be adding an extra class to your HTML. The benefit is that your CSS becomes predictable, as well as shorter and reusable.

  • http://www.gregbabula.com Greg Babula

    Great presentation, thanks for sharing

  • http://www.jered.me Jered

    Already seen, but a great lecture!

  • ian hobbs

    Uh, and top five mistakes in presentations. …

    • Jonny

      My god yes. Number 1 mistake. Uh. Uh. Uh. Uh. Uh. Uh. Unbearable.

      • Jonny

        More fail. Not having ajaxified the comments, so the video stops. Sigh. Re-buffer.

  • Dan Gordon

    That is some great advice.

  • http://newdailyblog.blogspot.com Tahsin Hasan

    hello,

    thnks for the post. visit also tahSin’s gaRage.

  • http://www.diegocostantino.com dieguitoweb

    Great presentation! many interesting tips

  • http://eriknow.com Erik

    This was VERY insightful, thanks a lot for sharing!

  • http://fsck.hu keriati

    maybe somebody needs this:

    .media{overflow:hidden; overflow:visible; zoom:1;margin:10px}
    .media .img{float:left;margin-right:10px}
    .media .img img{display:block}
    .media .imgExt{float:right;margin-left: 10px}

    however it doesn’t match the html object she showd

  • MC

    Wait a minute.

    Dont style ID’s? Or elements?

    And only style using classes, isnt that extra html code and adding a level of complexity (adding classes to everything)?

    • Mike Hopley

      There are really two questions here:

      1. Do I use an ID, or a class?
      2. Do I use minimal HTML and longer CSS, or longer HTML and minimal CSS?

      (1) For styling purposes, use a class. It’s not “extra html code”, because you need to use *something* — class or ID. Classes are reusable; IDs are not.

      As Nicole says in the presentation, IDs are often useful in javascript DOM work (even with JS selector engines, getElementById has the advantage of being fast). But for CSS, IDs are very rarely useful; classes are almost always better.

      (2) Using minimal HTML means that you have as few IDs and classes as possible, and as few semantically empty elements as possible (div, span). Advantages: your markup is “cleaner”, with slightly fewer bytes down the wire.

      The downside of minimal HTML is that your CSS becomes complicated and massive. It’s difficult to write CSS in a modular fashion if you won’t allow yourself a few extra divs (e.g. as a module container) and classes (e.g. the module container’s class name).

      Nicole’s approach is to *slightly* increase the markup, in return for short, reusable, and robust CSS. This is much better for performance and maintenance. Bear in mind that CSS blocks rendering, so the size of CSS files is much more important than the size of other assets (e.g. images).

      • Jak

        I liked this presentation a lot, but I’m still a little stuck on the ID vs class issue. I thought IDs were perfect for styling something that only appears once on a page. In her example, she X’ed out its use for styling #navigation — but isn’t that the perfect use for an ID? There’s only one navigation per page, yes? I thought if you knew for sure that something was unique, it would be better to use an ID, because then whatever does the styling knows there is only one, and once it has been found, it doesn’t have to search the page further — the way it would if it was a class. Or is that not correct at all?

        Thanks, and again, I really learned a lot from this video.

      • Mike Hopley

        “I thought IDs were perfect for styling something that only appears once on a page.”

        IDs work great for that, but so do classes. If you are *certain* you will never want more than one instance, then an ID is fine. Otherwise, use a class.

        “I thought if you knew for sure that something was unique, it would be better to use an ID, because then whatever does the styling knows there is only one, and once it has been found, it doesn’t have to search the page further — the way it would if it was a class. Or is that not correct at all?”

        You are correct that, when browsers are parsing CSS, they match IDs faster than classes. But only very, very slightly faster. The performance difference is incredibly small — so small that it should not influence any decision you make.

        Nicole’s point in the video was that, even when an element is unique NOW, you may want another one when you revisit the design 6 months later. It’s hard to plan this far ahead, so why not just stick to classes? There’s no real disadvantage to using a class.

        It’s really up to you. The bigger message is that you should be using classes *most* of the time. If you’re styling *lots* of IDs, then your CSS is not reusable.

  • http://www.jakswebdesign.com Adam

    While I love 95% of this talk and can’t wait to start putting it into practice, I’m stuck with object oriented CSS grid systems. Seems an awful lot like table based layouts to me without using the a table tag, same non-semantic jumble of markup and everything…..anybody have a comment on that?

    • Mike Hopley

      I don’t understand why people get hung up on “table layouts without the table tags”.

      In terms of layout, tables are great: they let you make a robust grid system. CSS methods for grid-based design have been quite fragile by comparison — until relatively recently, when solid CSS grid frameworks came out.

      There are three problems with table layouts:

      1. Bad semantics (saying something is a table of data, when it’s not)
      2. Accessibility (many table layouts are hard to understand in a screen reader)
      3. Performance (many table layouts prevent progressive rendering)

      CSS grid systems have none of these problems.

      Note that “non-semantic” doesn’t really matter: adding spare markup doesn’t affect the end-user at all. Nobody looks at your source code and counts the divs (I’m talking about real people here, not web geeks ;) ).

      *Bad* semantics can matter, however, because you change the meaning of the document. Depending on how the document is accessed, this can affect the user’s experience.

  • http://appex.no Fremmers

    Great tips, thanks for sharing!

  • Ann

    This would be a fantastic presentation – if only the presenters were properly prepared! How about doing a rehearsal (or two) and get the bugs straightened out – make sure everything is working and that both presenters AT LEAST have mikes? C’mon basic stuff!

  • Filip

    Cool

  • http://bit.ly/cLZXGi Julian

    Nicole is absolutely brilliant. I’ll definitely buy her book when it’s published!

  • http://infowick.com web

    I completely agree with you, these are the most common mistakes committed!!..

  • http://www.nouveller.com/ Benjamin Reid

    I couldn’t really watch past the excruciating mistake about one minute into the presentation.

    • http://www.jeffrey-way.com Jeffrey Way

      That’s too bad, as you missed out on a great presentation.

  • Mike Hopley

    It’s excellent for Nettuts to highlight these presentations. Far too few developers watch YUI theater, and Nettuts is bringing the cream of the crop to a wider audience. :)

    What’s with the bitchy comments I keep seeing on these videos? Seriously, you have an opportunity to learn from people with world-leading knowledge in web dev, and all you can do is pose instead? Have *you* ever given a presentation at a major conference? How well do *you* present? Have you watched yourself presenting on video? Hint: you may not be as crisp, witty, interesting, and comprehensible as you think you are.

    Try it and see (I have).

    I think this is an outstanding video, both in content and presentation. I’d be proud to present half as well as Nicole does here. If you think you can do better, then prove it. :p

    • http://anthonymclin.com Anthony McLin

      Or the person uploading the video could have simply edited out that first 5 minutes of bumbling for working slides and microphones.

      Presentation videos on the web are a slightly different beast than live presentations, and a slight bit of work on the video editor’s part would have gone a long way.

  • Richard Testani

    When these conversations of optimizing css or javascript and such, where is the threshold for this? I do many sites for small businesses, where optimizing images makes sense for bandwidth, were mostly talking load times for css and js optimizations.

    So where do you decide to spend the time to apply these sorts of optimizations to gain 10% load times?
    Is it subjective or is it mainly for enterprise websites?

    Just curious – thanks!

    • http://www.meditationsonlife.com John

      Having coded both, usually an enterprise site that is going to be getting hit a lot (250,000 page veiws a day plus) you will spend a ton of time on the optimization of code, images, html, and js. On small business sites, usually they only get a couple hundred page views a month and you are only optimizing for load times/bandwidth.

    • Mike Hopley

      It depends on the type of performance optimisation.

      Some optimisations make the code less robust or harder to maintain. For example, unrolling loops in javascript can make them faster. In these cases, there are trade-offs to be made between performance and other aspects of code quality. Unless the performance improvement is huge, it’s usually better to aim for high-quality code.

      The optimisations that Nicole is teaching are different. They are faster, but they also improve your code quality: it’s shorter, more reusable, easier to understand, and easier to maintain.

      Learning to code CSS in “Nicole’s way” should actually *save* you a lot of time, especially on large or long-term projects.

  • WD

    I’m looking at the grid examples over on OOCSS.org and I’ve noticed a severe case of divitis, what’s ironic is that some of the display values are being rendered as table cells. So the HTML structure resembles old fashioned table based design and behaves the same only the actual element name is different. Isn’t this a sign that CSS is broken if this has to be done?

    Not only this, but they have proven that this performs better than the traditional layering method which is being advocated everywhere else?

    From what I understand I’ve got two options :

    A. Use semantic mark-up and use a ton of CSS to get things working how I want them. HTML is very clean and readable but the site will take a performance hit. If your site is small, take this route?

    B. Use unnecessary mark-up but this method significantly reduces the amount of CSS used which means better performance? So on a gigantic site, this could potentially save a ton of money and also improves load times?

    Am I understanding this properly?

    I think I need the Jeff Way to break this down into a nice tutorial with before and after examples so my puny brain can understand this a little better.

    • http://www.jeffrey-way.com Jeffrey Way

      hehe – it’s definitely confusing.

    • Mike Hopley

      The markup on OOCSS’s grid examples is actually impressively lean, when you consider how complicated some of the examples are. {display: table} is absolutely fine — indeed, were it not for IE6 and IE7, we would be using it all the time.

      If you want a reliable grid system in CSS, then you have to use a few extra divs. Is that really such a terrible thing?

      Don’t be too quick to cry “divitis”. Real divitis is when you use divs *instead* of a more appropriate element. For example, [div class="heading1"]. This is bad, because now a screen reader cannot tell that it’s meant to be a heading.

      If a few extra divs make styling easier, then use them! You’d be mad not to. They are harmless. They do not make the document “less semantic”.

      Minimalism is not a good goal. A good primary goal of web development is to make stuff that people like, or stuff that makes money (or both). Good secondary goals include making it interoperable, making it robust, making it easy to maintain or extend, making it accessible, making it run fast, and doing all this in a short time.

      You don’t win an award for having the least markup. In certain rather snotty circles, minimalist markup gets you geek cred. But who cares about geek cred.?

      • WD

        That’s a very good point. Perhaps I’m looking into this way too much and over complicating things. Your posts have been helpful Mike.

      • Mike Hopley

        Glad to help. :) Presentations like this one have been a revelation for my web-dev, and I like to share the understanding.

        For me, Nicole’s presentation pinpointed everything I’ve done wrong with CSS.

        I started out with the usual “web standards zealot” ideology, because it was heavily marketed at the time and, as a beginner, I didn’t know any better. Following this ideology has caused me a lot of grief; for example, my CSS became the most unbelievable tangled mess.

        Yahoo’s presentations especially (Sullivan, Crockford, Zakas, Koechley, Heilmann…) have taught me to be more pragmatic. My experience, which mainly consists of making horrible mistakes, suggests that these people know what they’re talking about. Most times that they warn about a bad way of doing something, I’ve already felt the pain. ;)

        John Resig is well worth watching too.

  • http://rayvellest.com Ray Vellest

    Brilliant tips! Thanks for sharing.

  • kevin

    what is grep and gzip as mentioned in the lecture?

    • http://www.stevendavisphoto.com Steven Davis

      google is your friend.

  • http://bibikova.com ben

    Most of the time it is Art vs. Structure.
    I do believe that in enterprise level sites structure should heavily dominate. In smaller scale sites, such as blogs, portfolios..etc Art should rule of structure. That means if one wants to make five different heading colors to spice up his personal site, he should do that, because doing it otherwise would sacrifice his artistic abilities.

    • http://www.stevendavisphoto.com Steven Davis

      I agree with this. I feel elements should be styled a certain default way. Like “All H3′s will be 18px size, Arial Font, bold, and green” but then you should be able to add a class for exceptions like “my h3 is all these things, but if i have a class of ‘special’ on it, then give it a red color”.

      I feel in general it’s best to start with a bunch of general rules for everything, then code for the “one offs” as needed by adding extra classes.

  • Really?

    Seriously – these guys get paid to stand up and come out with this…? Is this what people see as cutting edge innovative web development…?

    Wait – what? Review the site for similar css objects – wow- groundbreaking….

    “There was this one time, for facebook, I did my job and created a basic code fragment based on best practices, standards and (some) common sense, and then I came up with a funky name for it and everyone was like – ooooh wow”

    You could on the other hand embrace the richness of CSS(3) selectors, combined with common sense.. whats the C for again… Cascading – oh yeah – feel free to use that too….

    I guess the stats were interesting… oh and could she mention that FaceAche site one more time!

    P.S – the Emperor has no clothes!

    • http://www.material-designer.com Chris

      I tend to agree with you.

      The entire concept of “Object Oriented CSS” irks me to no end, especially class based HTML/CSS.

      There is nothing wrong with complex CSS. Design is a complex entity. Semantics, however, should not be complicated.
      IMO, people are wont to really understand the Cascading aspect of CSS.
      And you are also correct about the richness of CSS selectors. div#content > p > li + a[href="#"] may look complex, but if you are trying to target a very specific element, there’s no need to muck up its markup.

      • Mike Hopley

        The complexity of individual selector chains is not the issue. On its own, div#content > p > li + a[href="#"] is fine. Anyone can understand the rule, given just a passing familiarity with CSS.

        The problem is in the CSS *architecture*. When you have lots of highly specified rules, it becomes increasingly difficult to know which rule takes precedence. As your site grows, you get “specificity wars”: you try to add a new design element, but the CSS gets overridden by some older elements that have higher specificity. So you make your new rules even more highly specific — and so on…

        Similarly, as your site grows, you encounter problems from using location-dependent styling. What if you want to use that same style, but inside div#sidebar? You either have to duplicate your code, or you have to change your original selector to remove the location dependency.

        “There is nothing wrong with complex CSS. Design is a complex entity.”

        I beg to differ. CSS should be simple. The most important benefit of making CSS simple is that it’s easier to change or extend.

        “Semantics, however, should not be complicated.”

        Your HTML classes do not affect the document’s semantics. In terms of semantics, these are exactly the same:

        [h1]Welcome to my website![/h1]

        [div class="gridA27856 camel ultra melon" id="yellow_biscuit"][h1]Welcome to my website![/h1][/div]

        Seriously, there is NO semantic difference. The only difference is how the code appears to you or your team.

        Code != content. If developers can understand this, they can start choosing techniques based on effectiveness, rather than markup purity. The markup purists are worshipping false gods. ;)

  • http://www.techwench.com techwench

    thanks!!! for a good lecture
    …..

  • Kristiaan

    I don’t really get this clearfix – overflow:hidden; overflow:visible; zoom:1;

    I know zoom:1 and overflow:hidden can trigger hasLayout, but why reset it to visible then?

    Does it immediately create a new block formatting context at overflow:hidden and then, when you reset the overflow to visible, still keep that context?

    Seems rather funky to set a property twice in the same declaration

    • http://tugbucket.net Alan

      I’m curious as well as Kristiaan.

      i set up a local test and this:

      .media{overflow:hidden; overflow:visible; zoom:1; padding:20px; background: #eee; }
      .media div { float: left; width: 200px; height: 200px; background: #9ed36c; }

      <div class=”media”>
      <div></div>
      </div>

      doesn’t seem to do anything.

      • Kristiaan

        I got this response on the stubbornella blog:

        @Kristiaan – zoom triggers hasLayout for IE. Overflow hidden triggers “hasLayout” for better browsers, but will cause issues for IE. So, using the _ hack we set it back to visible for IE only.

        So I guess the slide should have showed:
        overflow:hidden; _overflow:visible; zoom:1;

  • http://www.absentsolutions.co.za AL-jerreau davids

    Great lecture, going OOCSS all the way from now :-)

  • http://www.makadco.com Makadco

    Great demonstration. Its a good idea to think about to reduce the style sheet file sizes for big applications for optimal performance. Yep, I found this presentation very interesting. Thanks

  • Scriptaculaire

    Thanks for sharing! Great lecture!

  • http://greg-j.com Greg Johnson

    Good information.

    Poor presentation

  • http://konradstrozik.com Konrad

    Great statistics, but I expected little more examples about “how to build optimized css structure” :)

  • http://CLUSTERfoo.com Noam Gagliardi

    Top notch stuff! Found myself facepalming at my own choices. I will definitely be using everything in this talk from now on and following her work closely.