Top Banner
CSS oocss, smacss & more...
169

CSS - OOCSS, SMACSS and more

Sep 08, 2014

Download

Technology

Russ Weakley

An introduction to the changing world of CSS. Slides from Brisbane Web Designer Meetup 13 March 2013.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSS - OOCSS, SMACSS and more

CSSoocss, smacss & more...

Page 2: CSS - OOCSS, SMACSS and more

what’s wrong with

CSS

Page 3: CSS - OOCSS, SMACSS and more

CSS is simple... It’s simple to

understand. But CSS is not

simple to use or maintain.

”http://chriseppstein.github.com/blog/2009/09/20/why-stylesheet-abstraction-matters/

Page 4: CSS - OOCSS, SMACSS and more

If you’ve ever worked on a medium to large website, you have probably come across a range of issues with CSS.

Issues

Page 5: CSS - OOCSS, SMACSS and more

Lots of floats, font-size references, heading levels repeated...

Repetition

Page 6: CSS - OOCSS, SMACSS and more

It may have started out well, but it has become a mess?

Unmanageable

Page 7: CSS - OOCSS, SMACSS and more

You need to add some CSS - so you have to start adding to selectors in order to win.

Weight war

Page 8: CSS - OOCSS, SMACSS and more

Your CSS is specifically tied to HTML or location...

Coupling

Page 9: CSS - OOCSS, SMACSS and more

Developers have been telling us for years that “CSS sucks”. We all know it’s time for our CSS practices to evolve.

CSS sucks!

Page 10: CSS - OOCSS, SMACSS and more

Luckily, there are a wide range of exciting new developments to explore.

New stuff!

Page 11: CSS - OOCSS, SMACSS and more

Before we start... put aside any biases. At least until after the presentation, when you can rip into me :)

Biases aside...

Page 12: CSS - OOCSS, SMACSS and more

oocssobject oriented css

Page 13: CSS - OOCSS, SMACSS and more

What is OOCSS?

Page 14: CSS - OOCSS, SMACSS and more

In 2009, Nicole Sullivan introduced a new way of looking at CSS. She defined this as Object Oriented CSS (OOCSS).

Page 15: CSS - OOCSS, SMACSS and more

After optimising Facebook’s CSS, she discovered some amazing statistics... about how we reapply CSS properties throughout our style sheets.

Statistics

Page 16: CSS - OOCSS, SMACSS and more

Facebook:Facebook blueUnique colors

colors

Salesforce:padding

h1-h6

261548

6,498

3,668511

Page 17: CSS - OOCSS, SMACSS and more

“We have been doing it all

wrong.... Our best practices are

killing us”

Nicole Sullivan

Page 18: CSS - OOCSS, SMACSS and more

The purpose of OOCSS is to

encourage code reuse and,

ultimately, faster and more

efficient stylesheets that are

easier to add to and maintain.

http://coding.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/

Page 19: CSS - OOCSS, SMACSS and more

Looking for patterns - rows

Page 20: CSS - OOCSS, SMACSS and more

One of the key aims of OOCSS is to abstract as many components of the layout as possible.

Abstraction

Page 21: CSS - OOCSS, SMACSS and more

Can you see any repeating visual patterns?

Example

Page 22: CSS - OOCSS, SMACSS and more
Page 23: CSS - OOCSS, SMACSS and more
Page 24: CSS - OOCSS, SMACSS and more

The first layout pattern could be the rows.

Rows

Page 25: CSS - OOCSS, SMACSS and more

row

row

row

row

Page 26: CSS - OOCSS, SMACSS and more

row

row

row

row

Page 27: CSS - OOCSS, SMACSS and more

In the past, people may have styled these rows using a series of IDs.

Past practice

Page 28: CSS - OOCSS, SMACSS and more

#header

#main

#news

#footer

Page 29: CSS - OOCSS, SMACSS and more

1. use a single class so that it can be reused as needed2. abstract the module down to core purposes

OOCSS aims

Page 30: CSS - OOCSS, SMACSS and more

These rows have two purposes:1. clear each row2. trigger the block formatting context.

