.tweet-container {
padding: .5em 1em;
font-size: 1em;
color: #333;
background-color: #eee;
border: 1px solid #999;
border-radius: .5em;
}art
architecture
—https://en.wikibooks.org/wiki/Object_Oriented_Programming
#layout #header .page-title {
color: black;
font-size: 2em;
margin-bottom: 1em;
}
.article #comments ul > li > a.button {
border: 1px solid black;
background: #369;
color: #fff;
padding: 5px 10px;
}
#main .header .left-side .title {
font-size: 1.4em;
color: #999;
margin-bottom: 1em;
}
#main .header .right-side .title {
font-size: 1.4em;
color: #999;
}.page-title {
color: black;
font-size: 2em;
margin-bottom: 1em;
}
.button {
border: 1px solid black;
background: #369;
color: #fff;
padding: 5px 10px;
}
.side-title {
font-size: 1.4em;
color: #999;
}
.side-title--spaced }
margin-bottom: 1em;
}#layout #header .page-title {
color: black;
font-size: 2em;
margin-bottom: 1em;
}
.article #comments ul > li > a.button {
border: 1px solid black;
background: #369;
color: #fff;
padding: 5px 10px;
}
#main #title .left-side .title {
font-size: 1.4em;
color: #999;
margin-bottom: 1em;
}
#main #title .right-side .title }
font-size: 1.4em;
color: #999;
}.page-title {
color: black;
font-size: 2em;
margin-bottom: 1em;
}
.button {
border: 1px solid black;
background: #369;
color: #fff;
padding: 5px 10px;
}
.side-title {
font-size: 1.4em;
color: #999;
}
.side-title--spaced }
margin-bottom: 1em;
}Jonathan Snook
@snookca
https://smacss.com/
Set default values
body, form {
margin: 0;
padding: 0;
}
a {
color: #039;
}
a:hover {
color: #03F;
}High-level structure
#sidebar {
float: left;
width: 25%;
}
#main {
float: right;
width: 75%;
}
#footer {
border-top: 1px solid #ccc;
padding: 1em;
}.l-sidebar {
float: left;
width: 25%;
}
.l-main {
float: right;
width: 75%;
}
.l-footer {
border-top: 1px solid #ccc;
padding: 1em;
}Â
"Objects"
.media {
display: table;
margin-bottom: 1em;
}
.media > img {
display: table-cell;
}
.media-body {
display: table-cell;
vertical-align: top;
padding-left: 1em;
}
.media-title {
text-transform: uppercase;
font-weight: bold;
margin-bottom: .667em;
}Discrete and reusable
.media {
display: table;
margin-bottom: 1em;
}
.media > img {
display: table-cell;
}
.media-body {
display: table-cell;
vertical-align: top;
padding-left: 1em;
}
.media-title {
text-transform: uppercase;
font-weight: bold;
margin-bottom: .667em;
}<div class="media">
<img src="kitten.png"/>
<div class="media-body">
<h3 class="media-title">A kitten</h3>
<p>Lorem ipsum dolor sit amet.</p>
</div>
</div>Context independent
Add variants with subclasses
.button {
padding: .5em 1em;
background-color: #369;
color: white;
}#sidebar .button {
font-size: 1.2em;
}.button { ... }
.button--large {
font-size: 1.2em;
}<button class="button button--large">Suppose you need a different button in the sidebar...
Dynamic condition of a module
.section {
background: #ccc;
padding: 1em;
}
.section.is-collapsed {
height: 0;
}.navmenu { ... }
.navmenu > li.is-active { ... }Add variant to entire site
// in module-name.css
.mod {
border: 1px solid;
}
// in theme.css
.mod {
border-color: blue;
}#posts .post.partial h2 {
font-size: 2em;
font-weight: bold;
margin-bottom: 1.2em;
}
.post.single h1 {
font-size: 2em;
font-weight: bold;
margin-bottom: 1.2em;
}#posts .post.partial h2,
.post.single h1 {
font-size: 2em;
font-weight: bold;
margin-bottom: 1.2em;
}#posts .post.partial h2,
.post.single h1,
#categories h2 {
font-size: 2em;
font-weight: bold;
margin-bottom: 1.2em;
}.post-title {
font-size: 2em;
font-weight: bold;
margin-bottom: 1.2em;
}What if you need that style again elsewhere?
.button-primary {
padding: .5em 1em;
border-radius: .5em;
margin-bottom: .5em;
background-color: #369;
color: #fff;
}
.button-inverse {
padding: .5em 1em;
border-radius: .5em;
margin-bottom: .5em;
background-color: #fff;
color: #369;
}
.button-danger {
padding: .5em 1em;
border-radius: .5em;
margin-bottom: .5em;
background-color: #e33;
color: #fff;
}.button {
padding: .5em 1em;
border-radius: .5em;
margin-bottom: .5em;
}
.button--primary {
background-color: #369;
color: #fff;
}
.button--inverse {
background-color: #fff;
color: #369;
}
.button--danger {
background-color: #e33;
color: #fff;
}@mixin text-header-small {
font-family: Georgia, serif;
font-size: 1.1em;
text-transform: uppercase;
}
.panel-title {
@include text-header-small();
}
.modal-title {
@include text-header-small();
}
.list-heading {
@include text-header-small();
}.panel-body {
background-color: #eee;
font-size: 1em;
margin-bottom: 1em;
}
.twitter-body {
background-color: #eee;
font-size: 1em;
margin-bottom: 1em;
}.panel-body {
background-color: @gray-light;
font-size: 1em;
margin-bottom: 1em;
}
.twitter-body {
background-color: @gray-light;
font-size: 1em;
margin-bottom: 1em;
}.button-register {
display: inline-block;
border-radius: .5em;
font-size: 1.2em;
padding: 1em;
background-color: green;
color: white;
}.button {
display: inline-block;
border-radius: .5em;
}
.button--large {
font-size: 1.2em;
padding: 1em;
}
.button--success {
background-color: green;
color: white;
}.button--large { ... }
.button--small { ... }
.button--success { ... }
.button--warning { ... }
.button--failure { ... }.panel {
padding: 1em;
background-color: #ccc;
border: 1px solid #999;
}
.panel-title {
font-weight: bold;
margin-bottom: 1em;
}
.panel-body {
display: table;
}
.panel-body-left,
.panel-body-right {
display: table-cell;
width: 50%;
}
.panel-footer { ... }
.panel-button-close { ... }.panel { ... }
.panel-title { ... }
.panel-body { ... }
.split {
display: table;
}
.split-left,
.split-right {
display: table-cell;
width: 50%;
}
.button-row { ... }
.button--primary { ... }–Martin Fowler, Clean Code
Once a class is compiled and shipped,
it cannot be changed (except to fix bugs)
.search {
position: relative;
margin-bottom: 1em;
}#homepage .search {
position: fixed;
top: 2em;
right: 2em;
left: 2em;
}Unsafe—this is modifying the original class
.search--fixed {
position: fixed;
top: 2em;
right: 2em;
left: 2em;
}Safe—changes functionality through extension
.dialog {
padding: 1em;
border: 1px solid #999;
}
.dialog > h3 {
text-align: left;
}
.text-center {
text-align: center;
}<div class="dialog">
<h3 class="text-center">
Some title
</h3>
</div>Specificity mismatch!
.text-center {
text-align: center !important;
}!important is OK in
utility classes.
Â
Really!
.button {
padding: .5em 1em;
background-color: #369;
color: white;
font-size: 1em;
}
#sidebar .button {
font-size: 1.2em;
}.button {
padding: .5em 1em;
background-color: #369;
color: white;
font-size: 1em;
}
.button--large {
font-size: 1.2em;
}One class;
two outcomes
Two classes;
two outcomes
public class TextEditor {
private SpellChecker checker;
public TextEditor() {
this.checker = new SpellChecker();
}
}public class TextEditor {
private ISpellChecker checker;
public TextEditor(ISpellChecker checker) {
this.checker = checker;
}
}Logic is passed in. The calling code is in charge.
Core logic is buried inside the class. The class is in charge.
—Johnson & Foote, Designing Reusable Classes
Your HTML is dependent upon your CSS.
Not the other way around.
.content div .connections > .wrapper ul > li.connection:hover > .presence {
...;
}
#sidebar ul.nav li:last-child {
...;
}@css_in_action
@keithjgrant
keithjgrant.com