CSS Architecture For Design Systems - Brad Frost
CSS Architecture For Design Systems - Brad Frost
my name is
brad frost
work
workshops
book
blog
contact
To give a bit of a context for the project, we were tasked with creating a
design system and style guide meant to serve the organization’s
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 1/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 3/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
global namespace
All classes associated with the design system are pre xed with a
global namespace, which is the Company Name followed by a
hyphen:
.cn-
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 4/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
bem syntax
BEM stands for “Block Element Modi er”, which means:
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 6/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-c-btn--secondary
cn- is the global namespace for all styles coming from the design
system.
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 7/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-l-grid__item
.cn-c-primary-nav__submenu
Again, there’s no doubt these classes are more verbose than most other
approaches out there, but for this speci c system these conventions
made a lot of sense.
other tricks
being explicit with minutia
In order to prevent things from falling apart we detailed how to handle a
lot of the minor details like comments, spacing around code blocks, tabs
vs spaces, etc. Thankfully, Harry Roberts has put together an excellent
and comprehensive resource called CSS Guidelines, which we used as
our baseline for these kinds of conventions. We combed through
everything and agged areas where we planned on deviating from what
Harry spelled out.
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 9/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-c-primary-nav {
/**
* Nav appearing in header
* 1) Right-align navigation when it appears in the header
*/
.cn-c-header & {
margin-left: auto; /* 1 */
}
}
This means all my primary navigation styles can be found in the primary
navigation Sass partial, rather than splitting them between multiple les.
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 10/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
. Media queries
. Parent selectors
. States
For modi ers, if the rule is only a few lines long, the modi er can be
nested inside the parent like so:
.cn-c-alert {
border: 1px solid gray;
color: gray;
/**
* Error Alert
*/
&--error {
border-color: red;
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 11/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
color: red;
}
}
.cn-c-alert {
border: 1px solid gray;
color: gray;
}
.cn-c-alert--error {
border-color: red;
color: red;
}
For longer style blocks we didn’t nest the modi er code as it reduced
the legibility of the code.
2. media queries
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 12/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-c-primary-nav {
/* Base styles */
/**
* 1) On larger displays, convert to a horizontal list
*/
@media all and (min-width: 40em) {
display: flex;
}
}
.cn-c-primary-nav {
/* Base styles */
}
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 13/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
}
}
3. parent selectors
The design system will make use of Sass’s parent selector mechanism.
This allows all rules for a given component to be maintained in one
location.
.cn-c-primary-nav {
/**
* Nav appearing in header
* 1) Right-align navigation when it appears in the header
*/
.cn-c-header & {
margin-left: auto; /* 1 */
}
}
.cn-c-header .cn-c-primary-nav {
display: flex;
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 14/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
All styles for cn-c-primary-nav should be found in one place, rather than
scattered throughout multiple partial les.
4. states
.cn-c-btn {
background: blue;
&:hover, &:focus {
background: red;
}
}
.cn-c-btn {
background: blue;
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 15/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-c-btn:hover, .cn-c-btn:focus {
background: red;
}
States can also take the form of utility classes, such as is- and has-:
.cn-c-accordion__panel {
overflow: hidden;
max-height: 0;
&.cn-is-active {
max-height: 40em;
}
}
.cn-c-accordion__panel {
overflow: hidden;
max-height: 0;
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 16/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
.cn-c-accordion__panel.cn-is-active {
max-height: 40em;
}
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 17/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
I work on plenty of projects where I can get by with strings like .table-
of-contents li a, but those projects are mostly managed by me. For
client projects that involve working in a team environment, I’m very much
gravitating towards more verbose, explicit syntaxes like I described
above because they provide less room for people to screw things up. It’s
great to see other teams like Sparkbox come to similar conclusions.
After a few weeks away from the project, we’re returning to continue
work on version 1.1 of the design system. I’m looking forward to coming
back to this code base and seeing how quickly I can get re-acclimated
with it!
hey there!
I'm Brad Frost, a web designer, speaker,
consultant, writer, and musician located in
beautiful Pittsburgh, PA.
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ d 18/21
2/18/2021 CSS Architecture for Design Systems | Brad Frost
atomic design
I wrote a book called Atomic Design,
which covers all that goes into creating
and maintaining e ective design
systems. You can read it online and
order the ebook.
contact music
Interested in having me conduct a workshop at your I play music with my wife and brother in our home studio.
company? Have a project that could use some consulting Nothing serious; I just love playing music.
or frontend help? Have a question? Get in touch!
https://bradfrost.com/blog/post/css-architecture-for-design-systems/ 21/21