# CSS Conventions
# 1.1 Class names
Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural breaks in related class (e.g., .btn and .btn-danger).
/* Bad example */ .pageHead { } .sub_content { }
/* Good example */ .page-head { } .sub-content { }
Class Naming/ BEM Convention (opens new window)
- 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.
/* Bad example */
.t {
}
.red {
}
.big {
}
/* Good example */
.article {
}
.article__title {
}
.nav {
}
.nav-primary {
}
- Use BEM methodology(block__element--modifier).
block
block--modifier
block__element
block__element--modifier
- Use
.js-*
classes to denote behavior (as opposed to style), but keep these classes out of your CSS.
/* example */
.js-scroll {
}
.js-toggle {
}
- Not location-based.
Never use location-based styling. This means a block is never styled different because it is within another block.
Objects should have "modifiers" instead of location-related styles .block--large {}
instead of #sidebar .block {}
.
When styling block, keep styles encapsulated, this mean: don't add styles for this block on it's elements through other selectors example for .nav
:
/* Bad example */
ul .nav {
}
div .nav {
}
header .nav {
}
/* Good example */
.nav {
}
.card {
}
.intro {
}
# 1.2 Styles Declaration order
- Related property declarations should be grouped together following the order:
- Positioning
- Box model
- Typographic
- Visual
- Misc
.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-family: 'Helvetica Neue', sans-serif;
font-size: 13px;
font-weight: 700;
line-height: 1.5;
color: #333;
text-align: center;
/* Visual */
background-color: #f5f5f5;
border: 1px solid #e5e5e5;
border-radius: 3px;
/* Misc */
opacity: 1;
}
- 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.
# 1.3 Positioning components within containers
- Components within a container shouldn’t care about their positioning at all.
- The container should position the components it contains.This also means that containers are in charge of spacing their components away from each other properly. E.g. by using flex-box.
# 1.4 ITCSS Conventions
based on ITCSS (opens new window)
- Settings – used with pre-processors and contain fonts, colors definitions, etc. or so called variables.
- Tools - globally used mixins and functions. It’s important not to output any CSS in the first 2 layers.
- Generic - reset and/or normalize styles, box-sizing definition, etc. This is the first layer which generates actual CSS.
- Elements - styling for bare HTML elements (like H1, A, etc.). These come with default styling from the browser so we can redefine them here.
- Objects - (
.o-object
) - Those are giving layout - Components – (
.c-component-name
) specific UI components. This is where majority of our work takes place and our UI components are often composed of Objects and Components - Utilities - place for utility classes (
.alignleft
,.alignright
,.text-left
,.text-right
, etc). Those should have one or two declaration at most.
# 1.5 Internal CSS rules
- All SVG icons' style is stored in the
/assets/styles/components/_svg.scss
file. This includes the dimensions of the icons and their colors. If a modifier is needed, it is also added in this file. - All PNG icons' style is generated automatically by the PNG Sprite Generator and Webpack. All PNG files should be prefixed with the
ico-
prefix and used with this prefix in the HTML (<i class="ico-test">
) - The most common text styles should be set as variables in the
/assets/styles/settings/_text.scss
file. - Text related properties for headings should be set in the
assets/styles/elements/_headings.scss
file. This includesfont-family
,font-size
,line-height
,font-weight
but notcolor
or any box model properties. - All colors must be set as variables in the
assets/styles/settings/_colors.scss
file. It is not allowed to use hex, rgb(a) or hsl(a) values outside of this file. If you need to create a semi-transparent color out of a variable, use the SASS' built inrgba()
function. - The width of the site container element (
shell
) should be set in theassets/styles/settings/_variables.scss
file. - Prefer usage of flexbox for layout and use the available mixins found in the
assets/styles/tools/_flexbox.scss
file. - Always add
:hover
states using the@include hover
statement. The media query can be found in theassets/styles/tools/_mixins.scss
file. - All components should be stored in the
assets/styles/components
folder and each component should have its own file. - All objects should be stored in the
assets/styles/objects
folder and each object should have its own file. Ideally you don't need to add more objects than what we have right now. - All lists (unordered or ordered) are stored inside the
assets/styles/components/_lists.scss
file and are prefixed with thec-list-
prefix.
# Resources
Google HTML/CSS Style Guide (opens new window)
Principles of writing consistent/, idiomatic CSS (opens new window)
Code Guide (opens new window)
Css guidelines (opens new window)
Sass style guide (opens new window)
Sass-guidelin (opens new window)