Core purposes

Page 31: CSS - OOCSS, SMACSS and more

.row

.row

.row

.row

Page 32: CSS - OOCSS, SMACSS and more

Then we can write one simple, but very powerful class that can be reused anywhere in the layout.

Re-use

Page 33: CSS - OOCSS, SMACSS and more

.row {! clear: left;! overflow: hidden;! zoom: 1;! _overflow: visible;}

IE6 underscore hack

Triggers haslayout in IE5-7

Triggers block formatting

Clears each row

Page 34: CSS - OOCSS, SMACSS and more

Different rows

Page 35: CSS - OOCSS, SMACSS and more

Did you notice that two of the rows were different? They have different background colours and additional padding above and below.

Different rows

Page 36: CSS - OOCSS, SMACSS and more
Page 37: CSS - OOCSS, SMACSS and more

We could now add some classes based on the purpose of these rows - such as “news” and “footer”.

New names?

Page 38: CSS - OOCSS, SMACSS and more

.news {! padding: 1em 0;! background-color: blue;!}

.footer {! padding: 1em 0;! background-color: pink;!}

Page 39: CSS - OOCSS, SMACSS and more

However, it would be better to abstract these names further so that they are more flexible.

Abstract

Page 40: CSS - OOCSS, SMACSS and more

.row {! clear: left;! overflow: hidden;! zoom: 1;! _overflow: visible;}

.row-alt1 {! padding: 1em 0;! background-color: blue;!}

.row-alt2 {! padding: 1em 0;! background-color: pink;!}

Page 41: CSS - OOCSS, SMACSS and more

<div class="row"></div><div class="row"></div><div class="row row-alt1"></div><div class="row row-alt2"></div>

Page 42: CSS - OOCSS, SMACSS and more

If you wanted, these could be abstracted even further into padding and backgrounds as separate concepts.

Further

Page 43: CSS - OOCSS, SMACSS and more

.row-padding {! padding: 1em 0;}

.bg-color1 {! background-color: blue;!}

.bg-color2 {! background-color: pink;!}

Page 44: CSS - OOCSS, SMACSS and more

<div class="row"></div><div class="row"></div><div class="row row-paddingbg-color1"></div><div class="row row-paddingbg-color2"></div>

Page 45: CSS - OOCSS, SMACSS and more

It depends on the site and circumstances as to how far you think you need to abstract these concepts.

Up to you

Page 46: CSS - OOCSS, SMACSS and more

The row module

Page 47: CSS - OOCSS, SMACSS and more

The “row” class is our primary module. The additional classes are “modifiers” as they modify the primary class.

Primary module

Page 48: CSS - OOCSS, SMACSS and more

Modifiers should not rewrite any aspect of the primary module, they only modify or add to the primary module.

Modifiers

Page 49: CSS - OOCSS, SMACSS and more

Primary module

.row-alt1

Sub-module Modifier

.row-alt1

Types of class

Page 50: CSS - OOCSS, SMACSS and more

Looking for patterns - columns

Page 51: CSS - OOCSS, SMACSS and more

The second layout pattern could be the columns. The wide layout looks like it has four columns.

Columns

Page 52: CSS - OOCSS, SMACSS and more

Column 1 Column 2 Column 4Column 3

Page 53: CSS - OOCSS, SMACSS and more

Some of the rows spread across all columns. Others spread across two or one column.

Patterns

Page 54: CSS - OOCSS, SMACSS and more

4 columns

2 columns

2 columns

1 columns

2 columns

2 columns

1 columns 1 columns1 columns

Column 1 Column 2 Column 4Column 3

Page 55: CSS - OOCSS, SMACSS and more

To be safe, we should assume we need containers for 4, 3, 2 and 1 column widths. We can convert these column options into a simple grid framework.

Framework 1

Page 56: CSS - OOCSS, SMACSS and more

Wide layout4 column box 3 column box2 column box1 column box

Class names.w-4col .w-3col .w-2col .w-1col

Page 57: CSS - OOCSS, SMACSS and more

