内容简介:Maintaining a large-scale CSS project is hard. Over the years, we’ve witnessed different approaches aimed at easing the process of writing scalable CSS. In the end, we all try to meet the following two goals:For the past year and a half, I’ve been working
Maintaining a large-scale CSS project is hard. Over the years, we’ve witnessed different approaches aimed at easing the process of writing scalable CSS. In the end, we all try to meet the following two goals:
- Efficiency : we want to reduce the time spent thinking about how things should be done and increase the time doing things.
- Consistency : we want to make sure all developers are on the same page.
For the past year and a half, I’ve been working on a component library and a front-end framework called CodyFrame . We currently have 220+ components. These components are not isolated modules: they’re reusable patterns, often merged into each other to create complex templates.
The challenges of this project have forced our team to develop a way of building scalable CSS architectures. This method relies on CSS globals, BEM, and utility classes .
I’m happy to share it!
CSS Globals in 30 seconds
Globals are CSS files containing rules that apply crosswise to all components (e.g., spacing scale, typography scale, colors, etc.). Globals use tokens to keep the design consistent across all components and reduce the size of their CSS.
Here’s an example of typography global rules:
/* Typography | Global */ :root { /* body font size */ --text-base-size: 1em; /* type scale */ --text-scale-ratio: 1.2; --text-xs: calc((1em / var(--text-scale-ratio)) / var(--text-scale-ratio)); --text-sm: calc(var(--text-xs) * var(--text-scale-ratio)); --text-md: calc(var(--text-sm) * var(--text-scale-ratio) * var(--text-scale-ratio)); --text-lg: calc(var(--text-md) * var(--text-scale-ratio)); --text-xl: calc(var(--text-lg) * var(--text-scale-ratio)); --text-xxl: calc(var(--text-xl) * var(--text-scale-ratio)); } @media (min-width: 64rem) { /* responsive decision applied to all text elements */ :root { --text-base-size: 1.25em; --text-scale-ratio: 1.25; } } h1, .text-xxl { font-size: var(--text-xxl, 2.074em); } h2, .text-xl { font-size: var(--text-xl, 1.728em); } h3, .text-lg { font-size: var(--text-lg, 1.44em); } h4, .text-md { font-size: var(--text-md, 1.2em); } .text-base { font-size: 1em; } small, .text-sm { font-size: var(--text-sm, 0.833em); } .text-xs { font-size: var(--text-xs, 0.694em); }
BEM in 30 seconds
BEM (Blocks, Elements, Modifiers) is a naming methodology aimed at creating reusable components.
Here’s an example:
<header> <a href="#0"><!-- ... --></a> <nav> <ul> <li><a href="#0">Homepage</a></li> <li><a href="#0">About</a></li> <li><a href="#0">Contact</a></li> </ul> </nav> </header>
- A block is a reusable component
- An element is a child of the block (e.g.,
.block__element
) - A modifier is a variation of a block/element (e.g.,
.block--modifier
, .block__element--modifier
).
Utility classes in 30 seconds
A utility class is a CSS class meant to do only one thing. For example:
<section> <h1>Title</h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p> </section> <style> .padding-sm { padding: 0.75em; } .padding-md { padding: 1.25em; } .padding-lg { padding: 2em; } </style>
You can potentially build entire components out of utility classes:
<article> <h1>Title</h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p> </article>
You can connect utility classes to CSS globals:
/* Spacing | Global */ :root { --space-unit: 1em; --space-xs: calc(0.5 * var(--space-unit)); --space-sm: calc(0.75 * var(--space-unit)); --space-md: calc(1.25 * var(--space-unit)); --space-lg: calc(2 * var(--space-unit)); --space-xl: calc(3.25 * var(--space-unit)); } /* responsive rule affecting all spacing variables */ @media (min-width: 64rem) { :root { --space-unit: 1.25em; /* this responsive decision affects all margins and paddings */ } } /* margin and padding util classes - apply spacing variables */ .margin-xs { margin: var(--space-xs); } .margin-sm { margin: var(--space-sm); } .margin-md { margin: var(--space-md); } .margin-lg { margin: var(--space-lg); } .margin-xl { margin: var(--space-xl); } .padding-xs { padding: var(--space-xs); } .padding-sm { padding: var(--space-sm); } .padding-md { padding: var(--space-md); } .padding-lg { padding: var(--space-lg); } .padding-xl { padding: var(--space-xl); }
A real-life example
Explaining a methodology using basic examples doesn’t bring up the real issues nor the advantages of the method itself.
Let’s build something together!
We’ll create a gallery of card elements. First, we’ll do it using only the BEM approach, and we’ll point out the issues you may face by going BEM only. Next, we’ll see how Globals reduce the size of your CSS. Finally, we’ll make the component customizable introducing utility classes to the mix.
Here’s a look at the final result:
Let’s start this experiment by creating the gallery using only BEM:
<div> <article> <a href="#0"> <figure> <img src="/image.jpg" alt="Image description"> </figure> <div> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, totam?</p> </div> <div aria-hidden="true"> <svg viewBox="0 0 24 24"><!-- icon --></svg> </div> </a> </article> <article><!-- card --></article> <article><!-- card --></article> <article><!-- card --></article> </div>
In this example, we have two components: .grid
and .card
. The first one is used to create the gallery layout. The second one is the card component.
First of all, let me point out the main advantages of using BEM: low specificity and scope .
/* without BEM */ .grid {} .card {} .card > a {} .card img {} .card-content {} .card .title {} .card .description {} /* with BEM */ .grid {} .card {} .card__link {} .card__img {} .card__content {} .card__title {} .card__description {}
If you don’t use BEM (or a similar naming method), you end up creating inheritance relationships ( .card > a
).
/* without BEM */ .card > a.active {} /* high specificity */ /* without BEM, when things go really bad */ div.container main .card.is-featured > a.active {} /* good luck with that */ /* with BEM */ .card__link--active {} /* low specificity */
Dealing with inheritance and specificity in big projects is painful. That feeling when your CSS doesn’t seem to be working, and you find out it’s been overwritten by another class ! BEM, on the other hand, creates some kind of scope for your components and keeps specificity low.
But… there are two main downsides of using only BEM:
- Naming too many things is frustrating
- Minor customizations are not easy to do or maintain
In our example, to stylize the components, we’ve created the following classes:
.grid {} .card {} .card__link {} .card__img {} .card__content {} .card__title-wrapper {} .card__title {} .card__description {} .card__icon-wrapper {} .card__icon {}
The number of classes is not the issue. The issue is coming up with so many meaningful names (and having all your teammates use the same naming criteria).
For example, imagine you have to modify the card component by including an additional, smaller paragraph:
<div> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor...</p> <p>Lorem ipsum dolor...</p> <!-- --> </div>
How do you call it? You could consider it a variation of the .card__description
element and go for .card__description .card__description--small
. Or, you could create a new element, something like .card__small, .card__small-p
, or .card__tag
. See where I’m going? No one wants to spend time thinking about class names. BEM is great as long as you don’t have to name too many things .
The second issue is dealing with minor customizations. For example, imagine you have to create a variation of the card component where the text is center-aligned.
You’ll probably do something like this:
<div> <!-- --> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, totam?</p> </div> <style> .card__content--center { text-align: center; } </style>
One of your teammates, working on another component ( .banner
), is facing the same problem. They create a variation for their component as well:
<div></div> <style> .banner--text-center { text-align: center; } </style>
Now imagine you have to include the banner component into a page. You need the variation where the text is aligned in the center. Without checking the CSS of the banner component, you may instinctively write something like banner banner--center
in your HTML, because you always use --center
when you create variations where the text is center-aligned. Not working! Your only option is to open the CSS file of the banner component, inspect the code, and find out what class should be applied to align the text in the center.
How long would it take, 5 minutes? Multiply 5 minutes by all the times this happens in a day, to you and all your teammates, and you realize how much time is wasted. Plus, adding new classes that do the same thing contributes to bloating your CSS.
CSS Globals and utility classes to the rescue
The first advantage of setting global styles is having a set of CSS rules that apply to all the components.
For example, if we set responsive rules in the spacing and typography globals, these rules will affect the grid and card components as well. In CodyFrame, we increase the body font size at a specific breakpoint; because we use “em” units for all margins and paddings, the whole spacing system is updated at once generating a cascade effect .
As a consequence, in most cases, you won’t need to use media queries to increase the font size or the values of margins and paddings!
/* without globals */ .card { padding: 1em; } @media (min-width: 48rem) { .card { padding: 2em; } .card__content { font-size: 1.25em; } } /* with globals (responsive rules intrinsically applied) */ .card { padding: var(--space-md); }
Not just that! You can use the globals to store behavioral components that can be combined with all other components. For example, in CodyFrame, we define a .text-component
class that is used as a “text wrapper.” It takes care of line height, vertical spacing, basic styling, and other things.
If we go back to our card example, the .card__content
element could be replaced with the following:
<!-- without globals --> <div> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, totam?</p> </div> <!-- with globals --> <div> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, totam?</p> </div>
The text component will take care of the text formatting, and make it consistent across all the text blocks in your project. Plus, we’ve already eliminated a couple of BEM classes.
Finally, let’s introduce the utility classes to the mix!
Utility classes are particularly useful if you want the ability to customize the component later on without having to check its CSS.
Here’s how the structure of the card component changes if we swap some BEM classes with utility classes:
<article> <a href="#0"> <figure> <img src="image.jpg" alt="Image description"> </figure> <div> <h1><span>Title of the card</span></h1> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, totam?</p> </div> <div aria-hidden="true"> <svg viewBox="0 0 24 24"><!-- icon --></svg> </div> </a> </article>
The number of BEM (component) classes has shrunk from 9 to 3:
.card {} .card__title {} .card__icon-wrapper {}
That means you won’t deal much with naming things. That said, we can’t avoid the naming issue entirely: even if you create Vue/React/SomeOtherFramework components out of utility classes, you still have to name the components.
All the other BEM classes have been replaced by utility classes. What if you have to make a card variation with a bigger title? Replace text-lg with text-xl. What if you want to change the icon color? Replace color-white with color-primary. How about aligning the text in the center? Add text-center to the text-component element. Less time thinking, more time doing!
Why don’t we just use utility classes?
Utility classes speed-up the design process and make it easier to customize things. So why don’t we forget about BEM and use only utility classes? Two main reasons:
By using BEM together with utility classes, the HTML is easier to read and customize.
Use BEM for:
- DRY-ing the HTML from the CSS you don’t plan on customizing (e.g., behavioral CSS-like transitions, positioning, hover/focus effects),
- advanced animations/effects.
Use utility classes for:
- the “frequently-customized” properties, often used to create component variations (like padding, margin, text-alignment, etc.),
- elements that are hard to identify with a new, meaningful class name (e.g., you need a parent element with a
position: relative
→ create<div><div></div></div>
).
Example:
<!-- use only Utility classes --> <article/cdn-cgi/l/email-protection" data-cfemail="43202c2f6e7503302e">[email protected]</a> <a href="/cdn-cgi/l/email-protection" data-cfemail="cfaca0a3e2fb8fa2ab">[email protected]</a>"> <!-- card content --> </article> <!-- use BEM + Utility classes --> <article/cdn-cgi/l/email-protection" data-cfemail="1675797a3b2056657b">[email protected]</a> <a href="/cdn-cgi/l/email-protection" data-cfemail="91f2fefdbca5d1fcf5">[email protected]</a>"> <!-- card content --> </article>
For these reasons, we suggest that you don’t add the !important rule to your utility classes. Using utility classes doesn’t need to be like using a hammer. Do you think it would be beneficial to access and modify a CSS property in the HTML? Use a utility class. Do you need a bunch of rules that won’t need editing? Write them in your CSS. This process doesn’t need to be perfect the first time you do it: you can tweak the component later on if required. It may sound laborious “having to decide” but it’s quite straightforward when you put it to practice.
Utility classes are not your best ally when it comes to creating unique effects/animations.
Think about working with pseudo-elements, or crafting unique motion effects that require custom bezier curves. For those, you still need to open your CSS file.
Consider, for example, the animated background effect of the card we’ve designed. How hard would it be to create such an effect using utility classes?
The same goes for the icon animation, which requires animation keyframes to work:
.card:hover .card__title { background-size: 100% 100%; } .card:hover .card__icon-wrapper .icon { animation: card-icon-animation .3s; } .card__title { background-image: linear-gradient(transparent 50%, alpha(var(--color-primary), 0.2) 50%); background-repeat: no-repeat; background-position: left center; background-size: 0% 100%; transition: background .3s; } .card__icon-wrapper { position: absolute; top: 0; right: 0; width: 3em; height: 3em; background-color: alpha(var(--color-black), 0.85); border-bottom-left-radius: var(--radius-lg); display: flex; justify-content: center; align-items: center; } @keyframes card-icon-animation { 0%, 100% { opacity: 1; transform: translateX(0%); } 50% { opacity: 0; transform: translateX(100%); } 51% { opacity: 0; transform: translateX(-100%); } }
Final result
Here’s the final version of the cards gallery. It also includes grid utility classes to customize the layout.
File structure
Here’s how the structure of a project built using the method described in this article would look like:
project/ └── main/ ├── assets/ │ ├── css/ │ │ ├── components/ │ │ │ ├── _card.scss │ │ │ ├── _footer.scss │ │ │ └── _header.scss │ │ ├── globals/ │ │ │ ├── _accessibility.scss │ │ │ ├── _breakpoints.scss │ │ │ ├── _buttons.scss │ │ │ ├── _colors.scss │ │ │ ├── _forms.scss │ │ │ ├── _grid-layout.scss │ │ │ ├── _icons.scss │ │ │ ├── _reset.scss │ │ │ ├── _spacing.scss │ │ │ ├── _typography.scss │ │ │ ├── _util.scss │ │ │ ├── _visibility.scss │ │ │ └── _z-index.scss │ │ ├── _globals.scss │ │ ├── style.css │ │ └── style.scss │ └── js/ │ ├── components/ │ │ └── _header.js │ └── util.js └── index.html
You can store the CSS (or SCSS) of each component into a separate file (and, optionally, use PostCSS plugins to compile each new /component/componentName.css
file into style.css
). Feel free to organize the globals as you prefer; you could also create a single globals.css
file and avoid separating the globals in different files.
Conclusion
Working on large-scale projects requires a solid architecture if you want to open your files months later and don’t get lost. There are many methods out there that tackle this issue (CSS-in-JS, utility-first, atomic design, etc.).
The method I’ve shared with you today relies on creating crosswise rules (globals), using utility classes for rapid development, and BEM for modular (behavioral) classes.
You can learn in more detail about this method on CodyHouse . Any feedback is welcome!
以上所述就是小编给大家介绍的《Building a Scalable CSS Architecture With BEM and Utility Classes》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
恰如其分的软件架构
George Fairbanks / 张逸、倪健、高翌翔 / 华中科技大学出版社 / 2013-9-1 / 88.00
本书描述了一种恰如其分的软件架构设计方法。作者建议根据项目面临的风险来调整架构设计的成本,并从多个视角阐述了软件架构的建模过程和方法,包括用例模型、概念模型、域模型、设计模型和代码模型等。本书不仅介绍方法,而且还对方法和概念进行了归类和阐述,将软件架构设计融入开发实践中,与 敏捷开发方法有机地结合在一起,适合普通程序员阅读。 . 这是一本超值的书,案例丰富有趣,言简意赅,阅读轻松。当年......一起来看看 《恰如其分的软件架构》 这本书的介绍吧!