#Workshop 4 - Scalable and Maintainable CSS
#Workshop 4 - Scalable and Maintainable CSS
- best practices -
● Key Concepts in CSS
● What is SMACSS
How is it calculated?
We can use this - Calculator link
a.button {} /* Bad */
#sidebar ul > li > ul { }
● Separation of concerns is crucial. HTML is a
garden that many people touch, and they may
/* Good */
need to change the nature of the button and
change the tag to <a> tab button or a <span> .submenu { }
tag. The solution to this is use a simple class.
.product-card__button–selected {
…
2 underscores 2 dashes
}
Components
BLOCK
ELEMENT
MODIFIER
<figure class="photo">
<img class="photo__img photo__img--framed" src="me.jpg">
<figcaption class="photo__caption photo__caption--large">Look at me!</figcaption>
</figure>
<style>
.photo__img--framed {
/* incremental style changes */
}
.photo__caption--large {
/* incremental style changes */
}
</style>
What is SMACSS?
Pronounced “smacks”
What is SMACSS?
The main goal of the approach is to reduce the amount of code and simplify code support.
css/
Base |
|– base/
| |– reset.css # Reset
These are styles of the main website | |– typography.css # Typography rules
elements – body, input, button, ul, ol, etc. | |– utils.css # Utils rules
Default styles using type, descendent or | |– variables.css # Variables
child selectors, and pseudo-classes. | ... # Etc…
/* Type selector */
a {
color: #fff;
}
/* Pseudo-class */
a:hover{
color: #ababab;
}
css/
Layout |
|– layout/
| |– navigation.css # Navigation
Use for major page sections (e.g., | |– grid.css # Grid system
header) with an l-prefix or layout-prefix | |– header.css # Header
to clearly identify layout styles. | |– footer.css # Footer
| |– sidebar.css # Sidebar
| |– forms.css # Forms
| ... # Etc…
.l-footer {
background–color: #fff;
}
css/
Module |
|– module/
| |– button.css # Button
Blocks that can be used multiple times | |– modal.css # Modal
on a single page. For module classes, it | |– carousel.css # Carousel
is not recommended to use id and tag | |– dropdown.css # Dropdown
selectors (for reuse and context | ... # Etc…
independence, respectively).
.nav {
/* common nav styles */
}
.nav-bar {
/* specific */
}
State |
|– state/
| |– state.css # State
Used for variations that indicate a state | ... # Etc…
change, usually triggered by user
interaction.
.is-hidden {
State class names use an is- prefix
such as is-active or is-hidden. visibility: hidden;
Theme |
|– theme/
| |– dark.css # Dark mode
Styles that make up the | |– light.css # Light mode
look and feel. | ... # Etc…
/* module-name.css */ /* module-name.css */
.module { .module {
border: 1px solid; border: 1px solid;
} }
/* theme.css */ .theme-blue .module {
.module { border-color: blue;
border-color: blue; }
}
BEM + SMACSS?
The ideal solution?
Benefits of BEM + SMACSS
●
●
●
or the use of big size selectors
Easier to reuse, recycle or repurpose
Promotes consistency
Ease of code maintenance
+
Other CSS methodologies
● 7 - 1 pattern (SASS)
● ITCSS (BEM + IT = BEMIT) Inverted Triangle CSS (itcss.io)
● OOCSS (Object-oriented CSS)
● SUITCSS
● Atomic CSS (Also known as Functional CSS) atomicdesign.bradfrost.com
● And more...
LAYOUT MODES & ALGORITHMS
How are layouts calculated?
Layout modes Which layout will be used here?
● Flow .main-container {
● Flexbox z-index: 10;
● Float }
● Positioned
● Grid
● Table
Flow is the default layout algorithm
● Multi-column (e.g. column-count: 3;) used for non-table HTML elements.
It´s used for creating document-style
Every element will have its layout layouts, similar to word.
calculated using a primary layout mode.