Skip to content

Latest commit

 

History

History
352 lines (271 loc) · 7.33 KB

css.md

File metadata and controls

352 lines (271 loc) · 7.33 KB

CSS Style Guide

A reasonable style guide for modern CSS development.

  1. Whitespace
  2. Formatting
  3. Declaration Order
  4. Naming Conventions
  5. Comments
  6. Resources
  7. License

"Part of being a good steward to a successful project is realising that writing code for yourself is a Bad Idea™. If thousands of people are using your code, then write your code for maximum clarity, not your personal preference of how to get clever within the spec."

Idan Gazit

Use soft tabs set to 4 spaces.

/* Bad */
.selector {
..display: block;
}

/* Good */
.selector {
....display: block;
}

Place 1 space before the leading brace and place 1 space after the colon of a declaration.

/* Bad */
.selector{
    float:left;
}

/* Good */
.selector {
    float: left;
}

Place an empty newline at the end of the file.

/* Bad */
.selector {
    clear: both;
}
/* Good */
.selector {
    clear: both;
}

[⬆]

The chosen code format ensures that code is: easy to read; easy to clearly comment; minimises the chance of accidentally introducing errors; and results in useful diffs and blames.

  • Use 1 selector per line in multi-selector rulesets
  • Use 1 declaration per line in a declaration block
  • Use lowercase and shorthand hex values
  • Use double quotes "" e.g. input[type="checkbox"]
  • Where allowed, avoid specifying units for zero-values e.g. margin: 0
  • Include a space after each comma in comma-separated property or function values
  • Include a semi-colon at the end of the last declaration in a declaration block
  • Place the closing brace of a ruleset in the same column as the first character of the ruleset
  • Separate each ruleset by a blank line
/* Bad */
.selector-1, .selector-2, .selector-3 { display: block; font-family: helvetica,arial,sans-serif; background: #fff; background: linear-gradient(#fff,rgba(0,0,0,.8)) }
.selector-a, .selector-b { padding: 10px }

/* Good */
.selector-1,
.selector-2,
.selector-3 {
    display: block;
    font-family: helvetica, arial, sans-serif;
    background: #fff;
    background: linear-gradient(#fff, rgba(0, 0, 0, 0.8));
}

.selector-a,
.selector-b {
    padding: 10px;
}

[⬆]

Declare structurally important properties prior to all others.

.selector {
    /* Positioning */
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 10;

    /* Display and Box Model */
    display: inline-block;
    float: left;
    clear: both;
    overflow: hidden;
    width: 100px;
    height: 100px;
    margin: 10px;
    padding: 10px;
    border: 1px solid #ccc;

    /* Typography */
    font-family: sans-serif;
    font-size: 16px;
    line-height: 24px;
    text-align: right;
    vertical-align: baseline;
    white-space: nowrap;
    color: #333;

    /* Others */
    outline: 0;
    list-style: circle;
    background: #fff;
    opacity: 0.75;
    cursor: default;
    content: "";
}

[⬆]

Elements that occur exactly once inside a page should use IDs, otherwise, use classes. When in doubt, use a class name.

  • Good candidates for ids: header, footer, modal popups
  • Bad cadidates for ids: navigation, item listings

Use meaningful or generic ID and class names.

/* Bad */
#yee-1901 {
    float: left;
}

.button-green {
    background: #060;
}

/* Good */
#gallery {
    float: left;
}

.primary {
    background: #060;
}

Use ID and class names that are as short as possible but as long as necessary.

/* Bad */
#navigation {
    display: block;
}

.atr {
    font-weight: bold;
}

/* Good */
#nav {
    display: block;
}

.author {
    font-weight: bold;
}

Separate words in ID and class names with a hyphen.

/* Bad */
.demoimage {
    border: 1px solid #ccc;
}

.error_text {
    color: #f00;
}

/* Good */
.demo-image {
    border: 1px solid #ccc;
}

.error-text {
    color: #f00;
}

Avoid qualifying ID and class names with type selectors.

/* Bad */
ul#example {
    list-style: none;
}

div.error-block {
    background: #fff0f0;
}

/* Good */
#example {
    list-style: none;
}

.error-block {
    background: #fff0f0;
}

All code should be lowercase: This applies to element names, attributes, attribute values, selectors, properties and property values.

/* Bad */
BODY {
    margin: 0;
}

/* Good */
body {
    margin: 0;
}

[⬆]

/* ==========================================================================
   Section comment block
   ========================================================================== */

/* Sub-section comment block
   ===================================== */

/**
 * This is a docBlock style comment
 *
 * This is a longer description of the comment, describing the code in more
 * detail. Limit these lines to a maximum of 80 characters in length.
 */

/* Basic comment */

Example:

/* ==========================================================================
   Typography
   ========================================================================== */

p {
    margin: 0 0 20px;
    font-size: 16px;
}

/* Headings
   ===================================== */

h1,
h2,
h3,
h4,
h5,
h6 {
    margin: 0 0 20px;
    font-family: serif;
    font-weight: bold;
    text-rendering: optimizelegibility; /* Fix the character spacing for headings */
}

[⬆]

Blogs

Other Styleguides

[⬆]

(The MIT License)

Copyright (c) 2012 Nathan Staines

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[⬆]