More of less (take 2)

42
More of {less} Guilherme Zühlke O’Connor

description

 

Transcript of More of less (take 2)

Page 1: More of less (take 2)

More of {less}Guilherme Zühlke O’Connor

Page 2: More of less (take 2)

The Author

Head of front end development at Snupps

Page 3: More of less (take 2)

Snupps

We are a B2C, cross platform app on the web and mobile that helps you organise your stuff.

We are an early stage startup.We are getting ready to launch.

Page 4: More of less (take 2)

The problem

When you get to a certain point in life, you

1. Have too much stuff2. Don’t know what you have3. Don’t know where it is4. Don’t have quick access to the details

Page 5: More of less (take 2)

The solution

Snupps is the one place for you to store and keep track of your stuff and have it at your fingertips at

all times.

Page 6: More of less (take 2)

Lots of it...Scattered around...Difficult to find…

So, stuff, huh?

Page 7: More of less (take 2)

Lots of it...Scattered around...Difficult to find…

Surely I’m not the only one who sees a resemblance with CSS!

So, stuff, huh?

Page 8: More of less (take 2)
Page 9: More of less (take 2)

Which begs the question...

Page 10: More of less (take 2)

Which begs the question...

How on Earth do we organise CSS?

Page 11: More of less (take 2)

The problem

Easy syntaxGentle learning curveConceptually minimalistic

”Cascading Style Sheets (CSS) is a simple mechanism

for adding style (e.g., fonts, colors, spacing) to Web documents.”

http://www.w3.org/Style/CSS/Overview.en.html

Page 12: More of less (take 2)

The problem

Easy syntaxGentle learning curveConceptually minimalistic

Also minimal support for software engineering

CC”Cascading Style Sheets (CSS) is a simple

mechanism for adding style (e.g., fonts, colors, spacing) to Web documents.”

hhttp://www.w3.org/Style/CSS/Overview.en.html

Page 13: More of less (take 2)

Simple enough

Page 14: More of less (take 2)

CSS is simple enough for a simple document, but today’s average page on a web app is an amalgamation of several heavily complex design elements.

Page 15: More of less (take 2)

Every element may itself depend on an amalgamation of different techniques, glued together for the overall effect.

Page 16: More of less (take 2)

Each element on the page may be used more than once. Maybe on different pages. Maybe variations of its basic form.

Page 17: More of less (take 2)

Oh, did I just say Web App instead of Web document?

Page 18: More of less (take 2)

Oh, did I just say Web App instead of Web document?

Oops...

Page 19: More of less (take 2)

Compromises

Invariably, writing enough CSS for a website will require that at least one of the following is compromised

DRY (Don’t Repeat Yourself)CSS ModularizationSeparation of Concerns (classitis)

Page 20: More of less (take 2)

Repetition

button.addComment {

color: #ccc;

background: #333;

padding: 10px;

}

button.search {

color: #ccc;

background: #333;

padding: 10px;

}

Page 21: More of less (take 2)

Don’t Repeat Yourself

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system

Page 22: More of less (take 2)

Poor Grouping

button.addComment,button.search,button.login {

color: #ccc;background: #333;padding: 10px;

}

.comment .timestamp,

.notification .timestamp,

.creation .timestamp {color: #ccc;background: #333;padding: 10px;

}

Page 23: More of less (take 2)

DOM pollution and classitis.button-1 {

color: #ccc;background: #333;padding: 10px;

} <button class=”addComment button-1”>

<button class=”search button-1”>

<button class=”login button-1”>

Page 24: More of less (take 2)

Enter CSS preprocessing

Page 25: More of less (take 2)

A CSS preprocessor is an augmentation of the CSS language that can be run through a script to generate native CSS a browser can understand.

Page 26: More of less (take 2)

Why CSS preprocesing?

CSS preprocessing gives the developer tools to organize their code and create reusable components. It allows for the code to be grouped in semantic, logical components that can be defined elsewhere than where they are being used.

Page 27: More of less (take 2)

Variables@btnColor: #ea5430

button {background-color: @btnColor;

}

