Max CSS In Depth

Posted on

Yesterday morning, I spent five minutes writing up an idea I’d had in my head for quite some time. It turned out to become far more popular than I had anticipated — reaching over 10,000 hits in 24 hours thanks to links from Twitter, Hacker News, .Net magazine, and Smashing Magazine amongst others. The idea was simple — alongside our compressed and minified CSS files, we should include an uncompressed, no holds barred, comment-laden version of our CSS files for people to take a look at for learning purposes.

“Max CSS” has had a bit of a mixed reaction. Most people seem to agree with the sentiment, if not the proposed execution. A few people just don’t get it. So I’m going to take a minute to assess some of the more popular/common/heated concerns that have been raised on Twitter, Hacker News, and elsewhere.

“Just use web inspector/firebug/etc.”

Sure, tools like web inspector will tell you the styles for any given element on a page, theoretically helping new designers and developers learn techniques by way of example. But that’s not what Max CSS is about. Max CSS is encouraging the preservation of a CSS file’s structure and comments from the author, explaining potentially bizarre declarations, hacks, or methods used, and the reasoning behind their code.

There’s so much more to writing good CSS than the code itself. In fact, I’d say that commenting and structure are almost more important than the quality of the code itself in terms of maintainability — well documented and organised code is crucial for development of any site, with any number of people in a team. In short, Max CSS is about the author’s comments and organisation, ;not the code itself.

If, for example, we took the media object in a large, minified CSS file, we’d probably get something like this:

.media{padding:10px;overflow:hidden}
.media .img{float:left;margin-right:10px}

A bit of an eyesore, but you get the general idea. However, someone new to CSS might not get it completely, or might go blindly in and copy the code into their own site or app, completely unaware of the meaning behind the code. Jump to our style.max.css and we can see the author’s original intentions, clearly documented with comments:

/*------------------------------------*\
    $MEDIA
    .media object for containing an image or some other media and corresponding text/content.
    Usage example:
    <div class=media>
        <img class=img src="profile.jpg" alt="">
        <div class=bd>This is my face. There are many like it, but this one is mine.</div>
    </div>
\*------------------------------------*/

.media {
    padding: 10px;
    overflow: hidden; /* Clear floats - though a more reliable clearfix is recommended */
}

        .media .img {
            float: left;
            margin-right: 10px;
        }

And just like that, we’ve taught our designer/developer in-the-making how to clear a float, as well as informing them of better ways to do so, and demonstrating some decent practices for organising and structuring CSS. Knowledge is power.

“Minifying helps prevent code from being stolen.”

Hardly. There are tools available that allow developers to paste in minified CSS and have it reformatted for readability. Not to mention the fact that tools like web inspector allow you to view the CSS of any element — and whadyaknow, it formats it for you and everything. A quick copy-and-paste, and your code has been stolen.

But once again, if you find yourself worrying about this, you’re not really getting the idea of Max CSS — it’s a learning tool. Most new designers and developers start out by ‘stealing’ code. Replicating websites on their own computers (though admittedly, sometimes on live sites) for practice. It’s healthy. We’ve all been there. By giving those who want to learn unrestricted access to the fully documented CSS, at least we can point out potentially odd parts of code and explain what’s going on.

“A steep learning curve is healthy for up-and-coming designer/developers.”

It was this comment in particular that really got me. This chap seems to think that the world of web design is dictated by natural selection — survival of the fittest.

What?

Forgive me, but “survival of the fittest” is the very last thing I’d ascribe to the web design and development community. The complete opposite. I have never come across a more forgiving, more helpful, more universally accepting, curious, or happy community than the web design and development community I’m proud to call myself a part of. I can’t even imagine another industry like this one. We have a magnificent gift, fellow web workers. We can make our own future. And the future of those after us. We can literally change the world. And if you’re too busy designing like your life depends on it, in a “survival of the fittest” state of mind, you’re going to end up with CSS that’s impossible to maintain by your future self, let alone those who might pick up your code later on.

By hiding your secrets, you’re doing no one any favors. Someone might well come across a problem in your CSS you might otherwise have never noticed, and give you a helping hand in fixing it. But not if you minify it.

You might be able to give someone a piece of code that’ll help them with a problem they’ve been having for years.

But not if you minify it.

Other, smaller concerns.

There were a few other things people mentioned. Namely:

“This is what GitHub is for!”

Correct. If you’d rather put your unminified CSS up on GitHub, then that’s great! Not only does it encourage the same kind of explorative learning that inspired the idea for Max CSS, but you’re also allowing a wider community to significantly and easily contribute directly to your product. All good things.

“Why would you call it *.max.css? Why not omit the .max?”

This was mainly due to the fact that some systems — such as WordPress — require you to call the stylesheet simply style.css. The .max was a way around that. But you can call it whatever the heck you want.

“But I’m using LESS/SASS!”

Why? But personal preferences aside, good for you. You’ve found a workflow that works, which is good. There’s not much you have to change, though — simply provide a link to the unprocessed and unminified LESS/SASS files.

I still don't get it.

I’m not sure I do either. All I know is that it gives me such joy to look at the CSS another developer has worked on — get my head around the techniques they’ve used and let that lead me to questioning my own approach. Can I do better? Probably. Can I help another person do better? Hopefully.