<div class="row">! <div class="w-4col"></div></div><div class="row">! <div class="w-2col"></div>! <div class="w-2col"></div></div><div class="row row-alt1">! <div class="w-2col"></div>! <div class="w-2col"></div></div><div class="row row-alt2">! <div class="w-1col"></div>! <div class="w-1col"></div>! <div class="w-1col"></div>! <div class="w-1col"></div></div>

Page 58: CSS - OOCSS, SMACSS and more

The same is true of the narrow layout, except this time it has only two overall columns.

Narrow

Page 59: CSS - OOCSS, SMACSS and more

Column 1 Column 2

Page 60: CSS - OOCSS, SMACSS and more

2 columns

1 columns 1 columns

1 columns 1 columns

1 columns 1 columns

Page 61: CSS - OOCSS, SMACSS and more

We could also create a second, different grid for narrow screen. This would allow us to control whether columns sat beside each other or below at a narrower screen size.

Framework 2

Page 62: CSS - OOCSS, SMACSS and more

Narrow layout2 column box 1 column box

Class names.n-2col .n-2col

Page 63: CSS - OOCSS, SMACSS and more

<div class="row">! <div class="w-4col"></div></div><div class="row">! <div class="w-2col n-2col"></div>! <div class="w-2col n-2col"></div></div><div class="row row-alt1">! <div class="w-2col n-2col"></div>! <div class="w-2col n-2col"></div></div><div class="row row-alt2">! <div class="w-1col n-2col"></div>! <div class="w-1col n-2col"></div>! <div class="w-1col n-2col"></div>! <div class="w-1col n-2col"></div></div>

Page 64: CSS - OOCSS, SMACSS and more

With these two simple grids, we can control complicated layouts - both wide and narrow.

Control!

Page 65: CSS - OOCSS, SMACSS and more

Looking for patterns - boxes

Page 66: CSS - OOCSS, SMACSS and more

You may have noticed that there were also a series of smaller boxes, each with an image to the left or right.

Boxes?

Page 67: CSS - OOCSS, SMACSS and more
Page 68: CSS - OOCSS, SMACSS and more

1. contain content2. feature object to left or right3. content beside feature object4. margin below

Core purpose

Page 69: CSS - OOCSS, SMACSS and more

Adaptable boxWe need to create an adaptable box:- could be placed anywhere- any width or height- any feature content- feature content could be left/right- any content inside the body

Page 70: CSS - OOCSS, SMACSS and more

We need to be able to target - the overall box- the object (left or right)- the body content within the box- a possible heading (h1-h6)- possibly even the contents itself

Target

Page 71: CSS - OOCSS, SMACSS and more

This is a headingLorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut.

box

box feature

box bodybox heading

box content

Page 72: CSS - OOCSS, SMACSS and more

.box { }

.box-feature { }

.box-feature-alt { }

.box-body { }

.box-heading { }

.box-content { }

There are a range of possible class we could use here.

Page 73: CSS - OOCSS, SMACSS and more

Do not give the box a width - allow it to spread to fit the width of any parent container.

Width

Page 74: CSS - OOCSS, SMACSS and more

This box module must contain (and therefore wrap around) either a left or right floating object. We can solve this by triggering the block formatting context on the parent.

Contain floats

Page 75: CSS - OOCSS, SMACSS and more

.box {! overflow: hidden;! zoom: 1;! _overflow: visible;! margin-bottom: 1em;}

Triggers block formatting

Page 76: CSS - OOCSS, SMACSS and more

The box must work when placed anywhere within the layout. The box must be “location agnostic”.

Location Agnostic

aside .box { }.box { }

Page 77: CSS - OOCSS, SMACSS and more

The box may contain objects that have varying widths. We need our content box (“box-body”) to sit beside these objects, regardless of their widths.

Sit beside

Page 78: CSS - OOCSS, SMACSS and more

We can solve this by triggering the block formatting context on the “box-body” class.

BFC again

Page 79: CSS - OOCSS, SMACSS and more

This is a headingLorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut.

box-body

Page 80: CSS - OOCSS, SMACSS and more

.box-body {! overflow: hidden;! zoom: 1;! _overflow: visible;}

Triggers block formatting

Page 81: CSS - OOCSS, SMACSS and more

The box module

Page 82: CSS - OOCSS, SMACSS and more

We have just made a very powerful box. Nicole Sullivan refers to this box as the “media” element.

Powerful box

Page 83: CSS - OOCSS, SMACSS and more

.box,.box-body {! overflow: hidden;! zoom: 1;! _overflow: visible;}

.box { margin: 0 0 10px; }

.box-feature {! float: left;! margin: 0 10px 0 0;}

.box-feature-alt {! float: right;! margin: 0 0 0 10px;}

Page 84: CSS - OOCSS, SMACSS and more

In this case, the “box” class is our primary module. There are no modifiers, but there are a range of sub-modules.

Primary module

Page 85: CSS - OOCSS, SMACSS and more

Sub-modules are other classes associated with the primary module. They do not alter or add directly to the primary module.

Sub-modules

Page 86: CSS - OOCSS, SMACSS and more

Types of classPrimary module

.row-alt1

.box

Sub-module

.box-feature

.box-body

Modifier

.row-alt1

Page 87: CSS - OOCSS, SMACSS and more

Moving forward

Page 88: CSS - OOCSS, SMACSS and more

For years, we have been taught to keep our markup clean and only use “semantic” class names.

Semantic classes

Page 89: CSS - OOCSS, SMACSS and more

OOCSS seems to break both of these rules - and in a big way. But have we been thinking about “semantic” class names in the wrong way?

Break the rules?

Page 90: CSS - OOCSS, SMACSS and more

HTML class names offer no

semantic value to search engines

or screen readers, aside from

microformats.

http://www.brettjankord.com/2013/02/09/thoughts-on-semantic-html-class-names-and-maintainability/

Page 91: CSS - OOCSS, SMACSS and more

Rather than concerning

ourselves with creating semantic

class names, I think we should be

thinking about creating sensible

class names. Sensible class

names offer semantics, but they

also offer flexibility/reusability.

http://www.brettjankord.com/2013/02/09/thoughts-on-semantic-html-class-names-and-maintainability/

Page 92: CSS - OOCSS, SMACSS and more

If your class is called “blue” and

you want to change it to red, you

have far bigger problems than

class names to deal with!

https://speakerdeck.com/andyhume/css-for-grown-ups-maturing-best-practises

Page 93: CSS - OOCSS, SMACSS and more

In order to move forward, especially on large scale sites, we cannot keep using old practices.

Move forward

Page 94: CSS - OOCSS, SMACSS and more

OOCSS offers front end developers an alternative - light weight, modular CSS that can be re-used repeatedly across sites.

Solution?

Page 95: CSS - OOCSS, SMACSS and more

smacsscss architecture

Page 96: CSS - OOCSS, SMACSS and more

What is SMACSS?

Page 97: CSS - OOCSS, SMACSS and more

SMACSS is more style guide than

rigid framework - an attempt to

document a consistent approach

to site development when using

CSS.

http://alistapart.com/article/frameworksfordesigners

Page 98: CSS - OOCSS, SMACSS and more

In 2011, Jonathan Snook introduced a new way of looking at CSS architecture. He called this Scalable and Modular Architecture for CSS (SMACSS)

Page 99: CSS - OOCSS, SMACSS and more

Categorization

Page 100: CSS - OOCSS, SMACSS and more

Base rulesLayout rulesModule (and sub-module) rulesState rulesTheme rules

Categories

Page 101: CSS - OOCSS, SMACSS and more

Base rules are the defaults. They are almost exclusively single element selectors.

Base

Page 102: CSS - OOCSS, SMACSS and more

Layout rules divide the page into sections. Layouts hold one or more modules together.

Layout

Page 103: CSS - OOCSS, SMACSS and more

Modules are the reusable, modular parts of our design. They are the callouts, the sidebar sections, the product lists and so on.

Modules

Page 104: CSS - OOCSS, SMACSS and more

