CSS Spirit Quest
CSS annoys the crap out of programmers
- It's not that hard...
- But it's harder than it should be.
Some numbers
Days I spent in a web development immersive course: 90
Days the intructors spent on CSS: 1
1.11% of class time
...and some of that was spent talking about Bootstrap
Some people don't even think being a CSS expert is a thing anymore
“What we may not have realized is that once the browsers don’t suck, being an HTML and CSS 'guru' isn’t really a very marketable skillset.”
- Jeff Croft
Okay, but...
- Browsers still kind of suck
-
“There is a big difference between someone who knows how to write good CSS and HTML and someone who doesn't.”
- Patrick Hereford
So, what makes CSS good?
Ideally, CSS is:
- Functional - it makes the page look and behave the way you (and your designer) want it to
- Structured - it is readable, semantic and organized in a non-crazy fashion
- DRY - it achieves the desired end goal with a minimal amount of code, without redundancies
- Maintainable - it is flexible and easy to edit when future style changes are desired
In the wild:
- I just need to make sure it's functional.
- Maybe if there's time I can try to make it structured and DRY and maintainable and whatnot.
It's hard to follow best practices when you're not writing CSS in a vaccuum - but that's when you need them the most.
discovery-item-container {
color: #b5b3ae;
border-top: 1px solid #d2cbb7;
margin-right: 0;
height: 63px;
display: inline-block;
width: 100%;
img {
display: none;
}
.discovery-item {
margin-left: 3%;
padding: 0;
margin-top: 3px;
height: 100%;
width: 96%;
display: table;
}
.discovery-figure {
display: table-cell;
vertical-align: middle;
margin-left: 5%;
padding: 0 5% 0;
line-height: 16px;
width: 15.5%;
.figure-f {
vertical-align: text-top;
}
.figure-ig {
text-decoration: underline;
font-size: 10px;
line-height: 12px;
vertical-align: text-top;
}
}
}
Writing good CSS makes it easier to keep writing good CSS.
A lot of CSS standards and best practices are widely agreed upon...but not all of them
- IDs vs. Classes
!important
- Progressive enhancement vs. graceful degradation
“I have a blanket-ban on IDs in CSS. There is literally no point in them, and they only ever cause harm. Everything that needs styling is done so without using IDs. ”- Harry Robert http://csswizardry.com/2012/04/my-html-css-coding-style/
IDs vs. Classes
- IDs are kind of like !importants in that they trump everything(...except !importants). This can cause enormous headaches and specificity wars.
- Style-wise, there is nothing an ID can provide that a class can't.
- By definition, IDs can't be reused - but in some cases, this is semantic and appropriate.
- Avoiding using IDs for CSS can lead to grossness like this:
.class.some-other-class.one-more-class.yet-another-class {...}
Takeaway: Only use IDs for CSS if it really makes sense - and it probably wouldn't hurt to not use them at all.
Using !important
- The common feeling seems to be using
!important
= bad. - In reality, not always true. Abusing
!important
= bad. - Littering stylesheets with too many
!importants
is usually a signal that you've lost control and understanding of what's going on with your CSS. - But, that doesn't mean there isn't a use case for it.
Utility classes
.button {
background: red !important;
color: white !important;
padding: 3px !important;
border-radius: 5px !important;
border: 1px solid black !important;
/* For good measure */
text-decoration: none !important;
}
http://css-tricks.com/when-using-important-is-the-right-choice/
Takeaway: !important is okay to use, as long as you're using it intentionally, and not out of exasperation.
Graceful degradation vs. progressive enhancement
Start with the most modern browsers and work your way down to IE8
OR
Start with IE8 and work your way upward
Feature detection with Modernizr
html class="js canvas canvastext no-geolocation rgba hsla multiplebgs borderimage
borderradius boxshadow opacity cssanimations csscolumns cssgradients cssreflections
csstransforms csstransforms3d csstransitions video audio localstorage sessionstorage
webworkers applicationcache fontface"
http://domain7.com/blog/progressive-enhancement-graceful-degradation-modernizr
Graceful degradation
.button {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.no-borderradius .button {
background-image: url('rounded_corners.png');
}
http://domain7.com/blog/progressive-enhancement-graceful-degradation-modernizr
Progressive enhancement
.button {
background-image: url('rounded_corners.png');
}
.borderradius .button {
background-image: none;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
http://domain7.com/blog/progressive-enhancement-graceful-degradation-modernizr
Some thoughts
- Progressive enhancement seems to be favored in the community
- But graceful degradation is more intuitive to us as programmers, and feels less...degrading?
In conclusion
- When writing CSS for big apps, play the long game - CSS is a team sport
- There are lots of agreed upon best practices out there to guide CSS development - but there's still room for debate and personal style
- CSS is annoying, but worth trying to master
CSS Spirit Quest
By Laure Fischer
CSS Spirit Quest
- 702