We will be using the guidelines provided in the link below as a base: Google HTML/CSS Style Guide
Following are additional guidelines to be followed. Note: The following rules will override the ones provided in the Google Style Guide.
- Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment.
- When grouping selectors, keep individual selectors to a single line.
- Include one space before the opening brace of declaration blocks for legibility.
- Place closing braces of declaration blocks on a new line.
- Include one space after each
:
for each declaration. - Each declaration should appear on its own line for more accurate error reporting.
- End all declarations with a semicolon. The last declaration's semicolon is optional, but your code is more error-prone without it.
- Comma-separated property values should include a space after each comma (e.g.,
box-shadow
). - Don't include spaces after commas within
rgb()
,rgba()
,hsl()
,hsla()
, orrect()
values. This helps differentiate multiple color values (comma, no space) from multiple property values (comma with space). - Don't prefix property values or color parameters with a leading zero (e.g.,
.5
instead of0.5
and-.5px
instead of-0.5px
). - Lowercase all hex values, e.g.,
#fff
. Lowercase letters are much easier to discern when scanning a document as they tend to have more unique shapes. - Use shorthand hex values where available, e.g.,
#fff
instead of#ffffff
. - Quote attribute values in selectors, e.g.,
input[type="text"]
. They’re only optional in some cases, and it’s a good practice for consistency. - Avoid specifying units for zero values, e.g.,
margin: 0;
instead ofmargin: 0px;
.
Bad
.selector, .selector-secondary, .selector[type=text] {
padding:15px;
margin:0px 0px 15px;
background-color:rgba(0, 0, 0, 0.5);
box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF
}
Good
.selector,
.selector-secondary,
.selector[type="text"] {
padding: 15px;
margin-bottom: 15px;
background-color: rgba(0,0,0,.5);
box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
}
Related property declarations should be grouped together following the order:
- Positioning
- Box model
- Typographic
- Visual
Positioning comes first because it can remove an element from the normal flow of the document and override box model-related styles. The box model comes next as it dictates a component's dimensions and placement.
Everything else takes place inside the component or without impacting the previous two sections, and thus they come last.
.declaration-order {
/* Positioning */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 100;
/* Box-model */
display: block;
float: right;
width: 100px;
height: 100px;
/* Typography */
font: normal 13px "Helvetica Neue", sans-serif;
line-height: 1.5;
color: #333;
text-align: center;
/* Visual */
background-color: #f5f5f5;
border: 1px solid #e5e5e5;
border-radius: 3px;
/* Misc */
opacity: 1;
}
Compared to <link>
s, @import
is slower, adds extra page requests, and can cause other unforeseen problems. Avoid them and instead opt for an alternate approach:
- Use multiple
<link>
elements - Compile your CSS with a preprocessor like Sass or Less into a single file
- Concatenate your CSS files with features provided in Rails, Jekyll, and other environments
<!-- Use link elements -->
<link rel="stylesheet" href="core.css">
<!-- Avoid @imports -->
<style>
@import url("more.css");
</style>
Place media queries as close to their relevant rule sets whenever possible. Don't bundle them all in a separate stylesheet or at the end of the document. Doing so only makes it easier for folks to miss them in the future. Here's a typical setup.
.element { ... }
.element-avatar { ... }
.element-selected { ... }
@media (min-width: 480px) {
.element { ...}
.element-avatar { ... }
.element-selected { ... }
}
When using vendor-prefixed properties, indent each property such that the declaration's value lines up vertically for easy multi-line editing.
/* Prefixed properties */
.selector {
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
box-shadow: 0 1px 2px rgba(0,0,0,.15);
}
In instances where a rule set includes only one declaration, consider removing line breaks for readability and faster editing. Any rule set with multiple declarations should be split into separate lines.
The key factor here is error detection—
e.g., a CSS validator stating you have a syntax error on Line 183. With a single declaration, there's no missing it. With multiple declarations, separate lines are a must for your sanity.
/* Single declarations on one line */
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }
/* Multiple declarations, one per line */
.sprite {
display: inline-block;
width: 16px;
height: 15px;
background-image: url(../img/sprite.png);
}
.icon { background-position: 0 0; }
.icon-home { background-position: 0 -20px; }
.icon-account { background-position: 0 -40px; }
Strive to limit the use of shorthand declarations to instances where you must explicitly set all the available values. Common overused shorthand properties include:
padding
margin
font
background
border
border-radius
Often times we don't need to set all the values a shorthand property represents. For example, HTML headings only set top and bottom margin, so when necessary, only override those two values. Excessive use of shorthand properties often leads to sloppier code with unnecessary overrides and unintended side effects.
Bad
.element {
margin: 0 0 10px;
background: red;
background: url("image.jpg");
border-radius: 3px 3px 0 0;
}
Good
.element {
margin-bottom: 10px;
background-color: red;
background-image: url("image.jpg");
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
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.
Avoid unnecessary nesting. Just because you can nest, doesn't mean you always should. Consider nesting only if you must scope styles to a parent and if there are multiple elements to be nested.
// Without nesting
.table > thead > tr > th { … }
.table > thead > tr > td { … }
// With nesting
.table > thead > tr {
> th { … }
> td { … }
}
For improved readability, wrap all math operations in parentheses with a single space between values, variables, and operators.
Bad
.element {
margin: 10px 0 @variable*2 10px;
}
Good
.element {
margin: 10px 0 (@variable * 2) 10px;
}
Code is written and maintained by people. Ensure your code is descriptive, well-commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name.
Be sure to write in complete sentences for larger comments and succinct phrases for general notes.
Comments have to be used to group specific CSS for a particular section of the page.
Bad
/* Modal header */
.modal-header {
...
}
Good
/* Wrapping element for .modal-title and .modal-close */
.modal-header {
...
}
Follow BEM convention: https://www.freecodecamp.org/news/css-naming-conventions-that-will-save-you-hours-of-debugging-35cea737d849/#the-bem-naming-convention
- Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural breaks in related class (e.g.,
.btn
and.btn-danger
). - Avoid excessive and arbitrary shorthand notation.
.btn
is useful for button, but.s
doesn't mean anything. - Keep classes as short and succinct as possible.
- Use meaningful names; use structural or purposeful names over presentational.
- Prefix classes based on the closest parent or base class.
- Use
.js-*
classes to denote behavior (as opposed to style) or classes that are added by JavaScript, but keep these classes out of your CSS. - Follow modularity and specificity: https://docs.ckan.org/en/2.9/contributing/css.html#modularity-and-specificity
It's also useful to apply many of these same rules when creating Sass and Less variable names.
Bad
.t { ... }
.red { ... }
.header { ... }
Good
.tweet { ... }
.important { ... }
.tweet-header { ... }
- Use classes over a generic element tag for optimum rendering performance.
- Avoid using several attribute selectors (e.g.,
[class^="..."]
) on commonly occurring components. Browser performance is known to be impacted by these. - Keep selectors short and strive to limit the number of elements in each selector to three.
- Scope classes to the closest parent only when necessary (e.g., when not using prefixed classes).
Bad
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }
Good
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }
Reference: http://codeguide.co/#css
-
Use Stylelint for linting. https://stylelint.io/user-guide/get-started
-
.stylelintrc
config
{
"extends": "stylelint-config-standard",
"rules": {
"indentation": 2,
"selector-list-comma-newline-after": "always",
"block-opening-brace-space-before": "always",
"block-closing-brace-newline-after": "always",
"declaration-colon-space-after": "always",
"declaration-block-semicolon-newline-after": "always",
"value-list-comma-space-after": "always",
"number-leading-zero": "never",
"color-hex-case": "lower",
"color-hex-length": "short",
"selector-attribute-quotes": "always",
"length-zero-no-unit": true,
"selector-max-specificity": "0,3,1",
"max-nesting-depth": 3,
"selector-nested-pattern": "^&"
}
}