You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: css_architecture.md
+40-10
Original file line number
Diff line number
Diff line change
@@ -57,6 +57,7 @@ For example, don't:
57
57
```
58
58
59
59
Instead, create classes that extend:
60
+
60
61
```css
61
62
.widget {
62
63
/* base rules */
@@ -91,6 +92,7 @@ In general, you should try to avoid tag selectors. Apply classes directly to the
91
92
A *qualified selector* is one that includes the HTML element, for example, `div.error` is a *qualified selector*. In most cases it reduces reusability.
92
93
93
94
For example, instead of this:
95
+
94
96
```css
95
97
.error {
96
98
color: red;
@@ -115,12 +117,14 @@ Do this:
115
117
In the event that you want to use a qualified selector to indicate the HTML element that the class is intended for, use the following convention.
116
118
117
119
Instead of:
120
+
118
121
```css
119
122
ul.nav {}
120
123
```
121
124
122
125
Do this:
123
-
```css
126
+
127
+
```css
124
128
/*ul*/.nav {}
125
129
```
126
130
@@ -135,6 +139,7 @@ In this example `.title` is too common a name. It may end up being used somewher
135
139
```
136
140
137
141
Instead, do this:
142
+
138
143
```css
139
144
.widget {}
140
145
.widget__title {}
@@ -171,6 +176,7 @@ The *Single Responsibility Principle* is the idea that our CSS should be compose
171
176
```
172
177
173
178
We can break these two classes out into four smaller, reusable responsibilities:
179
+
174
180
```css
175
181
.box {
176
182
display: block;
@@ -226,6 +232,7 @@ You would then write new, separate code to handle the content inside of that lay
226
232
227
233
</div>
228
234
```
235
+
229
236
Code which adheres to the *separation of concerns* can be more confidently modified, edited, extended, and maintained because we know how far its responsibilities reach.
230
237
231
238
@@ -337,6 +344,7 @@ These suggestions use [BEM](http://getbem.com/naming/)-like conventions.
337
344
```
338
345
339
346
For example:
347
+
340
348
```css
341
349
.nav {
342
350
/* rules */
@@ -396,6 +404,7 @@ In the event that you absolutely need to override something with `!important;`,
396
404
There are a number of different approaches for categorizing your CSS.
@@ -432,11 +442,10 @@ Ultimately, you have to find a pattern that works for you and your team. The str
432
442
433
443
```css
434
444
/* VARIABLES */
435
-
/* DEFAULTS */
436
-
/* TYPOGRAPHY */
437
-
/* LINKS & BUTTONS */
445
+
/* BASE */
438
446
/* LAYOUT */
439
-
/* COMPONENTS */
447
+
/* COMPONENTS */
448
+
/* LINKS & BUTTONS */
440
449
/* COSMETIC */
441
450
/* UTILITY */
442
451
/* STATE */
@@ -486,11 +495,11 @@ Think about *dependency direction*. There are two ways you can write HTML/CSS:
486
495
487
496
### 1. CSS that depends on HTML
488
497
489
-
This is when you name your classes based on your HTML, for example `author-bio`, or `footer-links`. In this model, your HTML is re-stylable (think [CSS Zen Garden](http://www.csszengarden.com/)) but your **CSS is not reusable**.
498
+
This is when you name your classes based on your HTML, for example `author-bio`, or `footer-links`. In this model, your HTML is restyleable (think [CSS Zen Garden](http://www.csszengarden.com/)) but your **CSS is not reusable**.
490
499
491
500
### 2. HTML that depends on CSS
492
501
493
-
This is when you name your classes in acontent-agnostic way after repeating patterns in your UI, for example `media-card` or `sub-links`. In this model your CSS is reusable but your HTML is not as re-stylable.
502
+
This is when you name your classes in acontent-agnostic way after repeating patterns in your UI, for example `media-card` or `sub-links`. In this model your CSS is more reusable but your HTML is not as restyleable (as components marked with the same class are fixed to look the same).
494
503
495
504
Choosing reusability will result in more generic class names like:
496
505
```css
@@ -505,23 +514,44 @@ The idea of breaking things up for reusability like this is a big part of [OOCSS
505
514
506
515
### Summary:
507
516
517
+
In the end neither approach is right or wrong. The question to ask is... for the project you're working on, what would be more valuable: restyleable HTML or reusable CSS?
518
+
519
+
Consider this:
520
+
508
521
- The primary purpose of a class name is to provide a hook for CSS or JavaScript.
509
522
- Class names impart little or no semantic information to machines or human readers.
510
523
- Semantics are already served by the HTML markup.
511
524
- Class names should communicate useful information to developers. For example:
525
+
512
526
```html
513
527
<div class="news">
514
528
<h2>News </h2>
515
529
<p>... </p>
516
530
</div>
517
531
```
532
+
518
533
Giving the class name `news` provides no useful information and ensures the style can't be used for anything other than news.
519
534
520
535
In short, pick a name that is sensible but somewhat ambiguous. **Aim for high reusability**.
521
536
537
+
Note however that the more a component does, or the more specific a component is, the harder it is to reuse. This is where we may want to *also*use some [Utility-first CSS](https://adamwathan.me/css-utility-classes-and-separation-of-concerns/) to help reuse components. For example, utility classes for common visual tweaks like:
538
+
539
+
- Text sizes, colors, and weights
540
+
- Border colors, widths, and positions
541
+
- Background colors
542
+
- Flexbox utilities
543
+
- Padding and margin helpers
544
+
545
+
To take thinks further (as Tailwind's creator explains in the link above), these utility classes can also be used to remove component classes that will never actually get reused (e.g. main navigation bar), thus removing of a lot of bloat and complexity in stylesheets.
546
+
547
+
Keep in mind though, the cost od using too much utility-first CSS, is your code can become harder to read and maintain in the long-term. There’s also a deeper problem: because you are essentially recreating the CSS API with utility classes, you can end up with a huge file.
548
+
522
549
------
523
550
524
-
## Sources:
525
-
[cssguidelin.es](https://cssguidelin.es/)
526
-
[About HTML semantics and front-end architecture](http://nicolasgallagher.com/about-html-semantics-front-end-architecture/)
551
+
## Sources:
552
+
553
+
[About HTML semantics and front-end architecture](http://nicolasgallagher.com/about-html-semantics-front-end-architecture/)
554
+
[cssguidelin.es](https://cssguidelin.es/)
527
555
[codeguide.co](http://codeguide.co/)
556
+
[CSS Utility Classes and "Separation of Concerns"](https://adamwathan.me/css-utility-classes-and-separation-of-concerns/)
0 commit comments