Airbnb - Css - A Mostly Reasonable Approach To CSS and Sass - PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

This repository Search Pull requests Issues Gist

airbnb / css Watch 144 Star 2,382 Fork 544

Code Issues 6 Pull requests 4 Projects 0 Pulse Graphs

A mostly reasonable approach to CSS and Sass.

34 commits 1 branch 0 releases 11 contributors

Branch: master New pull request Create new file Upload files Find file Clone or download

ljharb committed on GitHub Merge pull request #40 from mazipan/englishversion Latest commit ebccd7c on 14 Dec 2016

.scsslint.yml Enable PrivateNamingConvention scsslint rule a year ago

README.md Add indonesian link 3 months ago

README.md

Airbnb CSS / Sass Styleguide


A mostly reasonable approach to CSS and Sass

Table of Contents
1. Terminology
Rule Declaration
Selectors
Properties
2. CSS
Formatting
Comments
OOCSS and BEM
ID Selectors
JavaScript hooks
Border
3. Sass
Syntax
Ordering
Variables
Mixins
Extend directive
Nested selectors
4. Translation

Terminology

Rule declaration
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:

.listing{
fontsize:18px;
lineheight:1.2;
}

Selectors
https://github.com/airbnb/css 1/6
14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

Selectors
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:

.myelementclass{
/*...*/
}

[ariahidden]{
/*...*/
}

Properties
Finally, properties are what give the selected elements of a rule declaration their style. Properties are keyvalue pairs, and a
rule declaration can contain one or more property declarations. Property declarations look like this:

/*someselector*/{
background:#f1f1f1;
color:#333;
}

CSS

Formatting
Use soft tabs 2 spaces for indentation
Prefer dashes over camelCasing in class names.
Underscores and PascalCasing are okay if you are using BEM see OOCSS and BEM below.
Do not use ID selectors
When using multiple selectors in a rule declaration, give each selector its own line.
Put a space before the opening brace { in rule declarations
In properties, put a space after, but not before, the : character.
Put closing braces } of rule declarations on a new line
Put blank lines between rule declarations

Bad

.avatar{
borderradius:50%;
border:2pxsolidwhite;}
.no,.nope,.not_good{
//...
}
#lolno{
//...
}

Good

.avatar{
borderradius:50%;
border:2pxsolidwhite;
}

.one,
.selector,
.perline{
//...
}

Comments

https://github.com/airbnb/css 2/6
14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

Prefer line comments // in Sassland to block comments.


Prefer comments on their own line. Avoid endofline comments.
Write detailed comments for code that isn't selfdocumenting:
Uses of zindex
Compatibility or browserspecific hacks

OOCSS and BEM


We encourage some combination of OOCSS and BEM for these reasons:

It helps create clear, strict relationships between CSS and HTML


It helps us create reusable, composable components
It allows for less nesting and lower specificity
It helps in building scalable stylesheets

OOCSS, or Object Oriented CSS, is an approach for writing CSS that encourages you to think about your stylesheets as a
collection of objects: reusable, repeatable snippets that can be used independently throughout a website.

Nicole Sullivan's OOCSS wiki


Smashing Magazine's Introduction to OOCSS

BEM, or BlockElementModifier, 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.

CSS Trick's BEM 101


Harry Roberts' introduction to BEM

We recommend a variant of BEM with PascalCased blocks, which works particularly well when combined with components
e.g. React. Underscores and dashes are still used for modifiers and children.

Example

//ListingCard.jsx
functionListingCard(){
return(
<articleclass="ListingCardListingCardfeatured">

<h1class="ListingCard__title">Adorable2BRinthesunnyMission</h1>

<divclass="ListingCard__content">
<p>Vestibulumidligulaportafeliseuismodsemper.</p>
</div>

</article>
);
}

/*ListingCard.css*/
.ListingCard{}
.ListingCardfeatured{}
.ListingCard__title{}
.ListingCard__content{}

.ListingCard is the block and represents the higherlevel component

.ListingCard__title is an element and represents a descendant of .ListingCard that helps compose the block as a
whole.
.ListingCardfeatured is a modifier and represents a different state or variation on the .ListingCard block.

ID selectors
While it is possible to select elements by ID in CSS, it should generally be considered an antipattern. ID selectors introduce
an unnecessarily high level of specificity to your rule declarations, and they are not reusable.

For more on this subject, read CSS Wizardry's article on dealing with specificity.

JavaScript hooks
https://github.com/airbnb/css 3/6
14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

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 crossreference each class they are changing, and at its worst, developers
being afraid to make changes for fear of breaking functionality.

We recommend creating JavaScriptspecific classes to bind to, prefixed with .js:

<buttonclass="btnbtnprimaryjsrequesttobook">RequesttoBook</button>

Border
Use 0 instead of none to specify that a style has no border.

Bad

.foo{
border:none;
}

Good

.foo{
border:0;
}

Sass

Syntax
Use the .scss syntax, never the original .sass syntax
Order your regular CSS and @include declarations logically see below

Ordering of property declarations


1. Property declarations

List all standard property declarations, anything that isn't an @include or a nested selector.

.btngreen{
background:green;
fontweight:bold;
//...
}

2. @include declarations

Grouping @includes at the end makes it easier to read the entire selector.

.btngreen{
background:green;
fontweight:bold;
@includetransition(background0.5sease);
//...
}

3. Nested selectors

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.

.btn{
background:green;
fontweight:bold;
@includetransition(background0.5sease);

https://github.com/airbnb/css 4/6
14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

.icon{
marginright:10px;
}
}

Variables
Prefer dashcased variable names e.g. $myvariable over camelCased or snake_cased variable names. It is acceptable to
prefix variable names that are intended to be used only within the same file with an underscore e.g. $_myvariable.

Mixins
Mixins should be used to DRY up your code, add clarity, or abstract complexityin much the same way as wellnamed
functions. Mixins that accept no arguments can be useful for this, but note that if you are not compressing your payload e.g.
gzip, this may contribute to unnecessary code duplication in the resulting styles.

Extend directive
@extend should be avoided because it has unintuitive and potentially dangerous behavior, especially when used with nested
selectors. Even extending toplevel placeholder selectors can cause problems if the order of selectors ends up changing later
e.g. if they are in other files and the order the files are loaded shifts. Gzipping should handle most of the savings you would
have gained by using @extend, and you can DRY up your stylesheets nicely with mixins.

Nested selectors
Do not nest selectors more than three levels deep!

.pagecontainer{
.content{
.profile{
//STOP!
}
}
}

When selectors become this long, you're likely writing CSS that is:

Strongly coupled to the HTML fragile OR


Overly specific powerful OR
Not reusable

Again: never nest ID selectors!

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 are writing well
formed HTML and CSS, you should never need to do this.

Translation
This style guide is also available in other languages:

Chinese Traditional: ArvinH/cssstyleguide

Chinese Simplified: Zhangjd/cssstyleguide

Japanese: nao215/cssstyleguide

Korean: CodeMakeBros/cssstyleguide

Portuguese: felipevolpatto/cssstyleguide

Russian: Nekorsis/cssstyleguide

Spanish: ismamz/guiadeestilocss

Vietnamese: trungk18/cssstyleguide

Bahasa Indonesia: mazipan/cssstyleguide

https://github.com/airbnb/css 5/6
14/03/2017 airbnb/css:AmostlyreasonableapproachtoCSSandSass.

2017 GitHub, Inc. Terms Privacy Security Status Help Contact GitHub API Training Shop Blog About

https://github.com/airbnb/css 6/6

You might also like