SMACSS allows for primary modules, modifiers and sub-modules, though they are labelled slightly differently.

Page 105: CSS - OOCSS, SMACSS and more

Primary module

.row-alt1

.box

Sub-moduleSub-component

.box-feature

.box-body

ModifierSub-module.row-alt1

Page 106: CSS - OOCSS, SMACSS and more

State rules are ways to describe how our modules or layouts will look when in a particular state. Is it hidden or expanded?

States

Page 107: CSS - OOCSS, SMACSS and more

Theme rules describe how the layout or modules might look.

Themes

Page 108: CSS - OOCSS, SMACSS and more

Naming Convention

Page 109: CSS - OOCSS, SMACSS and more

Use classes rather than IDs for styling purposes. Classes are more flexible. Using classes can reduce specificity issues.

Avoid IDs

Page 110: CSS - OOCSS, SMACSS and more

Class names should be meaningful for other authors, so that other developers can understand their purpose.

Meaningful

Page 111: CSS - OOCSS, SMACSS and more

Class names should follow understandable patterns.

Pattern

Page 112: CSS - OOCSS, SMACSS and more

Use “pseudo-namespaces” as prefixes - so that modules, modifiers and sub-modules can be identified.

Prefixes

Page 113: CSS - OOCSS, SMACSS and more

Possibly use different naming conventions for modifiers, sub-modules and states.

.example-widget { }

.example-widget--modifier { }

.example-widget__sub-module { }

.example-widget--is-somestate { }

Modifiers

Page 114: CSS - OOCSS, SMACSS and more

Decouple HTML/CSS

Page 115: CSS - OOCSS, SMACSS and more

I’ve noticed that designers

traditionally write CSS that is

deeply tied to the HTML that it is

designed to style. How do we

begin to decouple the two for

more flexible development with

less refactoring?

http://coding.smashingmagazine.com/2012/04/20/decoupling-html-from-css/

Page 116: CSS - OOCSS, SMACSS and more

So how do we “decouple” our HTML and CSS.1. using additional class names2. using child selectors

Decouple

Page 117: CSS - OOCSS, SMACSS and more

To see this in action, let’s look at the “box” example from earlier. What if we wanted to style the heading inside the “box-body”.

Example

Page 118: CSS - OOCSS, SMACSS and more

This is a headingLorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut.

heading

Page 119: CSS - OOCSS, SMACSS and more

We could style this heading using something like this:

.box { }

.box h2 { }

Style the h2?

Page 120: CSS - OOCSS, SMACSS and more

<div class="box">! <img class="box-feature"! src="dummy-140.png" alt="">! <div class="box-body">! ! <h2>Heading</h2>! ! </p>Lorem ipsum dolor</p>! </div></div>

Page 121: CSS - OOCSS, SMACSS and more

The problem is that the CSS is “coupled” with the HTML. What happens if there is an <h3> element inside the box?

Problem?

Page 122: CSS - OOCSS, SMACSS and more

One solution would be to set all heading levels.

.box { }

.box h1 { }

.box h2 { }

.box h3 { }

.box h4 { }

.box h5 { }

.box h6 { }

Page 123: CSS - OOCSS, SMACSS and more

However, the safest way to “uncouple” the CSS and HTML would be to use a simple class.

Use a class

Page 124: CSS - OOCSS, SMACSS and more

<div class="box">! <img class="box-feature"! src="dummy-140.png" alt="">! <div class="box-body">! ! <h2 class="box-heading">! ! ! Heading</h2>! ! </p>Lorem ipsum dolor</p>! </div></div>

Page 125: CSS - OOCSS, SMACSS and more

Now our HTML and CSS are more flexible. It doesn’t matter what heading level is used.

.box { }

.box-heading { }

More flexible

Page 126: CSS - OOCSS, SMACSS and more

modulesa closer look at

Page 127: CSS - OOCSS, SMACSS and more

The following “module” guidelines apply regardless of whether you are coming from OOCSS or SMACSS.

Guidelines

Page 128: CSS - OOCSS, SMACSS and more

Rule 1: keep modules simple

Page 129: CSS - OOCSS, SMACSS and more

