best practice css
TRANSCRIPT
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 1/28
Best Practice CSS
Rimian Perkins
@rimian
Senior Drupal Developer at PreviousNext:
previousnext.com.au
@previousnext
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 2/28
It's about...
Representing design faithfully.
● Enforcing the graphic designer's rules.
● Giving CSS more structure
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 3/28
I ❤ CSS
● Abstracts design from content
● Simple language structure (insert buzz word)
● Cascades
● Tolerates errors
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 4/28
It's a jungle out there...
A typical project may have:
● Many CSS files
● More than one developer
● A distributed team
Beware of Software Entropy
●
The natural tendency for information to becomechaotic.
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 5/28
But wait. There's more.
Imposed Duplication
● Attributes and Values must be re applied
● Block hierarchy unlike DOM
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 6/28
Duplication of values
Designer's idea: “I really like blue”
.a { color: #00c; }
.b { border-bottom: #00c; }
.c { background-color: #00c; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 7/28
A bit more complicated
Designer: “I really like blue colour schemes.”
.fav_color { color: #005; border-color: #00f; }
.contrast { color: #660; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 8/28
Complicated
Colours for <table> in garland theme:
#d3e7f4, #494949, #6f9dbd, #d3e7f4, #edf5fa
#fff, #ffb, #ffd, #ddecf5, #e6f1f7, #fff, #b4d7f0
#d4e7f3, #455067
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 9/28
Repetition of spacing
Designer: “I want all my margins 10 pixels”
.container { margin: 10px; }
..
h2 { margin-bottom: 10px; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 10/28
The Reverse: Ambiguity
“The image is 200 pixels x 100 pixels”
.a { width: 200px; height: 100px; }
“The columns are 200 pixels wide”
.b { width: 200px; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 11/28
Context of Block Declarations
.a {}
.a .b { }
.a .b .c{ }
.a .b .d{ }
.a .x { }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 12/28
It's just an expression
.col-1 { width: 80px; }
.col-2 { width: 160px; }
.col-3 { width: 240px; }
.col-4 { width: 320px; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 13/28
Technical Cost
The overhead of managing CSS
● Design knowledge must be stored outside CSS
● Developers have to reverse engineer.
● Flat structure increases name collisions
● The Original Design Idea is lost in theimplementation.
● Software Entropy
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 14/28
Definitions
A Framework
● An abstraction in which common code providinggeneric functionality can be overridden by user
code, thus providing specific functionality. An Abstraction Layer
● A way of hiding the implementation details of a
particular set of functionality
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 15/28
Drum Roll Please
Why not write CSS through an abstraction layer?
● Hide the evils of duplication
● Enhance the good features
● Write the code that writes the code.
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 16/28
Sass - Syntactically AwesomeStylesheets
sass-lang.com
● It's a Ruby Gem
● Sass is an extension of CSS.
● It’s translated to well-formatted, standard CSS.
Related to: Less
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 17/28
Features of Sass
● Variables
● Operations
● Control Structures
● Functions
● Selector Inheritance and Mixins
●
Nested Classes● Firebug plugin and console thingy
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 18/28
How do I get it?
$ gem install haml
$ mv style.css style.scss
$ sass --watch style.scss:style.css
OR
Drupal Projects: Sass, Less, xCSS
OR (for mac OSX)incident57.com/less
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 19/28
Variables
$hex: #9cc;$grid: 10px;
.a {color: $hex; margin: $grid;}
.a {color: #9cc; margin: 10px;}
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 20/28
Operations
$hex: #67ef54;
.a { color: $hex + #111; }
$grid: 10px;
img.thumb { width: $grid * 16; height: $grid * 9 }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 21/28
Selector Inheritance
.a { color: red; }
.b { width: 100px; }
.my_class{ @extend .a; @extend .b; }
..
.my_class { color: red; width: 100px; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 22/28
Mixins
@mixin spacer ($arg) { padding: $arg; }
.b { @include spacer(10px); color: red; }
.b { padding: 10px; color: red; }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 23/28
Here's one I prepared earlier.
● Define units● Create reusable things
● Compose the things like design requirement
● Grids are good!
● Run UI prototype in parallel to graphic design.
https://github.com/rimian/css-framework
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 24/28
HTML5 Document
<div id="container">
<header></header>
<aside id="sidebar-first">
<div class="content"><h2></h2></div>
</aside>
<div id="content">
<div class="content"><h2></h2></div>
</div>
<aside id="sidebar-second">
<div class="content"><h2></h2></div>
</aside>
</div>
<footer></footer>
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 25/28
Create Elements
@mixin positioning() { // Position containers on the page }
@mixin space($option) {
// Apply default white space around things
// Option for top & bottom or sides or whatever
}
@mixin coloring($option) { //Apply colour. With reverse out option. }
@mixin corners() { // Handle browser support and apply corners }
@mixin bordering() { // Apply a standard border to some things }
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 26/28
Define Units
grid: 10px;
$page_width: $grid * 120;
$col_width: $grid * 20;
$hex_bg: #efefef;
$hex_fg: #111;
$hex_content: white;
$hex_low: darken($hex_bg, 30%);
$corners: $grid * 0.8;$small: $grid * 0.9;
@import 'components.scss';
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 27/28
Compose CSS
header {
@include corners();
@include space(sides);
div { @include space(ends); }
}
aside {
@include positioning(column);
div.content {
@include corners();
@include space(all);
}
8/2/2019 Best Practice Css
http://slidepdf.com/reader/full/best-practice-css 28/28
FIN
Sass - sass-lang.com
Less - lesscss.org
Less Framework - lessframework.com
Less.app - incident57.com/less
xCSS (php) - xcss.antpaw.org
The Pragmatic Programmer – pragprog.com
https://github.com/rimian/css-framework