object oriented css for rapid, scalable and maintainable development

55
Object Oriented CSS

Upload: graeme-blackwood

Post on 27-Jan-2015

111 views

Category:

Design


1 download

DESCRIPTION

Most frontend developers have already heard of Object Oriented CSS, yet it is still sorely under-practiced. Do your CSS files have overrides and !importants scattered through them? Do they often come in at 3000 lines+ for a regular site? Tied with good semantic HTML, the Object Oriented CSS approach really is the answer to rapid, lightweight, scalable and easy to maintain frontend code. OOCSS will speed up any front end build, but really comes into its own on bigger sites. I will talk through my approach to frontend builds and show how OOCSS makes the process so much easier. I'll also talk a bit about CSS compilers and how it is important to have a good understanding of OOCSS methods before jumping into SASS or LESS. Poor CSS practice isn't going to be rescued by a compiler; in fact it can even compound problems by bringing in another layer to debug. I hope to instill a real understanding of what OOCSS means, why it REALLY matters, and how it's going to make you a better front end developer! @graemeblackwood http://www.deeson-online.co.uk/graeme-blackwood Presented at Frontend United – http://frontendunited.org

TRANSCRIPT

Page 1: Object Oriented CSS for rapid, scalable and maintainable development

Object Oriented CSS

Page 2: Object Oriented CSS for rapid, scalable and maintainable development

Graeme Blackwood Creative Lead, Deeson Online

deeson-online.co.uk @graemeblackwood

Page 3: Object Oriented CSS for rapid, scalable and maintainable development

Props•Nicole Sullivan, http://www.stubbornella.org

•Jonathan Snook (SMACSS), http://smacss.com

•Everyone at http://thesassway.com

•Chris Eppstein, http://compass-style.org

•Kevin Poulton http://www.flickr.com/photos/kevinpoulton

•And loads more!

Page 4: Object Oriented CSS for rapid, scalable and maintainable development

Before we get going

Page 5: Object Oriented CSS for rapid, scalable and maintainable development

OO CSS !=OO Programming

But an OOP mindset can be applied to CSS

Page 6: Object Oriented CSS for rapid, scalable and maintainable development

OOCSS does not require any newer technologies

It works in IE6 – no need for CSS3, HTML5 etc

Page 7: Object Oriented CSS for rapid, scalable and maintainable development

Semantic markup is very helpful, but usually not essential

Semantic is better though!

Page 8: Object Oriented CSS for rapid, scalable and maintainable development

The traditional approach to CSS

Page 9: Object Oriented CSS for rapid, scalable and maintainable development

Style output as needed Each item / page individually...

Page 10: Object Oriented CSS for rapid, scalable and maintainable development

Target styles by drilling down through nested elements

Page 11: Object Oriented CSS for rapid, scalable and maintainable development

Problems with traditional CSS

Page 12: Object Oriented CSS for rapid, scalable and maintainable development

Coding takes a lot longer

Page 13: Object Oriented CSS for rapid, scalable and maintainable development

It gets bloated

Page 14: Object Oriented CSS for rapid, scalable and maintainable development

It is difficult to read and hard to reuse

Page 15: Object Oriented CSS for rapid, scalable and maintainable development

Traditional CSS is tough to maintain

Page 16: Object Oriented CSS for rapid, scalable and maintainable development

Page 17: Object Oriented CSS for rapid, scalable and maintainable development

Object Oriented CSSA new way of thinking

Page 18: Object Oriented CSS for rapid, scalable and maintainable development

Separate layout from theme(Use a grid system!)

Page 19: Object Oriented CSS for rapid, scalable and maintainable development

Separate the container from the content

Page 20: Object Oriented CSS for rapid, scalable and maintainable development

Look for shared stylesYou will see them everywhere

Page 21: Object Oriented CSS for rapid, scalable and maintainable development

Why should I care?

Page 22: Object Oriented CSS for rapid, scalable and maintainable development

Reusable code

Page 23: Object Oriented CSS for rapid, scalable and maintainable development
Page 24: Object Oriented CSS for rapid, scalable and maintainable development

Faster development

Less effort to code

Page 25: Object Oriented CSS for rapid, scalable and maintainable development

Easier to maintain

Page 26: Object Oriented CSS for rapid, scalable and maintainable development
Page 27: Object Oriented CSS for rapid, scalable and maintainable development
Page 28: Object Oriented CSS for rapid, scalable and maintainable development

