-
Notifications
You must be signed in to change notification settings - Fork 375
Coding Guidelines
- Approach
This coding guide is based off of listening to some of the smartest people in the industry. It focuses on making performant, consistent, scalable and maintainable CSS. We all have our opinions and preferences, but have tried our best to put those aside to benefit the greater good with the current data at hand. This is a living document and we welcome contribution. The better the input the better the output. Feel free to help us get it right.
Use [2]consistent whitespace.
- Soft tabs of four spaces
.btn {
background: #fff;
}
Use consistent formatting in order to increase understanding and [2]legibility
- One selector per line
- Use one space between selector and first bracket
- Use one space between property and value after
:
- Always add a semicolon after property value
- Use double quotes
- Do not specify units for a zero value
- Include a space after each comma in a comma separated property list
- Separate each ruleset by an empty line
.selector-1,
.selector-2 {
box-sizing: border-box;
display: block;
font-family: helvetica, arial, sans-serif;
color: #333;
background: 0 0 #666 no-repeat url("../img/bg_img.png");
}
.selector-a,
.selector-b {
padding: 10px;
}
TopCoat is designed to be modular and component focused. The styles are created to be as portable as possibe while keeping performance as the top goal.
- Do not use IDs. They decrease [5]portability
/* NO */
#header {
position: absolute;
}
- Do not use tag selectors. They decrease [5]portability and it can impact [1]performance *see over qualified
/* NO */
header {
color: blue;
}
- Do not over qualify selectors because it impacts [1]performance
/* NO */
ul li.button.large.blue {
background: salmon;
}
- Use class names for specificity to increase [1]performance
/* YES */
.list-item {
border: 1px solid #666;
}
- Use component oriented naming [3]conventions
/*
* Base reset components
* Use the naming convention:
* {component}-{name}__{subcomponent}--{modifier}
*
*/
/* Component */
.button-group
/* Component Modifier */
.button-group--large
/* Subcomponent */
.button-group__button
/* Subcomponent Modifier */
.button-group__button--cta
/*
* Themed components
* Use the naming convention:
* {theme}-{component}-{name}__{subcomponent}--{modifier}
*/
/* Component */
.topcoat-button-group
/* Component Modifier */
.topcoat-button-group--large
/* Subcomponent */
.topcoat-button-group__button
/* Subcomponent Modifier */
.topcoat-button-group__button--cta
We depend on Topdoc comments in order to generate a style guide from the css.
/* topdoc
name: Button
description: A simple button
modifiers:
:active: Active state
.is-active: Simulates an active state on mobile devices
:disabled: Disabled state
.is-disabled: Simulates a disabled state on mobile devices
markup:
<a class="topcoat-button">Button</a>
<a class="topcoat-button is-active">Button</a>
<a class="topcoat-button is-disabled">Button</a>
example: http://codepen.io/
tags:
- desktop
- light
- mobile
- button
- quiet
blarg: very true
*/
.topcoat-button,
.topcoat-button--quiet,
.topcoat-button--large,
.topcoat-button--large--quiet,
.topcoat-button--cta,
.topcoat-button--large--cta {
Property order should be in [2]structural order of importance.
.selector {
/* Positioning */
position: absolute;
z-index: 10;
/* Display & Box Model */
display: inline-block;
overflow: hidden;
box-sizing: border-box;
width: 100px;
height: 100px;
padding: 10px;
border: 10px solid #333;
margin: 10px;
/* Visual styles */
background: #000;
color: #fff;
font-family: sans-serif;
font-size: 16px;
text-align: right;
/* Vendor prefixed styles */
-webkit-user-select: none;
}
Layout should be handled by a layout specific class. You should not mix layout and visual styling in one class definition.
.topcoat-header--dark {
background: #c6c8c8;
}
/* ... Layout section */
.span-3 {
width: 33.333%
}
<header class="topcoat-header--dark span-3"></header>
By importance of inclusion: