3 ms·
"Yes, CSS3 has some fairly advanced selectors, but littering your stylesheet with HTML-dependent stuff like “section ~ article h2″ is only going to cause you pa
by meric 14y ago
"Yes, CSS3 has some fairly advanced selectors, but littering your stylesheet with HTML-dependent stuff like “section ~ article h2″ is only going to cause you pain when you want to refactor, or simply target headings that don’t follow a section. Either you end up with 20-line-long lists of selectors, or you’re afraid to touch your HTML structure in case everything blows up."
Still a case can be made to reduce use of classes. e.g. for input[type=text], etc. Typically for any one site the style for most elements should remain consistent - you wouldn't have half a dozen different styles of input[type=text].
I haven't worked on an especially large website before and I'm unsure of the following points:
1. Let's say you're integrating two site-lets into a bigger site. Would namespace be a problem for classes specified in the two site-lets? What about namespace clashes between different "plugins" (e.g. jquery UI or something less polished).
2. Would the use of classes lead to "specificity war"? e.g. lets say we have the following CSS:
h1:first-child { padding: 10px; }
h1.large-heading { font-size:x-large; padding: 25px; }
And then later a web developer comes along and slaps in a h1 like this
<div><h1 class="large-heading">some text</h1> <p> some content </div>
He finds it breaks the h1:first-child rule and so goes and adds:
h1:first-child { padding: 10px !important; }
h1.large-heading { font-size:x-large; padding: 25px; }
And we can see where this is heading... when another developer sees the h1:first-child rule overruled his p + h1:first-child rule and slaps an !important next to it.
If we're using relative position selectors instead there wouldn't this problem be solved?
body > h1:first-child { padding: 10px; }
body > div > div > h1:first-child { font-size:x-large; padding: 25px; }
body p + h1:first-child { font-size:large; padding: 25px; }
I know generally you don't use headings like this but I'm trying to craft a simple example. I am not sure it still carries my point properly, so I'm crossing my fingers.
You mentioned being afraid of changing the HTML if your CSS is html dependent. Well if your website warrants a refactor of HTML wouldn't it warrant a refactor of CSS as well? After all, CSS is for describing how a piece of HTML should be presented. If you want to describe how an alternate set of HTML be presented, you use an alternate set of CSS rules, hopefully instead of injecting style information into content HTML that should ideally be only presenting information. There's a reason we stopped using <b></b> and <font></font> within our HTML, right?
- crazygringo 14y ago> Typically for any one site the style for most elements should remain consistent - you wouldn't have half a dozen different styles of input[type=text]. You could easily have more than half a dozen. You might have 3 different color schemes (light, medium, dark inverted), 5 different widths (what kind of form field is it?), 3 different font size contexts with customized padding, one with autocomplete and one without, one that's flat and one that's 3D, one that displays default text and one that doesn't... multiplying those yields 360 different variations, generated from 17 different classes. Not all those variations will actually be used, but this is certainly a reasonable visual vocabulary for a medium-size site.