Skip to content

Commit 325b659

Browse files
author
Mike Fowler
committed
Adds initial styleguide content.
0 parents  commit 325b659

File tree

2 files changed

+340
-0
lines changed

2 files changed

+340
-0
lines changed

.scss-lint.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
linters:
2+
3+
BorderZero:
4+
enabled: true
5+
convention: none
6+
7+
BemDepth:
8+
enabled: true
9+
10+
DeclarationOrder:
11+
enabled: false
12+
13+
LeadingZero:
14+
enabled: false
15+
16+
PropertySortOrder:
17+
enabled: false
18+
19+
QualifyingElement:
20+
enabled: false
21+
22+
SelectorFormat:
23+
enabled: true
24+
convention: hyphenated_BEM
25+
class_convention: ^(?!js-).*
26+
class_convention_explanation: should not be written in the form js-*
27+
28+
SingleLinePerProperty:
29+
enabled: true
30+
allow_single_line_rule_sets: false
31+
32+
StringQuotes:
33+
enabled: true
34+
style: double_quotes

README.md

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
# Airbnb CSS / Sass Styleguide
2+
3+
*A mostly reasonable approach to CSS and Sass*
4+
5+
## Table of Contents
6+
7+
1. [Terminology](#terminology)
8+
- [Rule Declaration](#rule-declaration)
9+
- [Selectors](#selectors)
10+
- [Properties](#properties)
11+
1. [CSS](#css)
12+
- [Formatting](#formatting)
13+
- [Comments](#comments)
14+
- [OOCSS and BEM](#oocss-and-bem)
15+
- [ID Selectors](#id-selectors)
16+
- [JavaScript hooks](#javascript-hooks)
17+
1. [Sass](#sass)
18+
- [Syntax](#syntax)
19+
- [Ordering](#ordering-of-property-declarations)
20+
- [Mixins](#mixins)
21+
- [Placeholders](#placeholders)
22+
- [Nested selectors](#nested-selectors)
23+
24+
## Terminology
25+
26+
### Rule declaration
27+
28+
A “rule declaration” is the name given to a selector (or a group of selectors) with an accompanying group of properties. Here's an example:
29+
30+
```css
31+
.listing {
32+
font-size: 18px;
33+
line-height: 1.2;
34+
}
35+
```
36+
37+
### Selectors
38+
39+
In a rule declaration, “selectors” are the bits that determine which elements in the DOM tree will be styled by the defined properties. Selectors can match HTML elements, as well as an element's class, ID, or any of its attributes. Here are some examples of selectors:
40+
41+
```css
42+
.my-element-class {
43+
/* ... */
44+
}
45+
46+
[aria-hidden] {
47+
/* ... */
48+
}
49+
```
50+
51+
### Properties
52+
53+
Finally, properties are what give the selected elements of a rule declaration their style. Properties are key-value pairs, and a rule declaration can contain one or more property declarations. Property declarations look like this:
54+
55+
```css
56+
/* some selector */ {
57+
background: #f1f1f1;
58+
color: #333;
59+
}
60+
```
61+
62+
## CSS
63+
64+
### Formatting
65+
66+
* Use soft tabs (2 spaces) for indentation
67+
* Prefer dashes over camelCasing in class names. Underscores are OK if you're using BEM (see [OOCSS and BEM](#oocss-and-bem) below).
68+
* Do not use ID selectors
69+
* When using multiple selectors in a rule declaration, give each selector its own line.
70+
* Put a space before `{` in rule declarations
71+
* Put blank lines between rule declarations
72+
* In property declarations, put a space after, but not before, the `:` character.
73+
* Order property declarations alphabetically for consistency
74+
* Put closing braces of declaration blocks on a new line
75+
76+
**Bad**
77+
78+
```css
79+
.avatar{
80+
border-radius:50%;
81+
border:2px solid white; }
82+
.no, .nope, .not_good {
83+
// ...
84+
}
85+
#lol-no {
86+
// ...
87+
}
88+
```
89+
90+
**Good**
91+
92+
```css
93+
.avatar {
94+
border-radius: 50%;
95+
border: 2px solid white;
96+
}
97+
98+
.one,
99+
.selector,
100+
.per-line {
101+
// ...
102+
}
103+
```
104+
105+
### Comments
106+
107+
* Prefer line comments (`//` in Sass-land) to block comments.
108+
* Prefer comments on their own line. Avoid end-of-line comments.
109+
* Write detailed comments for code that isn't self-documenting:
110+
- Uses of z-index
111+
- Compatibility or browser-specific hacks
112+
113+
### OOCSS and BEM
114+
115+
We encourage some combination of OOCSS and BEM for these reasons:
116+
117+
* It helps create clear, strict relationships between CSS and HTML
118+
* It helps us create reusable, composable components
119+
* It allows for less nesting and lower specificity
120+
* It helps in building scalable stylesheets
121+
122+
**OOCSS**, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusuable, repeatable snippets that can be used independently throughout a website.
123+
124+
* Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki)
125+
* Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
126+
127+
**BEM**, or “Block-Element-Modifier”, is a _naming convention_ for classes in HTML and CSS. It was originally developed by Yandex with large codebases and scalability in mind, and can serve as a solid set of guidelines for implementing OOCSS.
128+
129+
* CSS Trick's [BEM 101](https://css-tricks.com/bem-101/)
130+
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
131+
132+
**Example**
133+
134+
```html
135+
<article class="listing-card listing-card--featured">
136+
137+
<h1 class="listing-card__title">Adorable 2BR in the sunny Mission</h1>
138+
139+
<div class="listing-card__content">
140+
<p>Vestibulum id ligula porta felis euismod semper.</p>
141+
</div>
142+
143+
</article>
144+
```
145+
146+
```css
147+
.listing-card { }
148+
.listing-card--featured { }
149+
.listing-card__title { }
150+
.listing-card__content { }
151+
```
152+
153+
* `.listing-card` is the “block” and represents the higher-level component
154+
* `.listing-card__title` is an “element” and represents a descendant of `.listing-card` that helps compose the block as a whole.
155+
* `.listing-card--featured` is a “modifier” and represents a different state or variation on the `.listing-card` block.
156+
157+
### ID selectors
158+
159+
While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) to your rule declarations.
160+
161+
For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity.
162+
163+
### JavaScript hooks
164+
165+
Avoid binding to the same class in both your CSS and JavaScript. Conflating the two often leads to, at a minimum, time wasted during refactoring when a developer must cross-reference each class they are changing, and at its worst, developers being afraid to make changes for fear of breaking functionality.
166+
167+
We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`:
168+
169+
```html
170+
<button class="btn btn-primary js-request-to-book">Request to Book</button>
171+
```
172+
173+
## Sass
174+
175+
### Syntax
176+
177+
* Use the .scss syntax, never the original .sass syntax
178+
* Order your `@extend`, regular CSS and `@include` declarations logically (see below)
179+
180+
### Ordering of property declarations
181+
182+
1. `@extend` declarations
183+
184+
Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another.
185+
186+
```scss
187+
.btn-green {
188+
@extend %btn;
189+
// ...
190+
}
191+
```
192+
193+
2. Property declarations
194+
195+
Now list all standard property declarations, anything that isn't an `@extend`, `@include`, or a nested selector.
196+
197+
```scss
198+
.btn-green {
199+
@extend %btn;
200+
background: green;
201+
font-weight: bold;
202+
// ...
203+
}
204+
```
205+
206+
3. `@include` declarations
207+
208+
Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s.
209+
210+
```scss
211+
.btn-green {
212+
@extend %btn;
213+
background: green;
214+
font-weight: bold;
215+
@include transition(background 0.5s ease);
216+
// ...
217+
}
218+
```
219+
220+
4. Nested selectors
221+
222+
Nested selectors, if necessary, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors.
223+
224+
```scss
225+
.btn {
226+
@extend %btn;
227+
background: green;
228+
font-weight: bold;
229+
@include transition(background 0.5s ease);
230+
231+
.icon {
232+
margin-right: 10px;
233+
}
234+
}
235+
```
236+
237+
### Mixins
238+
239+
Mixins, defined via `@mixin` and called with `@include`, should be used sparingly and only when function arguments are necessary. A mixin without function arguments (i.e. `@mixin hide { display: none; }`) is better accomplished using a placeholder selector (see below) in order to prevent code duplication.
240+
241+
### Placeholders
242+
243+
Placeholders in Sass, defined via `%selector` and used with `@extend`, are a way of defining a block of properties that aren't automatically output in your compiled stylesheet. Instead, other selectors “inherit” from the placeholder, and the relevant selectors are copied to the point in the stylesheet where the placeholder is defined. This is best illustrated with the example below.
244+
245+
Placeholders are powerful but easy to abuse, especially when combined with nested selectors. *As a rule of thumb, avoid creating placeholders with nested declaration blocks, or calling `@extend` inside nested selectors.** Placeholders are great for simple inheritance, but can easily result in the accidental creation of additional selectors without paying close attention to how and where they are used.
246+
247+
**Sass**
248+
249+
```sass
250+
// Unless we call `@extend %icon` these properties won't be compiled!
251+
%icon {
252+
font-family: 'Airglyphs';
253+
}
254+
255+
.icon-error {
256+
@extend %icon;
257+
color: red;
258+
}
259+
260+
.icon-success {
261+
@extend %hide;
262+
color: green;
263+
}
264+
```
265+
266+
**CSS**
267+
268+
```css
269+
.icon-error,
270+
.icon-success {
271+
font-family: 'Airglyphs';
272+
}
273+
274+
.icon-error {
275+
color: red;
276+
}
277+
278+
.icon-success {
279+
color: green;
280+
}
281+
```
282+
283+
### Nested selectors
284+
285+
**Do not nest selectors more than three levels deep!**
286+
287+
```scss
288+
.page-container {
289+
.content {
290+
.profile {
291+
// STOP!
292+
}
293+
}
294+
}
295+
```
296+
297+
When selectors become this long, you're likely writing CSS that is:
298+
299+
* Strongly coupled to the HTML (fragile) *—OR—*
300+
* Overly specific (powerful) *—OR—*
301+
* Not reusable
302+
303+
304+
Again: **never nest ID selectors!**
305+
306+
If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you're writing well thought-out CSS, you should rarely, *if ever* need to do this.

0 commit comments

Comments
 (0)