แแแ แแแแแแแก CSS / Sass แกแขแแแแกแขแฃแ แ แกแแฎแแแแซแฆแแแแแแ
แซแแ แแแแแแ แแแแแแ แฃแแ แแแแแแแแแ CSS-แกแ แแ Sass-แแแ แแแแแแจแแ แแแแ
- แขแแ แแแแแแแแแ
- CSS
- Sass
- แแแ แแแแแแแ
- แแแชแแแแแ
แแแกแขแ แฃแฅแชแแ แแ แแก แกแแแแฅแขแแ แแก(แแ แกแแแแฅแขแแ แแแแก แฏแแฃแคแแก) แกแแฎแแแแกแ แแ แแแฎแแฎแแกแแแแแแแแแแก แแ แแแแแแแแ แ แแแแแกแแช แแฅแแก แจแแแแแแ แกแแฎแ:
.listing {
font-size: 18px;
line-height: 1.2;
}แแแกแขแ แฃแฅแชแแแก แแแแแชแฎแแแแแแกแแก, "แกแแแแฅแขแแ แแแ" แแแแกแแแฆแแ แแแแ แแฃ DOM-แแก แ แแแแ แแแแแแแขแแแแ แแแแ แชแแแแแแ แแแแแแแแฃแแ แแแฎแแกแแแแแแแแแ. แกแแแแฅแขแแ แ แจแแแซแแแแ แแแแแกแแแฆแแ แแก HTML แแแแแแแขแแก, แแแแแแแขแแก แแแแกแแก, แแแแแก แแ แแแกแ แ แแแแแแแ แแขแ แแแฃแขแแก แแแจแแแแแแ. แฅแแแแแ แแแงแแแแแแแ แ แแแแแแแแ แแแแแแแแ:
.my-element-class {
/* ... */
}
[aria-hidden] {
/* ... */
}แแ แแแแแก, แแแฎแแกแแแแแแแแแ แแแแญแแแแ แกแแแแฅแขแแ แแแแ แแ แฉแแฃแ แแแแแแแขแแแก แกแขแแแก. แแแแแแฃแแ แแแฎแแกแแแแแแแแ แจแแแแแแ แกแแฎแแแแกแ แแ แแแจแแแแแแแแกแแแแ, แแแแแกแแแแ แ แแแกแขแ แฃแฅแชแแ แจแแแชแแแก แแ แ แแ แ แแแแแแแแ แแแฎแแกแแแแแแแแก. แแแแแแแแแ:
/* some selector */ {
background: #f1f1f1;
color: #333;
}โฌ แแแกแแฌแงแแกแจแ แแกแแแ
- แแแแแแงแแแ แแแ แขแแแ tab(2 แแแแแขแแแแแ) แกแขแ แแฅแแแแก แแแฌแงแแแแกแแก.
- แแแแกแแก แกแแฎแแแแก แแแแกแแแฆแแ แแกแแก แฃแแแ แแขแแกแแแ แแแแแแญแ แขแแ แแก แแ แแ แ camelCase-แก.
- แฅแแแแ แขแแ แแก แแ PascalCase-แแแแก แแแแแงแแแแแแช แแแจแแแแฃแแแ แแฃ แแงแแแแ BEM แแแแแแแแก(แแแขแแแแแ OOCSS แแ BEM แกแแฅแชแแแจแ)
- แแ แแแแแแงแแแ ID แกแแแแฅแขแแ แแแ.
- แแ แ แแแกแขแ แฃแฅแชแแแจแ แ แแแแแแแแ แกแแแแฅแขแแ แแก แแแแแงแแแแแแก แจแแแแฎแแแแแจแ แแแแแแฃแแ แแแแแแแ แแแฌแแ แ แชแแแแ แกแขแ แแฅแแแแ.
- แแแกแขแ แฃแฅแชแแแก แแแแแชแฎแแแแแแกแแก, แแแแฎแกแแแแ แคแแแฃแ แฃแแ แแ แฉแฎแแแแก
}แฌแแ แแแกแแ แแแแแขแแแแแ. - แแแฎแแกแแแแแแแแจแ, แแแแแขแแแแแ แแแกแแ แแ แฌแแ แขแแแแก
:แจแแแแแ แแ แแ แ แฌแแ. - แฉแแแแแขแ แคแแแฃแ แฃแแ แแ แฉแฎแแแ
}แแแกแแ แแฎแแ แกแขแ แแฅแแแแ. - แแแกแขแ แฃแฅแชแแแแก แจแแ แแก แแแแแแงแแแ แชแแ แแแแ แกแขแ แแฅแแแ.
แชแฃแแ แแ แแฅแขแแแ
.avatar{
border-radius:50%;
border:2px solid white; }
.no, .nope, .not_good {
// ...
}
#lol-no {
// ...
}แแแ แแ แแ แแฅแขแแแ
.avatar {
border-radius: 50%;
border: 2px solid white;
}
.one,
.selector,
.per-line {
// ...
}- แฃแแแ แแขแแกแแแ แแแแแแญแ แกแขแ แแฅแแแแก แแแแแแขแแ แแแก(// Sass-แแก แแแ แแแแจแ) แแ แแ แ แแแแแแกแแก.
- แฃแแฏแแแแกแแ แแแแแแขแแ แ แแแแแแแแกแ แแแแแก แแแแแฃแแแแแแแ แกแขแ แแฅแแแแ แแแแ แ แแแแแ แกแขแ แแฅแแแแก แแแแแจแ แกแแแแช แแแแแ.
- แแแแแกแแแแก แ แแแแแแช แแแแแแ แแแ แแฆแฌแแ แก แแแแแจแแฃแแแแแก แแแฌแแ แ แแแขแแแฃแ แ แแแแแแขแแ แ:
- z-index แแแฎแแกแแแแแแแแก แจแแแแฎแแแแแจแ
- แแ แแฃแแแ แแแ แแแแกแแแแแแแแก แแ แแแกแแแ แแแแแแจแแ แแแฃแแ แกแฎแแ แแแแแแ แขแแแแกแแแแก
แ แแแแแแแแแชแแแก แแฃแฌแแแ OOCSS แแ BEM-แแก แแแ แแแแฃแแ แแแแแแแแชแแแแแก แแแแแงแแแแแแก แจแแแแแแ แแแแแแแแแก แแแแ:
- แแกแแแ แแแแฎแแแ แแแ HTML แแ CSS-แก แจแแ แแก แแแกแแแแแ, แแแแชแ แแ แแแแกแแแฆแแ แฃแแ แแแแจแแ แแแแก แจแแฅแแแแจแ
- แแกแแแ แแแแฎแแแ แแแ แแแแแแแ แแแแ แแแแแงแแแแแแแ, แแแแแฃแแแแแแแแ แแแแแแแแแขแแแแก แจแแฅแแแแจแ
- แแซแแแแ แแ แแแแแแแจแ แแแแแแแแ แฉแแแแแแแฃแแ แแ แแแแแแแแ แกแแแชแแคแแแฃแ แ แแแแแก แฌแแ แแก แกแแจแฃแแแแแแก
- แแแแฎแแแ แแแ แแแกแจแขแแแแ แแแแแ แกแขแแแแแแก แชแฎแ แแแแแแก แจแแฅแแแแจแ
OOCSS, แแแฃ แแแแแฅแขแแ แแ แแแแขแแ แแแฃแแ CSS, แแ แแก แแแแแแแ แ แแแแแแช แฎแแแก แแแแฌแงแแแก แแแคแแฅแ แแ แฉแแแแ แกแขแแแแแแก แชแฎแ แแแแแแ แ แแแแ แช แแแแแฅแขแแแแก แแ แแแแแแแแแแ, แ แแแแแแแช แแ แแก แฎแแแแฎแแ แแแแแงแแแแแแแ, แแแแแแแ แแแแแ แแแแแก แแแแแแ แแแแแ แ แแแแแแ แแแแแฃแแแแแแแแ แแแแแงแแแแแ แจแแกแแซแแแแแแแ แแแแกแแแขแแก แแแแแกแแแแ แแแฌแแแจแ.
- Nicole Sullivan's OOCSS wiki
- Smashing Magazine's OOCSS-แก แจแแกแแแแแ
BEM แแแฃ "แแแแแ-แแแแแแแขแ-แแแแแคแแแแขแแ แ" แแ แแก HTML แแ CSS-แจแ แกแแฎแแแแก แแแแกแแแฆแแ แแก แจแแแแแฎแแแแ. แแฆแแแจแแฃแแ แแแแแแแ แจแแแฃแจแแแแแฃแแ แแฅแแ "แแแแแแฅแก"-แจแ แแแแแแแฅแกแฃแ แ แแ แแแกแจแขแแแฃแ แ แแแแแแแชแแแแแกแแแแก แแ แแแแแแแ แ แแแแ แช แกแแคแฃแซแแแแแแ แแแแแแแแแแแ OOCSS แแแแแแแแแขแแชแแแกแแแแก.
- CSS Trick's BEM 101
- Harry Roberts' 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
function ListingCard() {
return (
<article class="ListingCard ListingCard--featured">
<h1 class="ListingCard__title">Adorable 2BR in the sunny Mission</h1>
<div class="ListingCard__content">
<p>Vestibulum id ligula porta felis euismod semper.</p>
</div>
</article>
);
}/* ListingCard.css */
.ListingCard { }
.ListingCard--featured { }
.ListingCard__title { }
.ListingCard__content { }.ListingCardis the โblockโ and represents the higher-level component.ListingCard__titleis an โelementโ and represents a descendant of.ListingCardthat helps compose the block as a whole..ListingCard--featuredis a โmodifierโ and represents a different state or variation on the.ListingCardblock.
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 to your rule declarations, and they are not reusable.
For more on this subject, read CSS Wizardry's article on dealing with specificity.
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.
We recommend creating JavaScript-specific classes to bind to, prefixed with .js-:
<button class="btn btn-primary js-request-to-book">Request to Book</button>Use 0 instead of none to specify that a style has no border.
Bad
.foo {
border: none;
}Good
.foo {
border: 0;
}- Use the
.scsssyntax, never the original.sasssyntax - Order your regular CSS and
@includedeclarations logically (see below)
-
Property declarations
List all standard property declarations, anything that isn't an
@includeor a nested selector..btn-green { background: green; font-weight: bold; // ... }
-
@includedeclarationsGrouping
@includes at the end makes it easier to read the entire selector..btn-green { background: green; font-weight: bold; @include transition(background 0.5s ease); // ... }
-
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; font-weight: bold; @include transition(background 0.5s ease); .icon { margin-right: 10px; } }
Prefer dash-cased variable names (e.g. $my-variable) 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. $_my-variable).
Mixins should be used to DRY up your code, add clarity, or abstract complexity--in much the same way as well-named 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 should be avoided because it has unintuitive and potentially dangerous behavior, especially when used with nested selectors. Even extending top-level 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.
Do not nest selectors more than three levels deep!
.page-container {
.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.
This style guide is also available in other languages:
Bahasa Indonesia: mazipan/css-style-guide
Chinese (Traditional): ArvinH/css-style-guide
Chinese (Simplified): Zhangjd/css-style-guide
French: mat-u/css-style-guide
Japanese: nao215/css-style-guide
Korean: CodeMakeBros/css-style-guide
Portuguese (Brazil): felipevolpatto/css-style-guide
Portuguese (Portugal): SandroMiguel/airbnb-css-style-guide
Russian: rtplv/airbnb-css-ru
Spanish: ismamz/guia-de-estilo-css
Vietnamese: trungk18/css-style-guide
Italian: antoniofull/linee-guida-css
German: tderflinger/css-styleguide
(The MIT License)
Copyright (c) 2015 Airbnb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.