Less code,smaller files

Page 29: Object Oriented CSS for rapid, scalable and maintainable development

Getting started

Page 30: Object Oriented CSS for rapid, scalable and maintainable development

Using a grid system?You are already on the right track!

Page 31: Object Oriented CSS for rapid, scalable and maintainable development

Use a reset stylesheetHTML5 boilerplate’s normalize.css

Page 32: Object Oriented CSS for rapid, scalable and maintainable development

Set good base stylesTo minimise later overrides

* {box-sizing: border-box} is pretty handy

Page 33: Object Oriented CSS for rapid, scalable and maintainable development

Set good base stylesTo minimise later overridesBuild HTML prototypeswith Drupal in mind...

Page 34: Object Oriented CSS for rapid, scalable and maintainable development

Clean up what Drupal throws at youDon’t just style,theme!

Page 35: Object Oriented CSS for rapid, scalable and maintainable development

Lose unnecessary markup if it affects you

Some divs are totally pointless!

Page 36: Object Oriented CSS for rapid, scalable and maintainable development

Don’t be too obsessed with perfect markup

Just enough is ok

Page 37: Object Oriented CSS for rapid, scalable and maintainable development

“Now, this is just a simulation of what the blocks will look like once they’re assembled”

Page 38: Object Oriented CSS for rapid, scalable and maintainable development

Let’s look at some real world applications...

Page 39: Object Oriented CSS for rapid, scalable and maintainable development

.block = inherited base styles only

.grid-4 = grid class

.theme-a = theme class, H3 and p inherit colour

.padded is a class I add to the grid framework for situations like this

Page 40: Object Oriented CSS for rapid, scalable and maintainable development

<div class=”grid-4 theme-a block”> <h3>Block title</h3> <p>Hello world!</p></div>

.grid-4 comes from your grid framework

.theme-a { background-color: #580174; border-radius: 6px; color: #fff; //H3 and p inherit this}

Page 41: Object Oriented CSS for rapid, scalable and maintainable development

Carousel nav tabsObject is a Drupal menu – UL with a class of .sidebar-menu. Custom jQuery targets the ID.

Page 42: Object Oriented CSS for rapid, scalable and maintainable development

Also used on a listingAnd it flexes to the container

Page 43: Object Oriented CSS for rapid, scalable and maintainable development

99% of the styles are reused, targeted through .right .sidebar-menu only to flip the active item

Page 44: Object Oriented CSS for rapid, scalable and maintainable development

Carousel itemsWith a class of .product-box

Page 45: Object Oriented CSS for rapid, scalable and maintainable development

Reused on listing pages

Page 46: Object Oriented CSS for rapid, scalable and maintainable development

Same layout as product listings and carousel, flexed to image size

Page 47: Object Oriented CSS for rapid, scalable and maintainable development

What aboutSASS and LESS?Why bother with presentational classes in markup anymore?

Page 48: Object Oriented CSS for rapid, scalable and maintainable development

Not everyone wants to use a CSS preprocessorAnd the mindset of reduce, reuse & recycle is just as important even with one

Page 49: Object Oriented CSS for rapid, scalable and maintainable development

Use SASS wisely – it’s another layer to debug

Page 51: Object Oriented CSS for rapid, scalable and maintainable development
Page 52: Object Oriented CSS for rapid, scalable and maintainable development

Disregarding the other principles of OOCSS may create pain laterYou could end up with generated CSS bloated even more than if you had hand-coded it

Page 53: Object Oriented CSS for rapid, scalable and maintainable development

Summary•OOCSS is as much a mindset as a technique

•The principles are to reduce, reuse and recycle code

•This is done through separating layout from theme

•and the container from the content

•It relies primarily on reused classes and markup

•We are working on Drupal 8 to make it much easier to change markup. In the meantime, try Display Suite or Skinr for node theming

Page 54: Object Oriented CSS for rapid, scalable and maintainable development

Summary

•CSS preprocessors like SASS and LESS are great

•But they create another layer to debug

•And the code they generate can be very bloated

•Preprocessors are not an excuse to be lazy

•They still need to be used with an OO mindset

Page 55: Object Oriented CSS for rapid, scalable and maintainable development

@graemeblackwood

Questions?

Thanks for listening!

Most of the fantastic lego images were by Kevin Poulton http://www.flickr.com/photos/kevinpoulton