top | item 2146580

How to Manage CSS Explosion

63 points| niyazpk | 15 years ago |stackoverflow.com | reply

26 comments

order
[+] mikeklaas|15 years ago|reply
You can't.

You can try to reduce the horribleness of a CSS file, but the language simply doesn't provide the tools needed to do the one thing all programmers need to manage complexity: abstract away the common part of two specific processes into a reusable component.

I'll never forgive the designers of CSS for not at least providing immutable constants, if not variables.

[+] brennen|15 years ago|reply
The more I think about it, the more certain I become that immutable constants (a good idea, IMO) are the furthest CSS could safely go down the rabbit hole before the entire enterprise would consume itself in spaghetti-code madness.

Not that plenty about CSS doesn't beg for better abstractions, but turning it into a programming language is kind of a terrifying prospect. I've see too much of what enthusiastic amateurs can inflict with JavaScript.

[+] ZoFreX|15 years ago|reply
You can.

So what if CSS doesn't have variables? We can add them. CSS is the format it has to reach the browser in, it's not the format we have to write in though. There are multiple projects out there for writing CSS at a higher level, which "compile" to CSS but let you use variables and things like that.

[+] tjogin|15 years ago|reply
Of course you can. The same way we make HTML manageable; by pre-processing it. Sass, Less to name just the two most common options.
[+] tintin|15 years ago|reply
"I am using a large number of div tags within the website (previously it was completely tables based)." That's one part of the problem. Why not use tables if you are using .title, .body and .footer for DIVs when tables have this as default? I see a lot of people using DIVs on places they shouldn't. They use things like <div class="title" ... Why not use H1 - H6 instead?

HTML tags provide structure. Structure means less complexity. Using only DIVs will get you into CSS trouble.

Edit: A good tip: your website will look more consistent and clean when you try to build your pages by only declaring HTML tags in your CSS at first. Only when there is no other solution declare classes and IDs. And even then remember you can use multiple classes in your class attribute. Things like: class="width50perc floatLeft textAlignRight textColor1" are very handy.

[+] notJim|15 years ago|reply
> Things like: class="width50perc floatLeft textAlignRight textColor1" are very handy.

At that point, how is this any different from just using inline styles? The whole point of CSS is to separate presentation from content, you've done gone and thrown a bunch of presentation back into it.

[+] kevinburke|15 years ago|reply
Surprised no one mentioned Sass or LessCSS.
[+] callahad|15 years ago|reply
The current version of Less is really impressive. It's written in JavaScript, which lets you do crazy things like:

  <link rel="stylesheet/less" type="text/css" href="styles.less">
  <script src="less.js" type="text/javascript"></script>
Since it's a superset of plain CSS, both the migration and the learning curve are really pleasant.
[+] ollysb|15 years ago|reply
I really like less as an improvement to css. I always end up discarding it though because of the dual representation you end up with. I've tried using the https://github.com/cloudhead/more/ plugin for rails which generates the css on the fly, but it doesn't work reliably enough for me to be confident the changes I've made have been reflected in the page I'm looking at. I also spend a lot of time in firebug tweaking rules and it can be a pain to translate back when you've abstracted the rules you're working on. I think the extensions they've made to css are absolutely the right choices, very minimal. If the browsers would support it I think it would be a winner.
[+] NHQ|15 years ago|reply
I love CSS. It's so easy to get the results you want. Redundancy, messiness, and other complaints are a small price to pay for that.
[+] Swizec|15 years ago|reply
I wish my designer would read this ... but he's just a designer, what does he care about the quality of his CSS.

Any good stackoverflow answers for that? Making people who think "code" is a side-product take pride in that part of their work too?

[+] beza1e1|15 years ago|reply
My personal guidelines:

* Partition into sections for fonts, colors+images, positions+spacing, text, and specials.

* Refactor for minimal property duplication, instead of selectors. For example text-weight:bold; will be mentioned only once, but the "#content a" selector multiple times.

* Use a CSS reset.

* Make an extra file per browser+version for hacks. Do not use hacks, which make sure they get triggered in IE7, but not 6 or 8, for example, but use ie6.css, ie7.css, and ie8.css. Duplicate code for this if necessary.

Example: http://beza1e1.tuxen.de/style.css