So, in the course of however long I’ve been on here, I’ve seen some things, man dark things(j/k). And been part of some heated debates. This is all well and good as its the nature of the beast to say “yeah but so and so told me such and suck so there” or my favorite " I read it somewhere buy some guy".
Well as I have pointed out I spend time at work teaching many things to developers. One of the things I have noticed recently in general is the lost art of CSS. I debated weather or not to even type this out but hey, take this with you or leave it, its cool, but I am going to share with you WHY I write some things the way I do.
First thing I want to point out is that I work in Sharepoint CMS(Its a monster, a bear, and other descriptive words as well), so a lot of thought has to go into "well they want it this way NOW, but they probably want it to be able to do(fill in weird business requirement here) later. Yeah, its part psychic BUT, if you take an approach thinking "wouldn’t it be cool if it could also do this, odds are you’ll find life easier when they eventually do… but moving on.
DISCLAIMER: I pulled some snippets from other threads, I am NOT tying to pint faults at anything, just using examples.
Lets take this example:
.navbar-header ul li {
text-transform: uppercase;
}
So, whats wrong with this? Well to see the problem is to understand how the browser parses this information which is right to left. Meaning that the browser is saying “oh hey look, I have a style for list items, okay lets find ALL of the list items in a page. Oh wait, only the ones in un-ordered lists… okay… so lets re-check to make sure all the list items I should apply this style to are inside un-ordered lists by looking at every un-ordered list on the page. PHEW… okay… wait… they ALSO need to be inside a thing with a class of navbar-header??? DAMNIT, okay, recheck the page… AGAIN to make sure all the list items that are INSIDE an un-ordered list are ALSO inside a thing with a class of navbar-header. DONE”. Seems slightly non performant yes?
Solution? Give the li a class like “navbar-header-item” or some such. Now the browser will say “oh everything with the class of navbar-header-item gets this style and… DONE”.
I do work in Sass ( and frankly probably always will), so normally… lets take a button. In a reset sheet I would probably “blank it out”:
button : {
background: transparent;
border: 0;
...whatever else
}
then have a button specific style sheet:
.btn {
...default button settings
}
and then in the same style sheet have more specific styles:
.btn_red {
...styles specific to ONLY btn_red
}
and then markup:
<button class="btn btn_red>Oh hi Mark</button>
In this case, mr. browser, “oh I need to apply these red styles to this class, check, are they inside .btn class button?? check, DONE”.
So, why not just set button tag with styles? Well sometimes buttons need to look like buttons, sometimes they need to look like links. Also you might have to over-ride them in some instances which is something to try and avoid if possible. Another anti-pattern would be something like this:
button.btn {
...styles
}
Now the browser will parse any .btn class and STILL look if its part of a button, which is just un-needed work. Imagine if you have 20 buttons on a page but only ONE has the .btn class, The browser will still check to see if the class is part of a button, having to look at every button on the page, so the actual tag is irrelevant.
I tend to use the same technique with typography. Ill set classes fro all my heading ( such as .h2, .h3, whatever), that way if I have a heading that should SEMANTICALLY be an h3 but LOOKS like an h2, I can just apply the styles (I cant tell you how often I’ve seen bad markup due to styles for headings).
Anywho, there some stuff is, take it for what its worth, or don’t, lol.