Hey guys, yesterday I found this post that goes over some of the ways to spot bad CSS code. One of the things the author says is that “undoing” styles is bad practice. For example, if you’re using declarations like
border: 0;
padding: 0;
float: none;
margin-left: 0;
Outside of a CSS reset, that means you applied the styles too early. This is something that I’ve never heard before, but now I can’t shake the feeling that I should rewrite my site’s CSS to be more efficient should a redesign ever be necessary (which it obviously will at some point)
The one thing I can’t understand, and can’t find anything else on, is this: does this suggestion apply only to undoing styles entirely, or should we avoid overriding styles too? My understanding was that the purpose of the CSS cascade was to allow us to override earlier styles rather than creating more selectors than necessary.
For example, say we want EVERY <h1> tag on an entire page to be black, 2em tall, and have a border to the left. Just an example.
h1 {
font-size: 2em;
border-left: 5px solid #000
color: #000
}
Then, later in development, we find that we want another h1 on the page, just one, to have a different colored border. Like this:
h1.derp {
border-color: #bada55
}
Or we may want to remove the border entirely:
h1.derp {
border: 0
}
Are either of these really bad practice? Would it really be worth it, in your eyes, to go back and give a different class name to every OTHER <h1>, like this?
h1 {
font-size: 2em;
color: #000
border: 5px solid
}
h1.herp {
border-color: #000
}
h1.derp {
border-color: #bada55
}
Sure, it’s cleaner CSS, but it seems like just overriding the original style would be cleaner HTML-wise.
This is obviously an extremely simple example, and in my style.css I have TONS of declarations that unset previous styles…which is probably what breaks my site when I try to use CSS minifiers.
What are your thoughts on this?
And just because, here is a link to my site’s CSS file (before the whitespace is stripped): https://dl.dropbox.com/u/87222573/style.css
If anyone wants to look through it and point out any glaring specificity mistakes, please do.