A few simple guidelines and ideas I wrote down for myself to craft consistent and maintainable CSS. // This is a living document and is subject to change.
- Think OOCSS and build reusable modules and patterns
- Don't be afraid of additional non-semantic
div
's and classes if they make sense and help organizing and modularizing the code. Think separation of content and container. #OOCSS - Responsive Design is more than just squishing the content. Think Progressive Enhancement/Responsible Responsive Design
- Start mobile first
- Use inline SVGs if possible
Keeping a consitent declaration order throughout the all CSS files helps organization and speeds up the development process.
.class {
/* Positioning */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
/* Box-Model and Display */
display: block;
overflow: hidden;
width: 100px;
height: 50px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 20px;
margin-left: 10px;
padding: 10px;
/* Typography */
color: #000;
font-family: Georgia, serif;
font-size: 1rem;
line-height: 1.3;
/* Visuals */
background-color: #ccc;
border: 1px solid #000;
border-radius: 3px;
opacity: .8;
}
Keep files as simple, short and modular as possible. Each one has a heading:
/*------------------------------------*\
File Heading
\*------------------------------------*/
Each section in a file has a heading:
/**
* Section Heading or multi-line comment
*/
.class {
position: relative;
}
Each section is followed by two blank lines. Sometimes one just needs a short comment:
/* Simple one-line comment */
.class {
color: red;
}
style.scss:
/**
* Assets
*/
@import "_assets/vars";
@import "_assets/mixins";
@import "_assets/normalize";
@import "_assets/fonts";
/**
* Modules
*/
@import "_modules/site-nav";
@import "_modules/ad-box";
/* and so on... */
This will result in style.min.css.
.nav {
/* ... */
}
.nav--small {
/* ... */
}
.nav__item {
/* ... */
}
.nav__item--new {
/* ... */
}
$blue: #0e649a;
$blue__dark: #083654;
$blue__dark--hover: #235f85;
$header__height: 50px;
$header__height--fixed: 20px;
.class {
margin-bottom: 20px;
.class-2 {
background-color: blue;
p {
font-size: 1.3rem;
}
}
}
Do:
.class {
color: #000;
}
Don't do:
#id {
color: #000;
}
header {
background-color: blue;
}
div.ad-box {
border: 1px solid red;
}
But you can select html elements like p
, blockquote
, and other text-based ones.
<a class="more-link js_more-link">Read more...</a>
<nav class="page-nav" id="js_page-nav"></nav>
.class {
@extend %clearfix;
padding: 10px;
font-size: 1.3rem;
}
@charset "UTF-8";
Sublime Text Settings:
"tab_size": 4,
"translate_tabs_to_spaces": true,
Sublime Text Setting:
"rulers": [80]
.class-1,
.class-2,
.class-3 {
font-size: 1.5rem;
}
An exception could be a state style like
.is-false {
color: red !important;
}
If you only want to set one value, be explicit, don't set values you don't have to.
Use:
.class {
margin-bottom: 20px;
}
Don't use:
.class {
margin: 0 0 20px 0;
}
.class {
top: 0;
opacity: .7;
}
.class {
color: #fff;
background-color: #000;
}
... which just happen to work for no particular reason.
.class {
position: relative;
top: 3px;
}
.class {
box-sizing: border-box;
}
Will be automatically outputted as
.class {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
And don't name breakpoints after actual devices like iphone
or tablet
. Use
arbitrary names.
@mixin breakpoint($point) {
@if $point == bp1 {
@media (min-width: 460px) {
@content;
}
}
@if $point == bp2 {
@media (min-width: 640px) {
@content;
}
}
@elseif $point == retina {
@media (-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi)
(-o-min-device-pixel-ratio: 5/4) {
@content;
}
}
}
.class {
padding: 10px;
@media (min-width: 500px) {
padding: 20px;
}
}
.class {
font-size: 2rem;
@include breakpoint(bp1) {
font-size: 1.5rem;
}
}
THIS IS NOT YET TESTED AND USED:
$font-size__base: 16;
$font-size__body: font-size__base +px;
@mixin font-size($size) {
font-size: $size +px;
font-size: $size / $font-size__base +rem;
}
body {
font-size: $font-size__body +px;
}
.class {
@include font-size(20);
}
/* CSS Output: */
.class {
font-size: 20px; /* Fallback */
font-size: 1.25rem;
}