label {background-color: @btnColor;

}

button {background-color: #ea5430;

}

label {background-color: #ea5430;

}

Page 28: More of less (take 2)

Mixins - browser prefixes.border-radius(@n: 3px) { -webkit-border-radius: @n; -moz-border-radius: @n; border-radius: @n;}

button {.border-radius();

}

label {.border-radius(5px);

}

button { -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;}

label { -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;}

Page 29: More of less (take 2)

Mixins - combined rules.hide-offscreen() {

position: absolute;left: -999em;

}

button {.hide-offscreen();

}

a.skipToContent {.hide-offscreen();

}

button {position: absolute;left: -999em;

}

a.skipToContent {position: absolute;left: -999em;

}

Page 30: More of less (take 2)

Nested Rulesp {

text-indent: 1em;

em {

color: #ea5430;

}

}

p {

text-indent: 1em;

}

p em {

color: #ea5430;

}

Page 31: More of less (take 2)

The ”&” CombinatorThe ”this” of CSS

Page 32: More of less (take 2)

The “&” Combinatora {

color: #ea5430;

text-decoration: none;

&:hover {

text-decoration: underline;

}

&.someClass {

background-color: #ccc;

}

}

a {

color: #ea5430;

text-decoration: none;

}

a:hover {

text-decoration: underline;

}

a:someClass {

background-color: #ccc;

}

Page 33: More of less (take 2)

Going wild...input[type=checkbox] {

.hide-offscreen();

& + label {

background-color: #ea5430;

&:before {

content: ””;

.icon();

}

&:hover {

background-color: #cc4b29;

}

&:active {

background-color: #cc4b29;

}

}

}

input[type=checkbox] {

position: absolute;

left: -999em;

}

input[type=checkbox] + label {

background-color: #ea5430;

}

input[type=checkbox] + label:before { … }

input[type=checkbox] + label:hover { … }

input[type=checkbox] + label:active { … }

Page 34: More of less (take 2)

Really wild...input[type=checkbox] {

.hide-offscreen();

& + label {

background-color: #ea5430;

&:before {…}

&:hover {…}

&:active {…}

}

&:checked + label {

&:before {…}

&:hover {…}

&:active {…}

}

}

input[type=checkbox] {

position: absolute;

left: -999em;

}

input[type=checkbox] + label {

background-color: #ea5430;

}

input[type=checkbox] + label:before { … }

input[type=checkbox] + label:hover { … }

input[type=checkbox] + label:active { … }

input[type=checkbox]:checked + label:before { … }

input[type=checkbox]:checked + label:hover { … }

input[type=checkbox]:checked + label:active { … }

Page 35: More of less (take 2)

Even backwards...input[type=checkbox] {

.style-1();

.someParent & {

.style-2();

}

}

input[type=checkbox] { … }

.someParent input[type=checkbox] { … }

Page 36: More of less (take 2)

Operations

@colorRegular: #ea5430;

@colorHover: @colorRegular - #111; @colorActive: @colorRegular - #222;

p {color: @colorRegular}

p {color: @colorHover}

p {color: @colorActive}

@colorRegular: #ea5430; @

@colorHover: #d9431f;@

@colorActive: #c8320e;

Page 37: More of less (take 2)

All together now!

Page 38: More of less (take 2)

There’s much more...

Guarded mixins, client side usage, watch mode, javascript functions, reading parameters from other properties, debuggable in Web Inspector…

The list goes on.

Page 39: More of less (take 2)

What about SASS?

Less is not the only CSS preprocessor. There are different flavours of preprocessors with pros and cons. What we covered here, can be used on SASS as well, though the syntax may differ.

Page 40: More of less (take 2)

Architecture

This presentation is not meant to be an exhaustive overview of less syntax but a display of how it can be used to build a powerful CSS architecture for modern day, complex web apps.

Less is almost as easy to learn as CSS. Go nuts!

Page 41: More of less (take 2)

Questions?

Page 42: More of less (take 2)

Guilherme Zühlke O’Connor, London, Nov/2013