Skip to content

Commit 5854876

Browse files
committed
整理 css 代码规范
1 parent c2a1a82 commit 5854876

File tree

6 files changed

+1275
-0
lines changed

6 files changed

+1275
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# css-style-guide
2+
23
css-style-guide

design.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# 规范设计指南
3+
4+
目的:实现易维护、易使用的样式表,侧重继承、组合、扩展、场景层次划分
5+
6+
- class 连接符统一使用 -,禁止使用大写字母。
7+
- 关于 css,选择 SMACSS、suit(bem)还是 OOCSS。暂建议组合使用,详细待定
8+
- 规范实现细节,精读 google、mdo、airbnb、stylelint 等css规范后制定
9+
- 是否引入 postcss。待定
10+
- 整理一份文档,优劣写法对比 good pk bad
11+
- 使用 stylelint 插件检测代码,整理一个好的配置文件

docs/airbnb.en.md

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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+
- [Border](#border)
18+
1. [Sass](#sass)
19+
- [Syntax](#syntax)
20+
- [Ordering](#ordering-of-property-declarations)
21+
- [Variables](#variables)
22+
- [Mixins](#mixins)
23+
- [Extend directive](#extend-directive)
24+
- [Nested selectors](#nested-selectors)
25+
1. [Translation](#translation)
26+
27+
## Terminology
28+
29+
### Rule declaration
30+
31+
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:
32+
33+
```css
34+
.listing {
35+
font-size: 18px;
36+
line-height: 1.2;
37+
}
38+
```
39+
40+
### Selectors
41+
42+
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:
43+
44+
```css
45+
.my-element-class {
46+
/* ... */
47+
}
48+
49+
[aria-hidden] {
50+
/* ... */
51+
}
52+
```
53+
54+
### Properties
55+
56+
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:
57+
58+
```css
59+
/* some selector */ {
60+
background: #f1f1f1;
61+
color: #333;
62+
}
63+
```
64+
65+
## CSS
66+
67+
### Formatting
68+
69+
* Use soft tabs (2 spaces) for indentation
70+
* Prefer dashes over camelCasing in class names.
71+
- Underscores and PascalCasing are okay if you are using BEM (see [OOCSS and BEM](#oocss-and-bem) below).
72+
* Do not use ID selectors
73+
* When using multiple selectors in a rule declaration, give each selector its own line.
74+
* Put a space before the opening brace `{` in rule declarations
75+
* In properties, put a space after, but not before, the `:` character.
76+
* Put closing braces `}` of rule declarations on a new line
77+
* Put blank lines between rule declarations
78+
79+
**Bad**
80+
81+
```css
82+
.avatar{
83+
border-radius:50%;
84+
border:2px solid white; }
85+
.no, .nope, .not_good {
86+
// ...
87+
}
88+
#lol-no {
89+
// ...
90+
}
91+
```
92+
93+
**Good**
94+
95+
```css
96+
.avatar {
97+
border-radius: 50%;
98+
border: 2px solid white;
99+
}
100+
101+
.one,
102+
.selector,
103+
.per-line {
104+
// ...
105+
}
106+
```
107+
108+
### Comments
109+
110+
* Prefer line comments (`//` in Sass-land) to block comments.
111+
* Prefer comments on their own line. Avoid end-of-line comments.
112+
* Write detailed comments for code that isn't self-documenting:
113+
- Uses of z-index
114+
- Compatibility or browser-specific hacks
115+
116+
### OOCSS and BEM
117+
118+
We encourage some combination of OOCSS and BEM for these reasons:
119+
120+
* It helps create clear, strict relationships between CSS and HTML
121+
* It helps us create reusable, composable components
122+
* It allows for less nesting and lower specificity
123+
* It helps in building scalable stylesheets
124+
125+
**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.
126+
127+
* Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki)
128+
* Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
129+
130+
**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.
131+
132+
* CSS Trick's [BEM 101](https://css-tricks.com/bem-101/)
133+
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
134+
135+
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.
136+
137+
**Example**
138+
139+
```jsx
140+
// ListingCard.jsx
141+
function ListingCard() {
142+
return (
143+
<article class="ListingCard ListingCard--featured">
144+
145+
<h1 class="ListingCard__title">Adorable 2BR in the sunny Mission</h1>
146+
147+
<div class="ListingCard__content">
148+
<p>Vestibulum id ligula porta felis euismod semper.</p>
149+
</div>
150+
151+
</article>
152+
);
153+
}
154+
```
155+
156+
```css
157+
/* ListingCard.css */
158+
.ListingCard { }
159+
.ListingCard--featured { }
160+
.ListingCard__title { }
161+
.ListingCard__content { }
162+
```
163+
164+
* `.ListingCard` is the “block” and represents the higher-level component
165+
* `.ListingCard__title` is an “element” and represents a descendant of `.ListingCard` that helps compose the block as a whole.
166+
* `.ListingCard--featured` is a “modifier” and represents a different state or variation on the `.ListingCard` block.
167+
168+
### ID selectors
169+
170+
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, and they are not reusable.
171+
172+
For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity.
173+
174+
### JavaScript hooks
175+
176+
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.
177+
178+
We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`:
179+
180+
```html
181+
<button class="btn btn-primary js-request-to-book">Request to Book</button>
182+
```
183+
184+
### Border
185+
186+
Use `0` instead of `none` to specify that a style has no border.
187+
188+
**Bad**
189+
190+
```css
191+
.foo {
192+
border: none;
193+
}
194+
```
195+
196+
**Good**
197+
198+
```css
199+
.foo {
200+
border: 0;
201+
}
202+
```
203+
204+
## Sass
205+
206+
### Syntax
207+
208+
* Use the `.scss` syntax, never the original `.sass` syntax
209+
* Order your regular CSS and `@include` declarations logically (see below)
210+
211+
### Ordering of property declarations
212+
213+
1. Property declarations
214+
215+
List all standard property declarations, anything that isn't an `@include` or a nested selector.
216+
217+
```scss
218+
.btn-green {
219+
background: green;
220+
font-weight: bold;
221+
// ...
222+
}
223+
```
224+
225+
2. `@include` declarations
226+
227+
Grouping `@include`s at the end makes it easier to read the entire selector.
228+
229+
```scss
230+
.btn-green {
231+
background: green;
232+
font-weight: bold;
233+
@include transition(background 0.5s ease);
234+
// ...
235+
}
236+
```
237+
238+
3. Nested selectors
239+
240+
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.
241+
242+
```scss
243+
.btn {
244+
background: green;
245+
font-weight: bold;
246+
@include transition(background 0.5s ease);
247+
248+
.icon {
249+
margin-right: 10px;
250+
}
251+
}
252+
```
253+
254+
### Variables
255+
256+
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`).
257+
258+
### Mixins
259+
260+
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.
261+
262+
### Extend directive
263+
264+
`@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.
265+
266+
### Nested selectors
267+
268+
**Do not nest selectors more than three levels deep!**
269+
270+
```scss
271+
.page-container {
272+
.content {
273+
.profile {
274+
// STOP!
275+
}
276+
}
277+
}
278+
```
279+
280+
When selectors become this long, you're likely writing CSS that is:
281+
282+
* Strongly coupled to the HTML (fragile) *—OR—*
283+
* Overly specific (powerful) *—OR—*
284+
* Not reusable
285+
286+
287+
Again: **never nest ID selectors!**
288+
289+
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.
290+
291+
## Translation
292+
293+
This style guide is also available in other languages:
294+
295+
- ![tw](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Taiwan.png) **Chinese (Traditional)**: [ArvinH/css-style-guide](https://github.com/ArvinH/css-style-guide)
296+
- ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [Zhangjd/css-style-guide](https://github.com/Zhangjd/css-style-guide)
297+
- ![ja](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) **Japanese**: [nao215/css-style-guide](https://github.com/nao215/css-style-guide)
298+
- ![ko](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) **Korean**: [CodeMakeBros/css-style-guide](https://github.com/CodeMakeBros/css-style-guide)
299+
- ![PT-BR](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese**: [felipevolpatto/css-style-guide](https://github.com/felipevolpatto/css-style-guide)
300+
- ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian**: [Nekorsis/css-style-guide](https://github.com/Nekorsis/css-style-guide)
301+
- ![es](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Spanish**: [ismamz/guia-de-estilo-css](https://github.com/ismamz/guia-de-estilo-css)
302+
- ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Vietnam.png) **Vietnamese**: [trungk18/css-style-guide](https://github.com/trungk18/css-style-guide)
303+
- ![id](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Indonesia.png) **Bahasa Indonesia**: [mazipan/css-style-guide](https://github.com/mazipan/css-style-guide)

0 commit comments

Comments
 (0)