For my work
- Comments
- Formatting
- Selectors
- Declarations
- Values
- Vendor Prefixes
- ClassName
- Split
- Hacks
- Vertical Centering
/* ======================================================================
Block comment
Block comment
====================================================================== */
/* Block comment
====================================================================== */
/* Inline comment */
.selector {
margin: .5em; /* Inline comment */
}
- 2 spaces.
@charset "utf-8";
@media (min-width: 992px) {
.selector-1,
.selector-2 {
color: rgba(0, 0, 0, .5);
}
.selector-3 {
padding: 10px;
}
}
- No
div
orspan
. - No ID selectors.
/* bad */
div,
a span {
display: none;
}
#id {
font-size: 14px;
}
.selector-a .selector-b .selector-c .selector-d {
margin: 0;
}
/* good */
a:hover,
a:focus {
color: red;
}
label + input[type="radio"] {
/* ^_^ */
}
.list > li {
margin-bottom: 2px;
}
- Give each declaration its own line.
- Put a space after the : character.
- Make sure the declarations are in alphabetical order by the property.
- Don't omit the semi-colon at the end of the last declaration in a declaration block.
.selector {
box-sizing: border-box;
color: #666;
display: inline-block;
padding: 10px;
}
- Double quotes.
- Lowercase.
.selector {
background-image: url(i/bg.gif);
color: #f00;
font-family: "Microsoft Yahei", sans-serif;
margin: -.25em;
transition: all 0s;
}
.selector: before {
content: "\00a0";
}
Use Autoprefixer to add vendor prefixes to rules.
.selector {
display: flex;
}
will compile to
.selector {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex
}
- Lowercase and dashes.
- Meaningful names.
.btn-primary {
margin: 0;
}
Use js-* classes to denote behavior (as opposed to style), but keep these classes out of your CSS.
<div class="js-box"></div>
/* bad */
.btn {
background-color: #efefef;
border: 1px solid #ccc;
color: #36f;
display: block;
height: 24px;
line-height: 24px;
margin-left: 8px;
padding: 0 12px;
}
/* good */
.btn {
background-color: #efefef;
border: 1px solid #ccc;
color: #36f;
display: block;
height: 24px;
line-height: 24px;
padding: 0 12px;
}
.margin-left-8 {
margin-left: 8px;
}
No IE 10-
.selector {
position: fixed;
top: 50%;
transform: translateY(-50%);
}