Front-end methodologies discusses common solutions like SMACSS and ITCSS for organizing CSS code into reusable, maintainable components. It also covers approaches like OOCSS, BEM, and CSS Modules. CSS Modules in particular scopes class names to prevent collisions and makes extending classes problematic.
3. @arashmanteghi#coderconf
Introduction
Problems
There are several things that bother us in CSS
The most common annoyances we have are:
• repeating common code
• browser prefixes
• lack of comments
• over qualified selectors
• poor class names
Goal: readable, reusable and maintainable code
4. Introduction
Common Solutions
Split a large stylesheet into multiple smaller pieces
/stylesheet
style.css
/stylesheets
reset.css
scaffolding.css
layout.css
typography.css
/components
nav-bar.css
search-bar.css
signup-form.css
@arashmanteghi#coderconf
5. Introduction
Common Solutions
Better variable organization
/* Background Colors */
$background:
$header-background:
$content-background:
/* Colors */
$heading-color:
$link-color:
/* Sizes */
$header-height:
$footer-height:
/* use variable */
a {color: $link-color}
/* Background Colors */
$color-bg:
$color-bg-header:
$color-bg-content:
/* Colors */
$color-heading:
$color-link:
/* Sizes */
$height-header:
$height-footer:
/* use variable */
a {color: $color-link}
/* Colors */
$colors = (
bg: value,
bg-header: value,
bg-content: value,
heading: value,
link: value
);
/* use variable */
a {color: map-get($colors,link)}
@arashmanteghi#coderconf
6. Introduction
Common Solutions
Order your CSS properties
39%
45%
2%
14%
@arashmanteghi#coderconf
Random
Grouped by type
By line length
Alphabetical
0 12.5 25 37.5 50
39%
45%
2%
14%
7. Necessity
Should I use it?
typeface
colors
images/icons
composition
“We’re not designing pages, we’re designing systems of
components” ~ Stephen Hay
@arashmanteghi#coderconf
8. MaintanableCSS
Semantics
<!-- Bad -->
<div class=“red pull-left”>
<div class=“grid row”>
<div class=“col-xs-4”>
Name something based on what it is, not how it looks or behaves
<!-- Good -->
<div class=“header”>
<div class=“basket”>
<div class=“product”>
<div class=“searchResults”>
It’s not clear at all what this HTML
represents.
Here I know exactly what I am looking
at. I know the intention of what this
HTML represents.
So why else should we use semantic class names?
Because it’s easier to understand.
We are building responsive websites.
Semantic class names are easier to find.
The standards recommend it.
@arashmanteghi#coderconf
11. Reuse
MaintanableCSS
Submit Delete!
.SubmitButton { /* common styles */ }
.SubmitButton-normal { @extend .SubmitButton; /* blue colors */ }
.SubmitButton-danger { @extend .SubmitButton; /* red colors */ }
<button class=“SubmitButton-normal”> Submit </button>
<button class=“SubmitButton-danger”> Delete! </button>
Reuse causes bloat. Reuse breaks semantics. But using preprocessors can help us.
.SubmitButton, .SubmitButton-normal, .SubmitButton-danger {
/* common styles */
}
.SubmitButton-normal { /* blue colors */ }
.SubmitButton-danger { /* red colors */ }
What about Mixins? They can be useful too, but should be designed with caution.
@arashmanteghi#coderconf
12. Conventions
MaintanableCSS
Conventions can be a bone of contention amongst engineers, but what matters
most is readability and consistency.
/* Square brackets denote optional parts */
.<moduleName>[—<componentName>]—[<state>] { }
/* module container/root */
.searchResults {}
/* components of a module */
.searchResults-heading {}
.searchResults-item {}
/* state: such as AJAX loading */
.searchResults-isLoading {}
@arashmanteghi#coderconf
Modifiers are similar to states in that they can change or override the style of a
module.
13. SMACSS
What is SMACCS
@arashmanteghi#coderconf
SMACSS is more style guide than rigid framework
Every project needs some organization. There are five types of categories:
• Base
• Layout
• Module
• State
• Theme
Each category has certain guidelines that apply to it.
14. ITCSS
Manage Large CSS Projects
@arashmanteghi#coderconf
Settings
Tools
Generic
Elements
Objects
Components
Trumps
Each layer contains a series of partials. Recommend naming convention is:
_<layer>.<partial>.scss
For example: _settings.colors.scss , _elements.headings.scss , _components.tabs.scss
16. OOCSS
The Principles of OOCSS
@arashmanteghi#coderconf
An approach for writing CSS that’s reusable, maintainable, and standards-based.
Separate structure and skin Separate container and content
18. BEM
Block, Element, Modifier
@arashmanteghi#coderconf
A popular naming convention for classes in HTML and CSS
• Block is a top-level abstraction of a new component, for example a button.
• Elements, can be placed inside and these are denoted by two underscores following
the name of the block
• Modifiers can manipulate the block and Elements.
.dog { /* some styles… */}
.dog__tail { /* some styles… */ }
.dog --small { /* some styles… */ }
19. @arashmanteghi#coderconf
BEM
Block, Element, Modifier
/* Block component */
.btn {}
/* Element that depends upon the block */
.btn__price {}
/* Modifier that changes the style of the block */
.btn --success {}
.btn --info {}
/* Block component */
.btn {
/* Element that depends upon the block */
&__price {}
/* Modifier that changes the style of the block */
& --success {}
& --info {}
}
20. CSS Modules
What are CSS Modules
@arashmanteghi#coderconf
CSS files in which all class names and animation names are scoped locally by default.
import styles from “./styles.css”;
element.innerHTML = ‘<h1 class=“{styles.title}”>
An example heading
</h1>’;
._styles__title_309571057 {
background-color: red;
}
<h1 class=“_styles__title_309571057”>
An example heading
</h1>
On the other hand, it is still possible to define global classes (with :global()) such as
helpers
21. How It Works
@arashmanteghi#coderconf
CSS Modules
{
test: /.css/,
loader: ExtractTextPlugin.extract(‘css?modules
&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]’)
}
with the help of Webpack or Browserify
var browserify = require('browserify')();
browserify.add('./main.js');
browserify.plugin(require('css-modulesify'), {
rootDir: __dirname,
output: './path/to/my.css'
});
browserify.bundle();
bit.ly/css--modules