By making your base objects this

simple your choices become

boolean; you use the object or

you don’t. The object is either

entirely suitable as a basis, or

entirely _un_suitable.

http://csswizardry.com/2012/06/the-open-closed-principle-applied-to-css/

Page 130: CSS - OOCSS, SMACSS and more

The base module should be defined as simply as possible. This means that they are highly flexible.

Keep ‘em simple

Page 131: CSS - OOCSS, SMACSS and more

Let’s use an example of our “row” class. What if we added some padding to this rule?

.row {! clear: left;! overflow: hidden;! padding: 20px 0;}

Page 132: CSS - OOCSS, SMACSS and more

But what if we want a row that doesn’t have padding? The problem is that this rule is now very specifically defined. It is therefore not as flexible.

Page 133: CSS - OOCSS, SMACSS and more

Rule 2: don’t undo styles

Page 134: CSS - OOCSS, SMACSS and more

Any CSS that unsets styles (apart

from in a reset) should start

ringing alarm bells... Rulesets

should only ever inherit and add

to previous ones, never undo.

http://csswizardry.com/2012/11/code-smells-in-css/

Page 135: CSS - OOCSS, SMACSS and more

Leading on from the first rule, you should avoid writing rules to undo a previous module.

Don’t undo

Page 136: CSS - OOCSS, SMACSS and more

For example, what if you wanted almost all of your headings to have a border-bottom?

h2 {! font-size: 1.5em! margin-bottom: 1em;! padding-bottom: 1em;! border-bottom: 1px solid red;}

Page 137: CSS - OOCSS, SMACSS and more

But in some cases you might want a heading without a border-bottom.

Page 138: CSS - OOCSS, SMACSS and more

You could write a new rule like this:

.no-border {! padding-bottom: 0;! border-bottom: none;}

Page 139: CSS - OOCSS, SMACSS and more

This is not ideal. It is much better to write sub-modules that add styles, rather than write sub-modules to undo styles.

Page 140: CSS - OOCSS, SMACSS and more

So, a better way might be to write two rules like this:

Page 141: CSS - OOCSS, SMACSS and more

/* default style */h2 {! font-size: 1.5em! margin-bottom: 1em;}

/* only when border needed */.headline {! padding-bottom: 1em;! border-bottom: 1px solid red;}

Page 142: CSS - OOCSS, SMACSS and more

Rule 3: extend but don’t

modify

Page 143: CSS - OOCSS, SMACSS and more

Base modules can be extended using sub-modules. However, the base module itself should never be modified.

Don’t modify

Page 144: CSS - OOCSS, SMACSS and more

This is based on the object oriented programming “open/close principle”.

Page 145: CSS - OOCSS, SMACSS and more

Software entities (classes,

modules, functions, etc.) should

be open for extension, but closed

for modification.

http://en.wikipedia.org/wiki/Open/closed_principle

Page 146: CSS - OOCSS, SMACSS and more

If a based module needs to be modified to suit a specific case, it is probably better to create a new module.

Page 147: CSS - OOCSS, SMACSS and more

Rule 4: think before adding

new modules

Page 148: CSS - OOCSS, SMACSS and more

It is always tempting to add a module based on your need at the time... as well as based on the needs of the system.

Don’t rush

Page 149: CSS - OOCSS, SMACSS and more

This often happens after the initial planning and build has been done. It’s easy to be tempted by “I’ll just drop this new class in at the bottom of my CSS”.

Page 150: CSS - OOCSS, SMACSS and more

However, adding poorly structured new modules, without rigorous abstraction, will lead to bloated, hard-to-manage CSS.

Page 151: CSS - OOCSS, SMACSS and more

practicescoding

Page 152: CSS - OOCSS, SMACSS and more

Comment conventions

Page 153: CSS - OOCSS, SMACSS and more

There is a growing trend to use the DocBlock as an overall comment convention. In fact, a movement around this type of commenting began over 6 years ago with the CSSdoc group

DocBlock

Page 154: CSS - OOCSS, SMACSS and more

"A DocBlock is an extended C++-

