Mo CSS, Mo Problems
May 01, 2015
You may not know, but your CSS is getting out of control, it may even have a mind of its own.
The specificity war is happening right now and you might not even know it.
Is your file always getting bigger? Are you adding stuff to the end of the file? You are showing symptoms of CSSitis. May be linked to Divitis, you can get it treated, but you have to act fast.
Users time should be the highest priority. More code, more potential bugs, more time fixing things. A product should be fast, systematic and maintainable.
42 Popular sites and frameworks
CSS File Size - kb
CSSitis is a condition that causes performance and maintainability issues. It may even keep you up at night, it’s a scientific word for - too much css.
Highest specificity score
Excessive use of nesting in sass and use of !important
effects performance. Remember the Inception rule, three levels down, you could lose sight of what’s real.
It will become hard to read for your team, so aim for two levels. Using too many ID’s instead of classes will clog the users browser.
CSS stats
Floats | Unique colors | Font sizes | Font families | Media queries | Style tags | Style links | |
---|---|---|---|---|---|---|---|
Basscss * | 10 | 10 | 8 | 3 | 3 | 0 | 1 |
Pure CSS * | 1 | 13 | 14 | 7 | 2 | 0 | 1 |
Tachyons * | 12 | 31 | 13 | 12 | 3 | 0 | 1 |
Unsplash | 63 | 23 | 29 | 7 | 9 | 0 | 1 |
Wikipedia | 4 | 20 | 21 | 3 | 13 | 3 | 0 |
Bootstrap * | 38 | 36 | 23 | 7 | 12 | 0 | 1 |
Stripe | 3 | 28 | 22 | 4 | 4 | 0 | 3 |
12 | 32 | 19 | 5 | 4 | 8 | 0 | |
Smashing Magazine | 40 | 46 | 26 | 10 | 10 | 2 | 2 |
Marvel App | 13 | 63 | 28 | 8 | 15 | 1 | 2 |
CSS-Tricks | 57 | 52 | 49 | 5 | 6 | 1 | 2 |
PayPal | 38 | 41 | 69 | 29 | 21 | 1 | 1 |
Skyscanner | 100 | 32 | 38 | 10 | 25 | 1 | 6 |
Slack | 31 | 90 | 59 | 7 | 40 | 0 | 4 |
IMDB | 124 | 79 | 49 | 18 | 2 | 0 | 7 |
Apple | 38 | 16 | 32 | 31 | 44 | 0 | 5 |
69 | 53 | 27 | 24 | 4 | 1 | 7 | |
Yahoo | 28 | 140 | 72 | 12 | 20 | 7 | 2 |
Airbnb | 84 | 43 | 50 | 11 | 17 | 0 | 2 |
Dropbox | 37 | 26 | 21 | 4 | 9 | 2 | 17 |
BBC | 58 | 54 | 60 | 18 | 74 | 1 | 6 |
Rightmove | 134 | 48 | 73 | 11 | 6 | 0 | 2 |
Zoopla | 227 | 81 | 63 | 15 | 4 | 1 | 3 |
UFC | 141 | 53 | 37 | 4 | 2 | 0 | 7 |
Medium | 69 | 51 | 57 | 10 | 35 | 0 | 2 |
Youtube | 210 | 49 | 30 | 4 | 24 | 1 | 4 |
Etsy | 232 | 91 | 54 | 18 | 50 | 1 | 5 |
Dribbble | 313 | 51 | 41 | 5 | 49 | 0 | 5 |
280 | 78 | 49 | 14 | 29 | 0 | 3 | |
Github | 399 | 151 | 63 | 17 | 12 | 0 | 4 |
The Guardian | 307 | 69 | 38 | 16 | 36 | 8 | 3 |
Stack Overflow | 216 | 176 | 40 | 9 | 4 | 0 | 1 |
448 | 127 | 76 | 16 | 33 | 1 | 5 | |
Kickstarter | 462 | 87 | 89 | 23 | 51 | 0 | 1 |
* css frameworks |
Stats from CSS Stats. Have an idea? @csspurge, roy[at]csspurge.com and Github.
Highest floats – 462
Did you know there are only three floats?
.fl { float: left; }
.fr { float: right; }
.fn { float: none; }
Highest colors – 176
How many colours does a brand have? Fifty shades of grey is the exception. It’s possible to just have one color for all tags and then one for links
$black: #111111;
$blue: #0074D9;
body { color: $black; }
a { color: $blue; }
Highest font sizes – 89
By default the web has provided 9 tags to give you a robust scale
body { font-size: 16px; }
h1 { font-size: 2.25rem; }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.5rem; }
h4 { font-size: 1.25rem; }
h5 { font-size: 1rem; }
h6 { font-size: 0.875rem; }
p { font-size: 1rem; }
small { font-size: 0.875rem; }
// On a large screen
// Font size on the body with rem
// set on elements will increase in size
@media only screen and (min-width: 64em) {
body {
font-size: 18px;
}
}
Highest font families – 31
It is recommended to have two fonts: one for the body and one for headings. Perhaps other fonts are used to support different languages?
$copy: SF UI Text, Arial, sans-serif;
$heading: SF UI Display, Arial, sans-serif;
body { font-family: $copy; }
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: $heading;
}
Highest specificity score – 512
Frameworks are as low as 50. Too many declarations can lead to performance issues and bugs.
Some sites graph is very spikey at the beginning meaning a bigger hit on the site load when the user first visits.
To style an element that requires unique styling such as a sign up link, there are many ways to target it.
<nav>
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Products</a></li>
<li><a class="btn" href="#">Sign Up</a></li>
</ul>
</nav>
// A. You could do this
nav ul li a.btn { }
// B. or simply
.btn { }
According to the specificity war powers, option A would be Vader:
A:
141+1+1+1+10
B:
1010
Points
- (1) Element Selector
- (10) Class Selector
- (100) ID Selector
Another good reason to use classes over ID’s
Highest media queries – 74
320 and up looks at 4 sizes on average. To cater for every screen is counter intuitive. New devices and screens will emerge, then you have to code for another screen.
Take reference from Bruce Lee “be like water my friend”.
$mw-s: 320px;
$mw-m: 768px;
$mw-l: 1024px;
$mw-xl: 1382px;
// Mobile
@media screen and (min-width: $mw-s) {
}
// Tablets
@media screen and (min-width: $mw-m) {
}
// iPads
@media screen and (min-width: $mw-l) {
}
// Powerbooks
@media screen and (min-width: $mw-xl) {
}
Highest style links – 17
More external stylesheets will mean more http requests, so page speed is effected. Good for maintenance but bad for performance.
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/type.css">
<link rel="stylesheet" href="css/grid.css">
<link rel="stylesheet" href="css/colors.css">
<link rel="stylesheet" href="css/modules.css">
Tools like Sass and Gulp can combine all stylesheets into one.
Example folder structure:
css/
node_modules/
sass/
gulpfile.js
index.html
package.json
_base.scss
_type.scss
_grid.scss
_colors.scss
_modules.scss
styles.scss
// 1. Use gulpjs to get all your sass partials
gulp.task('workflow', function () {
gulp.src('sass/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(cssnano())
.pipe(sourcemaps.write('./'))
// 2. Output to a single css file
.pipe(gulp.dest('css/'))
.pipe(browserSync.reload({stream:true}));
});
<!-- Voilà! -->
<link rel="stylesheet" href="css/styles.css">
Case study - Unsplash
Resources
Did I mention all these tools are free? Rather than reinvent the wheel everytime, some developers and designers have done some amazing things. Good to default from these tools. Collaboration is key.
Articles
- Top five mistakes of massive CSS—Nicole Sullivan
- Optimize CSS delivery—Patrick Sexton
- The specificity graph—Harry Roberts
- CSS and scalability—Adam Morse
- Rethinking variables in CSS—Brent Jackson
- Falling With Style - feat. Jon Gold—Does not compute
Talks
- Daniel Eden: Move Slow and Fix Things—dotconferences
- Adam Morse: Things I’ve Learned About CSS—CSSConf Oakland 2014
- Harry Roberts: Managing CSS Projects with ITCSS—DaFED