Not minifying your CSS — or providing a direct link to the unminified version — is the best thing we can do to encourage new designers and developers. Well, second best — right after actually taking the time out to reply to their tweets and emails.

Be helpful. It’s what newbie you needed when you started.

  • http://twitter.com/shpoonj Flip Stewart

    Tools such as Recess[1] allow you to make crazy readable CSS from your LESS files. Even when using stock preprocessors, you have the option to set multiple output styles[2]. No excuses.

    [1] http://twitter.github.com/recess/2 http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#output_style

    • ImINaBAR

      Awesome! You just blew away my excuse.
      Thanks.

  • http://twitter.com/thecodezombie Jason Hobbs

    Like I said earlier, my only reservation with max.css is that it could spawn copy/paste fiends…grabbing “larger than appropriate” chunks and using them blindly to get the job done, as opposed to actually learning about how they work and what they do (I was like this back in early college before I was bitten by the dev bug).

    But I suppose max.css isn’t for the people wanting shortcuts, or the thieves. Like you pointed out, even if it’s minified, once the code is out there it’s steal-able and can be de-minified.

    Just a point, CSS pre-processors might be both a curse and blessing for max.css. A blessing because changing output type from “nested” to “compressed” (SASS lingo) takes seconds. A curse because nested @media’s compile into @media blocks, and @extend adds a class to the extender class…what I mean is that it can get a little muddled. BUT, the saving grace is, the preprocessor files are also readable in-browser…so people can just read that.

  • jordankoschei

    Great idea, Dan. I don’t think we can ever stop people from stealing our code, and the benefits to the community definitely offset the risks. You’ve won a convert — I’m going to start adding max.css files to all my sites.

  • Pingback: Is max CSS the right way to go? | ValidateThis

  • http://willworkforart.net wwfa

    I wholeheartedly agree. I’m all for sharing code. View source was how I learned the web.

    How about adopt something similar to humans.txt that could scope out the links to the unminified js and css or less or sass and whether its on github or wherever…

  • http://twitter.com/aplombontwerp Aplomb

    I like the idea, but I think the developers who want to find out how you did something, will find out eventually. And that’s a better way. It’s nice to be able to read how you’ve coded your website, but it’s more rewarding to wrap your head around the techniques yourself, right?

    • http://willworkforart.net wwfa

      They would, hopefully, eventually wrap their head around it, but why not make it even easier for them by giving them direct assess to the bigger picture?

  • csiu88

    http://coderwall.com/p/t/css same function on learning pro tips

  • Clark

    There’s already a feature to do this in chrome nightlies; Its called Source Maps.

    Heres an article explaining it for javascript, but the plan is to port it over for CSS.

    http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/

    I think support for it is limited to Chrome Canary at the moment, but hopefully will be rolled out properly soon.

  • http://twitter.com/envex Matt Vickers

    The thing about this is — no one I’ve ever known has commented css like that. Why would I waste my dev time commenting a css file just on the off chance someone will want to learn from what I’ve done.

    Isn’t half the fun of developing something figuring out how to do it be searching and tinkering? Being able to just copy and paste other peoples work defeats that purpose.

    • http://willworkforart.net wwfa

      Believe me, I’m the last person to comment css, even when I’m collaborating with someone else. Most of it is pretty straight forward enough to figure it out.

      It’s when you start getting into some of the transitions and transforms that I have to comment for my own sanity.

  • chriscoyier

    Mine is called “global.scss” — but I know how you feel about preprocessors ;)

    • http://willworkforart.net wwfa

      It took me ages to finally like pre-processors. It just didn’t seem right after having no problem writing css for 10+ years.

      Still haven’t fallen for coffeescript, but we’ll see.

  • http://twitter.com/Nitza Daniel Nitsikopoulos

    I like the idea. I’m all for helping out my fellow developer and will do so gladly. But I don’t think placing a simple comment in my CSS will do any good. Especially for things like using overflow:hidden; to “clear a float”. What’s a float and why do I need to clear it? And why does overflow hidden work like it does? Do I need to document my code for someone that’s just figured out how to view source?

    When I first started, I picked up the CSS from a site I liked and went through each rule, and searched it on w3schools (yes, kill me :P ) to figure out what it was for, which was pretty futile because I had no HTML to go along with it, and I see you’ve mentioned that above, but do I need to write a tutorial style explanation for every snippet of code? I don’t know.

    Basically I’m unconvinced as of yet. If people truly want to learn to write CSS, they will, documentation only plays a small part in someones education as a developer.

  • http://twitter.com/joesnellpdx Joe Snell

    Great post and follow up. I’d simply say “Be cool, don’t be a douche. Share your code.” Have a version of your code/css that easy to follow & understand for all. I’m often concerned about handing off my work to a client where down the line another developer is going to get in and make modifications. I feel it is my responsibility to lay out the game plan for him/her. Totally agree.

  • ImINaBAR

    As @twitter-447608506:disqus suggests you can already do it on SASS (and probably LESS). I did a bit of digging and came up with a simple solution. Just add something like:
    /*! relative path for original SASS file: ../sass/styles.scss */
    to the beginning of your SCSS code. Because the comment has an exclamation mark (!) at the beginning it won’t get removed by the compressor even when compiling :compressed CSS.

  • Pingback: How To Write Code Comments Well - Aj Freelancer