tags:

views:

458

answers:

6
+1  Q: 

CSS vs DRY

You're creating an HTML layout. Let's assume that you don't need the benefits of multiple stylesheets, that a small increase in HTML size is not a concern, and that you have a style which will only be used once. I'm often in favour of using an inline style here, as I view the repetition of your CSS class name or ID as the cost of an abstraction you don't currently need, and may not ever use.

Standard doctrine these days is to always create HTML layouts using semantic markup and CSS styles, so am I missing something here? Please let me know your thoughts.

+1  A: 

There are some situations in which I usually neglect creating a new class for a simple style change on a single element. It is usually pretty clear when you are doing it that there's a low-to-zero chance of you needing to apply that particular style to something else later down the road. The most common case for me is when I need something to have a particular padding/margin to be in the right place but it's not an element important enough to have its own ID.

This may not be a popular opinion here, but in those scenarios I don't think an inline style is evil.

Paolo Bergantino
Calvin
Not really what I meant, but okay.
Paolo Bergantino
What isn't really what you meant?
Calvin
+9  A: 

Even if you only use a particular style once there are still benefits to keeping it with your other styles and not putting it inline. First, there is the separation of concerns that leads to improved maintainability. If you know you are going in to make only a style change, there is a single place to look for any changes. Another benefit is the self-documentation from having to type out the class name. By giving that style a name, even though it is used once, it makes the semantic code below more declarative -- you can read that not only is this random p a paragraph, it is also, say, the intro paragraph.

This is, of course, assuming that you are never going to use that particular style again. If you might than there is even more reason to factor it out into a named style. Inline styles aren't evil, but they are somewhat of a gateway drug.

rndmcnlly
I don't think I agree with your improved maintainability argument here. To modify a non-inline style, I have to find the tag I want to change, and then find its entry in my CSS file. Self-documentation is a nice benefit but when it comes to modifying styles I've made more than about an hour ago, I always use Firebug to work out what the hell I was doing before changing the HTML or CSS. If you might use that style again, you also might not! I'm a firm believer of YAGNI here.Love the gateway drug simile.
Alex
+3  A: 

One advantage of keeping the HTML and CSS separate is that you can re-skin the webpage without changing any of the HTML.

Steve

Steve Harrison
This is one of those "advantages" that I am still looking for an actual instance where it comes into play. 99.9999% of the time you're re-skinning the site you're going to need to change the HTML. Zen Garden is cute, but it's not like that in real life.
Paolo Bergantino
Then you're confusing re-skinning with redesigning. Most blogging applications, CMSs, forum software, etc. can be re-skinned simply by swapping out the stylesheet. But you can't add a search box to a layout simply by changing CSS.
Calvin
This is usually not true, and should not be regarded as one of the reasons to use CSS. In practice, this is only true to very minor changes. A complete redesign will very often require some changes to the markup.
Arve Systad
Certainly, as Paolo Bergantino says, a complete re-design will require changes to the HTML. However, as Calvin points out, re-skinning is different: re-skinning often takes the existing structure and just changes how it looks. For this, separate CSS stylesheets are ideal; if the styling were embedded in the HTML, re-skinning would be much harder. This is not to say that you should never use inline styles—I'm just pointing out one of the potential advantages of keeping your HTML and CSS separate.
Steve Harrison
+4  A: 

Ideally your CSS should be "Object Oriented" (at least, as OO as CSS can be). You should "inherit" from classes that set common properties and create new classes when you define properties that could be used elsewhere.

Take a look at the OOCSS project which is trying to espouse these principles (or re-introduce them as it were).

To quote Welbog:

... It seems to me that "OOCSS" is just CSS that isn't written haphazardly. Much the same way you can write non-object-oriented designs in OO languages, you can easily mess up the fundamental ideals upon which CSS was created. OOCSS seems to be saying, "Let's not screw up anymore, guys."

brianpeiris
Great link. It's full of the kind of principles I wish I'd known about when I _started_ creating my current project.
Alex
Same here. The web is crowded with people who started writing CSS in a head-first manner (myself included). Unfortunately it has lead to a lot of un-semantic code, wholesale confusion and a general lack of understanding of CSS's capabilities and uses.
brianpeiris
A: 
CodeJoust
A: 

"A foolish consistency is the hobgoblin of little minds"

So, in this case is which is the foolish consistency? :) Why does DRY take precedence over the separation of markup and style?

Can you be sure that your CSS rule will only be used once? More over, how can you be sure that it won't need to be changed in the future, and how can you be sure that you would be the person needing to make the change? Are you sure you even need to add a class or id to target this unique element?

I guess I am having trouble seeing how adding

<input type="submit" style="border: 1px solid red;"/>

is some how "superior" to 12 or so more characters

<input type="submit" class="b-red">

.b-red {border: 1px solid red;}

or to a potentially equivalent character count

input {border:1px solid red;}

Of course there are situations where every rule of thumb can and should be violated. The question is, what do you gain from following DRY that outweighs the importance of following markup/style dichotomy?

jacobangel