Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap CSS Coding Standards

Bootstrap CSS Coding Standards

Syntax

Do you have questions about the terms used here? Please refer to the Wikipedia article on Cascading Style Sheets - Syntax。



/* Bad CSS */

.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 CSS */

.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;

}

declaration order

Related property declarations should be grouped together and arranged in the following order:

  1. Positioning
  2. Box model
  3. Typographic
  4. Visual

Since positioning can remove elements from the normal document flow and can override box model related styles, it comes first. The box model comes second because it determines the size and position of components.

Other properties only affect the *inside* of a component or do not affect the first two groups, so they come last.

For the complete list of properties and their ordering, please refer to Recess。



.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;

}

Do not use the @import

The difference between Compared with the tag,@import the directive is much slower, not only adding extra HTTP requests but also causing unpredictable problems. The alternatives are as follows:

Please refer to Steve Souders' articlefor more information.













Placement of media queries

Place media queries as close as possible to the relevant rules. Do not bundle them in a single stylesheet file or put them at the bottom of the document. If you separate them, they will only be forgotten later. A typical example is given below.



.element { ... }

.element-avatar { ... }

.element-selected { ... }



@media (min-width: 480px) {

  .element { ...}

  .element-avatar { ... }

  .element-selected { ... }

}

Prefixed properties

When using vendor-specific prefixed properties, use indentation to align the values of each property vertically, which makes multi-line editing easier.

In TextMate, use Text → Edit Each Line in Selection (Ctrl+Cmd+A). In Sublime Text 2, use Selection → Add Previous Line (Ctrl+Shift+Up) and Selection → Add Next Line (⌃⇧↓)。



/* Prefixed properties */

.selector {

  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);

          box-shadow: 0 1px 2px rgba(0,0,0,.15);

}

Single-line rule declarations

Forcontaining only one declarationFor readability and easy editing, it is recommended to put the statement on the same line. For styles with multiple declarations, the declarations should still be split across multiple lines.

The key to doing this is error detection -- for example, a CSS validator points out a syntax error on line 183. With one declaration per line, you will not overlook the error; with multiple declarations on one line, you have to analyze carefully to avoid missing errors.



/* 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; }

Shorthand property declarations

When you need to explicitly set all values, you should limit the use of shorthand property declarations as much as possible. Common cases of abusing shorthand property declarations are as follows:

In most cases, we do not need to specify all values for a shorthand property declaration. For example, HTML heading elements only need the top and bottom margin values set, so when necessary, just override those two values. Overusing shorthand property declarations leads to messy code and unnecessary overrides of property values, causing unexpected side effects.

A very good article on MDN (Mozilla Developer Network) aboutshorthand properties is very useful for users who are not familiar with shorthand property declarations and their behavior.



/* Bad example */

.element {

  margin: 0 0 10px;

  background: red;

  background: url("image.jpg");

  border-radius: 3px 3px 0 0;

}



/* Good example */

.element {

  margin-bottom: 10px;

  background-color: red;

  background-image: url("image.jpg");

  border-top-left-radius: 3px;

  border-top-right-radius: 3px;

}

Nesting in Less and Sass

Avoid unnecessary nesting. This is because just because you can use nesting does not mean you should. Only use nesting when you must restrict styles to a parent element (that is, descendant selectors) and there are multiple elements that need to be nested.



// Without nesting

.table > thead > tr > th { … }

.table > thead > tr > td { … }



// With nesting

.table > thead > tr {

  > th { … }

  > td { … }

}

Notes

Code is written and maintained by people. Please make sure your code is self-describing, well-commented, and easy for others to understand. Good code comments convey context and code purpose. Do not simply restate component or class names.

For longer comments, be sure to write complete sentences; for general annotations, concise phrases are fine.



/* Bad example */

/* Modal header */

.modal-header {

  ...

}



/* Good example */

/* Wrapping element for .modal-title and .modal-close */

.modal-header {

  ...

}

Class naming

The guidelines listed above can also be referenced when naming Sass and Less variables.



/* Bad example */

.t { ... }

.red { ... }

.header { ... }



/* Good example */

.tweet { ... }

.important { ... }

.tweet-header { ... }

Selector

Further reading:



/* Bad example */

span { ... }

.page-container #stream .stream-item .tweet .tweet-header .username { ... }

.avatar { ... }



/* Good example */

.avatar { ... }

.tweet-header .username { ... }

.tweet .avatar { ... }

Code organization



/*

 * Component section heading

 */



.element { ... }





/*

 * Component section heading

 *

 * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.

 */



.element { ... }



/* Contextual sub-component or modifer */

.element-heading { ... }

Editor configuration

Configure your editor with the following settings to avoid common code inconsistencies and differences:

Refer to the documentation and add this configuration information to your project's .editorconfig file. For example:Example .editorconfig in Bootstrap. For more information, please refer to about EditorConfig。