style PHP comment that begins

with "/**" and has an "*" at the

beginning of every line.

DocBlocks precede the element

they are documenting...

http://en.wikipedia.org/wiki/PHPDoc

Page 155: CSS - OOCSS, SMACSS and more

/** * Short desc * * Long description first sentence starts * and continues on this line for a while * finally concluding here at the end of * this paragraph * * The blank line above denotes a paragraph */

Page 156: CSS - OOCSS, SMACSS and more

Formatting CSS rules

Page 157: CSS - OOCSS, SMACSS and more

In the early days of CSS, a lot of developers preferred single line CSS rules as they could easily see the selectors.

Single line?

Page 158: CSS - OOCSS, SMACSS and more

Today, with the complexity of individual rules, most developers seem to prefer either the multi-line format, or multi-line with indenting format.

Multi-line

Page 159: CSS - OOCSS, SMACSS and more

Multi-line Format with IndentingMulti-line FormatSingle-line FormatSingle-line Format with Indenting/TabbingMostly Single-line FormatSingle-line Format with TabbingOther

44%28%11%5%5%4%3%

*CSS-tricks poll: http://css-tricks.com/different-ways-to-format-css/

CSS Tricks rule formatting poll

Page 160: CSS - OOCSS, SMACSS and more

.navigation_rss_icon {! position: absolute;! left: 940px;! bottom: 0px;}

#navigation_rss {! position: absolute;! left: 720px;! font-family: Verdana, Arial, Helvetica, sans-serif;! text-transform: uppercase;! color: #897567;! line-height: 2.5em;}

#navigation_rss li {! display: inline;}

#navigation_rss li a:link, #navigation_rss li a:visited {! color: #fffffe;! text-decoration: none;! padding: 0px 2px;! letter-spacing: -0.05em;}#navigation_rss li a:hover {! color: #eed2a1;! text-decoration: none;}

Page 161: CSS - OOCSS, SMACSS and more

.navigation_rss_icon { position: absolute; left: 940px; bottom: 0px;}

#navigation_rss { position: absolute; left: 720px; font-family: Verdana, Arial, Helvetica, sans-serif; text-transform: uppercase; color: #897567; line-height: 2.5em;}

#navigation_rss li { display: inline; }

#navigation_rss li a:link, #navigation_rss li a:visited { color: #fffffe; text-decoration: none; padding: 0px 2px; letter-spacing: -0.05em; }

Page 162: CSS - OOCSS, SMACSS and more

Declaration order

Page 163: CSS - OOCSS, SMACSS and more

Similarly, many developers used to prefer to sort their declarations alphabetically.

Alphabet?

Page 164: CSS - OOCSS, SMACSS and more

Today, most people prefer to group their declarations by type.

Group

Page 165: CSS - OOCSS, SMACSS and more

Grouped by type 45%Random - 39%Alphabet - 14%By line - 2%

45%39%14%2%

*CSS-tricks poll: http://css-tricks.com/different-ways-to-format-css/

CSS Tricks declaration formatting poll

Page 166: CSS - OOCSS, SMACSS and more

.selector { /* Positioning */ position: absolute; z-index: 10; top: 0; right: 0;

/* Display & Box Model */ display: inline-block; overflow: hidden; box-sizing: border-box; width: 100px; height: 100px; padding: 10px; border: 10px solid #333; margin: 10px;

/* Color */ background: #000; color: #fff /* Text */ font-family: sans-serif; font-size: 16px; line-height: 1.4; text-align: right;

/* Other */ cursor: pointer;}

Page 167: CSS - OOCSS, SMACSS and more

Of course, many tools and pre-processors take care of this for you. If your tools do not have this capability, take a look at CSS Combhttp://csscomb.com/

Page 168: CSS - OOCSS, SMACSS and more

get busy!

Page 169: CSS - OOCSS, SMACSS and more

Russ WeakleyMax Design

Site: maxdesign.com.auTwitter: twitter.com/russmaxdesignSlideshare: slideshare.net/maxdesignLinkedin: linkedin.com/in/russweakley