Home

Awesome

.bc-style-guide {

The so-called "nico-style" brought to markdown. Because Style Guide.

This style guide is a jab at solving collisions between CSS class names, and issues that ultimately lead to confusion, having to use !important rules, copying and pasting style declarations, and similarly awful aspects of CSS development.

Goal

These suggestions aren't set in stone, they aim to provide a baseline you can use in order to write more consistent codebases. To maximize effectiveness, share the styleguide among your co-workers and attempt to enforce it. Don't become obsessed about code style, as it'd be fruitless and counterproductive. Try and find the sweet spot that makes everyone in the team comfortable developing for your codebase, while not feeling frustrated that their code always fails automated style checking because they added a single space where they weren't supposed to. It's a thin line, but since it's a very personal line I'll leave it to you to do the drawing.

Astonishingly, this style guide won't do anything for you that you're not able to figure out for yourself. That means you won't see the benefits unless you try and follow most of the conventions laid out next.

Use together with bevacqua/js for great good!

Feel free to fork this style guide, or better yet, send Pull Requests this way!

Table of Contents

  1. Namespaces
  2. Classes
  3. Attributes
  4. id attribute
  5. Tag Names
  6. Selectors and Nesting
  7. Organization
  8. Presentation-Specific vs Layout-Specific Styles
  9. Styles
  10. Media Queries
  11. Frameworks and Vendor Styles
  12. Languages
  1. License

Namespaces

Components should always be assigned a unique namespace prefix.

Consider the following example, where we assigned ddl to a drop down list component. Take note of the class names.

Good
.ddl-container {
  // ...
}

.ddl-item-list {
  // ...
}

.ddl-item {
  // ...
}
Bad
.item-list {
  // ...
}

.dropdown-item-list {
  // ...
}

.xyz-item-list {
  // ...
}

Classes that are meant to be shared among a large set of elements, or provide reusable styles, should be grouped under a universal namespace, such as uv.

Good
.uv-clearfix {
  // ...
}
Bad
.clearfix {
  // ...
}

See Selectors and Nesting for information in regard to how styles should be overridden

Classes

Class names must follow a few rules.

Good
.ddl-item {
  // ...
}

.ddl-selected {
  // ...
}

.ddl-item-selected {
  // ...
}
Bad
.ddlItem {
  // ...
}

.ddl-item-container-text {
  // ...
}

.ddl-foo-bar-baz {
  // ...
}

Attributes

Attributes make decent selectors from time to time. Some ground rules apply.

Good
[href] {
  // ...
}
Bad
a[href] {
  // ...
}

[href^='http'] {
  // ...
}

id attribute

While the id attribute might be fine in HTML and JavaScript, it should be avoided entirely inside stylesheets. Few reasons.

Good
.ur-name {
  // ...
}
Bad
#ur-name {
  // ...
}

Just assign a class name to the element.

Tag Names

Tag names in selectors follow a few rules.

Good
button {
  padding: 5px;
  margin-right: 3px;
}

.ddl-button {
  background-color: #f00;
}
Bad
.ddl-container button {
  background-color: #f00;
}

Selectors and Nesting

Styles shouldn't need to be nested more than three (four at worst) levels deep. This includes pseudo-selectors. If you find yourself going further, think about re-organizing your rules (either the specificity needed, or the layout of the nesting).

Good
.sg-title-icon:before {
  // ...
}

.dg-container .sg-title {
  font-size: 1.1em; // larger segment title inside dialogs
}
Bad
.dg-container .sg-container .sg-title {
  font-size: 1.1em;
}

.dg-container .sg-title span:before {
  // ...
}

If a component needs to be different within another component, these rules apply.

Suppose you have a User List component .ul-* and a User Card component .uc-*.

Good
<div class='ul-container'>
  <div class='uc-container'>
    <span class='uc-name ul-card-name'>John Doe</span>
  </div>
</div>
.ul-card-name {
  // ...
}
Okay
<div class='ul-container'>
  <div class='uc-container'>
    <span class='uc-name'>John Doe</span>
  </div>
</div>
.ul-container .uc-name {
  // ...
}
Bad
<div class='ul-container'>
  <div class='uc-container'>
    <span class='uc-name uc-name-in-ul'>John Doe</span>
  </div>
</div>
.uc-name-in-ul {
  // ...
}

Organization

Ideally, you should keep your stylesheets separated in different files. Either of the approaches below is fine. The former is prefered.

A few rules apply.

Presentation-Specific vs Layout-Specific Styles

Presentation-Specific styles are those that only alter the visual design of the element, but don't change its dimensions or position in a meaningful way. The examples below are presentation-specific.

Layout-Specific Styles are those that change the dimensions or positioning of DOM elements. These are mostly layout-specific.

Where possible, it's suggested to explicitly split styles into these two categories. The explicit differentiation could be made in a few different ways.

Good
.foo {
  position: fixed;
  top: 8px;
  right: 8px;
  padding: 2px;
  font-weight: bold;
  background-color: #333;
  color: #f00;
}
.foo {
  position: fixed;
  top: 8px;
  right: 8px;
  padding: 2px;

  font-weight: bold;
  background-color: #333;
  color: #f00;
}
.foo {
  position: fixed;
  top: 8px;
  right: 8px;
  padding: 2px;
}

.foo {
  font-weight: bold;
  background-color: #333;
  color: #f00;
}
Bad
.foo {
  font-weight: bold;
  background-color: #333;
  color: #f00;
  position: fixed;
  top: 8px;
  right: 8px;
  padding: 2px;
}

.foo {
  right: 8px;
  color: #f00;
  padding: 2px;
  top: 8px;
  background-color: #333;
  font-weight: bold;
  position: fixed;
}

Styles

These rules apply to your CSS property values

Good
.btn {
  color: #222;
}

.btn-error {
  color: #f00;
}
Bad
.btn-red {
  color: #f00 !important;
}

.btn {
  color: #222;
}

Media Queries

If you are reading this, I salute you. You're almost as boring as I am. I'm more boring because I actually wrote the damn thing. It's not a contest, though.

A few rules apply to media queries.

Good
.co-field {
  width: 120px;
}

@media only screen and (min-width: 768px) {
  .co-field {
    width: 400px;
    color: #f00;
  }
}
Bad
.co-field {
  width: 400px;
  color: #f00;
}

@media only screen and (max-width: 768px) {
  .co-field {
    width: 120px;
    color: initial;
  }
}

Frameworks and Vendor Styles

You should shy away from all of these. A few rules apply.

Languages

Some rules apply to stylesheet, regardless of language.

Good
.foo {
  color: #f00;
}

.foo,
.bar,
.baz {
  color: #f00;
}
Bad
.foo {
    color: #f00;
}

.foo, .bar, .baz {
  color: #f00;
}

.foo {
  color: red;
}

Not Stylus

These rules apply to every language except Stylus.

Good
.foo {
  color: #f00;
}
Bad
.foo{
  color: #f00;
}

.foo {
  color:#f00;
}

.foo {
  color: #f00
}

Not CSS

Rules applicable to most pre-processor languages.

Good
// foo
.bar {
  // ...

  .baz {
    ///...
  }
}
Bad
/* foo */
.bar {
  // ...
}

.bar .baz {
  // ...
}

Stylus

Rules specific to Stylus.

Good (Stylus)
// foo
.foo
  color #f00
.foo
  color #f00

  .bar
    padding 2px
Bad (Stylus)
/* foo */
.foo {
  color: #f00;
}
.foo {
  color: #f00;
}

.foo .bar {
  padding: 2px;
}

License

MIT

Fork away!

}