make your css beautiful again

34
{ Make your CSS beautiful again Refactoring without incurring a testing overhead

Upload: spannerspace-hales

Post on 26-Jun-2015

533 views

Category:

Technology


0 download

DESCRIPTION

This is the first of three presentations detailing the front end optimization of goodtoknow.co.uk. Part one describes how we refactored the CSS, part two will address the HTML structure and implementation of Behat tests, and part three will contend with image optimization specifically addressing data URI’s and sprites. Full notes here: http://spannerspace.com/make-your-css-beautiful-again/

TRANSCRIPT

Page 1: Make your CSS beautiful again

{

Make your CSS beautiful again

Refactoring without incurring a testing overhead

Page 2: Make your CSS beautiful again

Goodtoknow has more than 36 stylesheets.

gtkMain.css

gtkHeader.css

base.css

galleryCarousel.css

bauOverride.cssgtkFooter.css

rightRail.css

gtkFacebook.css

brightcove.css

tabbedBox.css

recipeArticle.css

atoz.css

videos.csssubmitPic.css

comments.css

pluck.cssgalleryCarousel.css

recipes.css

gtkMain.css

gtkHeader.css

base.cssgalleryCarousel.c

ss

bauOverride.cssgtkFooter.css

rightRail.css

gtkFacebook.css

brightcove.css

tabbedBox.css

recipeArticle.css

atoz.css

videos.css submitPic.css

comments.csspluck.cssgalleryCarousel.css

recipes.css

Page 3: Make your CSS beautiful again

Each page uses approx. 10 of those stylesheets at a combined weight of around 280k

The average page only uses 50% of the CSS served

At an average of 1500 CSS rules per page, around 300 are inefficient and 150 are very inefficient.

The average page speed score is 82/100

Page 4: Make your CSS beautiful again

{ Its time to make our CSS

beautiful again*

Page 5: Make your CSS beautiful again

*without exhausting our test analysts

Page 6: Make your CSS beautiful again

You need Cactus!https://github.com/winston/cactus

Page 7: Make your CSS beautiful again

CSS tests are simply a baseline representation of your style guide.

<shame>Don’t have one? Neither did we!</shame>

By writing CSS tests you can refactor with confidence, whilst building & reinforce your style guide

Page 8: Make your CSS beautiful again

+ =

Page 9: Make your CSS beautiful again
Page 10: Make your CSS beautiful again

Link to jQuery Add the cactus.js file Write some tests

Page 11: Make your CSS beautiful again

Cactus.expect(".header", "font-size").toEqual("24px");Cactus.expect(".header", "font-family").toContain("Helvetica");Cactus.expect(".header", "color").toEqual("#ff0000");Cactus.expect(".header").toHaveMargin("10px");Cactus.expect(".header").toHaveBorderWidth("1px");Cactus.expect(".header").toHaveBorderColor("#ff000");Cactus.expect(".header“, “float”).toContain(“left");

Page 12: Make your CSS beautiful again
Page 13: Make your CSS beautiful again

This page needs a screengrab of our yml file

Page 14: Make your CSS beautiful again

<p class=“heading”><h3 class=“smaller”><h2 class=“bigger”>

Page 15: Make your CSS beautiful again

YOU CANNOT BE SERIOUS!

Page 16: Make your CSS beautiful again

Refactor with confidence Ensures code quality Reduce testing overhead Build a style guide Promotes semantic markup Reduce CSS file size Reduce page load times And it can even be automated (but we’re not doing this just yet!)

Why use cactus?

Page 17: Make your CSS beautiful again

More?

Page 18: Make your CSS beautiful again

Syntactically Awesome Stylesheets

http://sass-lang.com/

Page 19: Make your CSS beautiful again

“Sass is an extension of CSS3 which adds nested rules, variables, mixins, selector inheritance, and more. Sass generates well formatted CSS and makes your stylesheets easier to organize and maintain.”

Page 20: Make your CSS beautiful again

gem install sass mv style.css style.scss sass --watch web/sass-folder:web/css-

folder

now write some Sassy CSS…

Page 21: Make your CSS beautiful again

Nesting

CSS

SCSS

Page 22: Make your CSS beautiful again

This is the CSS generated by Sass for our nested SCSS

4 out of the 5 rules are inefficient

Page 23: Make your CSS beautiful again
Page 24: Make your CSS beautiful again

Nesting without creating inefficient selectors

:( :)

Page 25: Make your CSS beautiful again

Sass allows you to declare variables that can be used throughout your stylesheet. Variables begin with $ and are declared just like properties. They can have any value that’s allowed for a CSS property, such as colors, numbers (with units), or text.

Variables

Page 26: Make your CSS beautiful again

Selector inheritance

Page 27: Make your CSS beautiful again

http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html

Operations, Functions & Interpolation

Page 28: Make your CSS beautiful again

Mixins

.pagination .next,

.pagination .previous {@include button;

}

Page 29: Make your CSS beautiful again
Page 30: Make your CSS beautiful again

example:

CSS old SASS CSS new

0

50

100

150

200

250

300

350

400

450

Tabbed box css Channel cssTotal rules

Page 31: Make your CSS beautiful again

Nesting can create inefficient selectors Generated CSS file can be slightly more

inflated if used carelessly Be cautious that you’re not duplicating

variables and mixins

Reasons to be cautious:

Page 32: Make your CSS beautiful again

Reasons you love Sass: Build and easily maintain a style guide SCSS file is more concise Variables and mixins prevent careless errors Stacks of great functions available Error handling for poor code Easier to maintain Use the math functions for responsive builds Now your CSS is beautiful again!

Page 33: Make your CSS beautiful again

footnotes

Page 34: Make your CSS beautiful again