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
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: css/class-naming-conventions/readme.md
+17-20Lines changed: 17 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,17 +43,17 @@ Our CSS class naming convention (which we call CSM or Component, Sub-Component,
43
43
</div>
44
44
```
45
45
46
-
This example may seem confusing at first but if we break down each of the selectors that we have, it begins to make more sense.
46
+
This example may seem confusing at first, but if we break down each of the selectors that we have, it begins to make more sense.
47
47
48
48
`.c-blog` This is a high-level component. In this example, it describes a wrapper that may contain blog content.
49
49
50
-
`.c-blog__title` This is a sub-component. In this example, it is a title for a blog.
50
+
`.c-blog__title` This is a sub-component. In this example, it's a title for a blog.
51
51
52
-
`.c-blog-post` This is another high-level component. In this example, it describes the blog post itself. Notice that it is its own component instead of a sub-component of `c-blog` because a blog post does not need to be child of the blog container. This way the component is able to live anywhere.
52
+
`.c-blog-post` This is another high-level component. In this example, it describes the blog post itself. Notice that it can be its own component instead of a sub-component of `c-blog` because a blog post does not need to be child of the blog container. This way, the component is able to live anywhere.
53
53
54
54
`c-blog-post.c--featured` This is a modifier. Notice that the `c--featured` class is paired with the component or sub-component it belongs to. In this example, it describes a different way of displaying the `c-blog-post` component.
55
55
56
-
`.c-blog-post__time` Like before, this is another sub-component this time belonging to the `c-blog-post` component. It is still a sub-component even though it is not a **direct** child of the component. In other words, sub-components are not required to be direct children of its parent component.
56
+
`.c-blog-post__time` Like before, this is another sub-component this time belonging to the `c-blog-post` component. It's still a sub-component, even though it is not a **direct** child of the component. In other words, sub-components are not required to be direct children of their parent component.
57
57
58
58
### Components
59
59
@@ -70,7 +70,7 @@ Components are independent and self-contained units of UI. Styles belonging to a
70
70
71
71
### Sub-components
72
72
73
-
Sub-components are elements that are child to its parent component. The classname should be formatted as such: `c-[parent-component-name]__[sub-component-name]`. Sub-components do not, and should not, have sub-components of their own. If you find you need to write a sub-sub-component, instead just treat it as a sub-component – sub-components are only ever child to the parent component.
73
+
Sub-components are elements that are descendants of their parent component. Their classnames should be formatted as such: `c-[parent-component-name]__[sub-component-name]`. Sub-components do not, and should not, have sub-components of their own. If you find you need to write a sub-sub-component, instead just treat it as a sub-component.
74
74
75
75
Like components, these should always live at the root level of a file. Avoid nesting these within the parent component or another sub-component.
76
76
@@ -103,7 +103,7 @@ Like components, these should always live at the root level of a file. Avoid nes
103
103
104
104
### Modifiers
105
105
106
-
Modifiers, as their name suggests, modify components or sub-components. They are always chained to the component or sub-component they belong to.
106
+
Modifiers, as their name suggests, modify components or sub-components. They're always chained to the component or sub-component they belong to.
107
107
108
108
* Prefixed with the namespace of the affected element and two dashes (`c--`, `t--`)
109
109
* Contained to the scope of a single component
@@ -292,7 +292,7 @@ Prefix | Purpose | Location |
292
292
`.m-` (*) | Desktop embedded mobile markup classes: these are classes that we will use if we author Markup that is intended for clients to embed onto their desktop pages, but is for mobile content. | *n/a* |
293
293
`.js-` | Javascript classes are used exclusively by scripts and should never have CSS styles applied to them. Repeat: **Do NOT** style Javascript classes. | *n/a*
294
294
295
-
> \* The `m-` class prefix has an old, deprecated use: Mobify Modules. However, Mobify Modules have been replaced with third part plugins, and are treated as third party libraries with their own conventions.
295
+
> \* The `m-` class prefix has an old, deprecated use: Mobify Modules. However, Mobify Modules have been replaced with third party plugins, and are treated as third party libraries with their own conventions.
296
296
297
297
298
298
## Components That Style Components
@@ -308,9 +308,9 @@ Sometimes there are situations when a component makes use of other components, a
308
308
</button>
309
309
```
310
310
311
-
In situations like this it is tempting to just style the icon's class inside of the button. However, this practice is poor and creates tight coupling between the Button and Icon components that shouldn't exist. As a rule of thumb, a component should only know about what it's responsible for; it should be unaware of anything external to itself. Continuing with this example, the Icon is an external component, therefore the Button component should be completely unaware there there is even such thing as icon classes, like`c-icon`.
311
+
In situations like this it is tempting to just style the icon's class inside of the button. However, this practice is poor and creates tight coupling between the Button and Icon components that shouldn't exist. As a rule of thumb, a component should only know about what it's responsible for; it shouldn't be aware of anything external to itself. Since Icon is an external component, the Button component should be completely unaware of`c-icon`.
312
312
313
-
The solution to this challenge is to instead give the external component a new class that our new component can know about, like `c-button__icon`. By doing it in this way the external Icon component is, for all intents and purposes, being treated as a sub-component of the Button component. This method also has the benefit of being free of any (tight) coupling between the components. Both components can change, be added or removed, without really effecting the other in an unpredictable way.
313
+
The solution to this challenge is to instead give the external component a new class that our new component can know about, like `c-button__icon`. This way, the component istreated like a sub-component of Button, and eliminates any tight coupling between the components. Both components can change, be added or removed, without affecting the other in an unpredictable way.
314
314
315
315
So, to summarize...
316
316
@@ -349,25 +349,22 @@ So, to summarize...
349
349
350
350
## Parsing vs. Decorating
351
351
352
-
It's important to understand that we have a few different ways of authoring our CSS, and the way we do this is depends a lot on how we convert the desktop markup for mobile. Ideally, we parse the desktop markup and take full control of the final HTML. However, this isn't always possible, and sometimes we just output the desktop markup as is: untouched, or perhaps partially wrapped in order to control the appearance entirely through CSS.
352
+
Ideally, we have total control over the HTML markup of a project. However, sometimes there are engineering requirements that force us to retain client markup, or partially wrap it in a container of our own.
353
353
354
-
If you find yourself wondering "should I be adding a new class, or should I use the classes from desktop?" consider the following: If we're using their class names, we obviously can't follow our CSM syntax. But that said, sometimes we just have no choice; perhaps there are engineering requirements that force us to retain the markup structure. Under such circumstances, we must stick to the desktop classes.
355
-
356
-
Below is laid out some situational advice that should clarify when to use desktop classes and when to author our own. We also talk about ways to adjust the code style when using their class names.
354
+
If you find yourself wondering "should I be adding a new class, or should I use the classes from desktop?" consider the following: If we're using their class names, we can't follow our CSM syntax. Here's some advice:
357
355
358
356
### When to use our class naming convention
359
357
360
358
* When writing your own markup in a template
361
-
* When decorating (adding, moving, or wrapping) markup in a View, Parser, Decorator or UI-Script
359
+
* When decorating (adding, moving, or wrapping) markup in a View, Parser, Decorator or UI-Script (Adaptive.js)
362
360
* When adding custom classes to existing markup
363
361
* When you find yourself using @extend
364
362
365
-
### When to use their existing selectors
363
+
### When to use client selectors
366
364
367
365
* When it's fastest, easiest or most efficient to use their markup than it is to add our own.
368
366
* When their markup is too inconsistent, or makes parsing too difficult.
369
-
* When mobile functionality is tightly coupled to desktop's markup structure.
370
-
* When intercepting AJAXed content or content added after a page is too costly, unperformant, or inefficent.
367
+
* When functionality is tightly coupled to markup structure.
371
368
372
369
### How to use their existing selectors in our components
373
370
@@ -394,7 +391,7 @@ Always component classes should always be structured with our naming conventions
394
391
395
392
Desktop classes can be added inside their parent component, but adding our own classes should be your FIRST approach so as to avoid nesting.
396
393
397
-
Constantly evaluate your nesting in situation like this.
394
+
Constantly evaluate your nesting in situations like this.
398
395
399
396
```scss
400
397
// Okay
@@ -427,7 +424,7 @@ Constantly evaluate your nesting in situation like this.
427
424
}
428
425
```
429
426
430
-
Use their modifiers the same way you would use our modifiers. Chain it to the component or sub-component it directly affects.
427
+
Use their modifiers the same way you would use our modifiers. Chain them to the component or sub-component it directly affects.
431
428
432
429
```scss
433
430
// Okay
@@ -456,4 +453,4 @@ Use their modifiers the same way you would use our modifiers. Chain it to the co
456
453
}
457
454
```
458
455
459
-
Continue on to [Block Comment Documentation Guide →](../localization-and-theming-best-practices/readme.md)
456
+
Continue on to [Responsive Best Practices →](../responsive-best-practices/readme.md)
Because we use Autoprefixer, we are able to write our code as if all our properties are fully supported. When compiled, Autoprefixer will of course convert any properties that have special requirements as far as compatibility is concerned and add prefixes to them as needed.
25
-
26
-
That includes things like Flexbox, CSS3 properties, and many more! This also means that we *should not* be using mixins for prefixing. Remember that Autoprefixer uses data from caniuse.com to determine what will be output — so properties that don't have enough browser support should probably still be avoided (or at least used with extreme caution).
25
+
Because we use post-processors, we are able to write our code as if all our properties are fully supported–includeing things like Flexbox, CSS3 properties, and many more! This also means that we *should not* be using mixins for prefixing.
27
26
28
27
29
28
## Selector Specificity
@@ -32,32 +31,30 @@ We strive to write performant, portable, selectors whenever possible. In short,
32
31
33
32
To help do that, it might be helpful to know how to measure specificity which [Smashing Magazine has an article just for that](http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/)!
34
33
35
-
### Dos:
34
+
### Some Notes:
36
35
37
36
* Use class names for specificity because it improves performance.
38
-
* Use the component oriented naming conventions outlined below.
39
-
* If you have no other choice and you must select an ID, use the attribute selector instead
40
-
41
-
### Do Nots:
42
-
43
37
* Avoid using IDs. They decrease portability.
38
+
* Use the component oriented naming conventions outlined below.
44
39
* Avoid using tag selectors. They both impact performance and portability.
45
40
* Never over qualify selectors because it impacts performance.
41
+
* If you have no other choice and you must select an ID, use the attribute selector instead
42
+
46
43
47
44
```scss
48
-
//NO
45
+
//Instead of:
49
46
ul.button-groupli.button {
50
47
}
51
48
52
-
//Yes
49
+
//Try:
53
50
.button-group__button {
54
51
}
55
52
56
-
//NO
53
+
//Instead of:
57
54
#something {
58
55
}
59
56
60
-
//Yes (Only as a last resort!)
57
+
//Try (Only as a last resort!):
61
58
[id="something"] {
62
59
}
63
60
```
@@ -67,7 +64,7 @@ ul.button-group li.button {
67
64
68
65
A common use case is to target input types. For example `input[type="text"]`.
69
66
70
-
It's important to realize that the element selector is not necessary here. If you think about it, we are actually increasing the specificity needlessly. Attribute selectors have the same specificity as a class. The above example has the same specificity as `input.someClass`
67
+
It's important to realize that the element selector (`input`, in this case) is not necessary here. Using `input` increases the specificity needlessly. Attribute selectors have the same specificity as a class. The above example has the same specificity as `input.someClass`
71
68
72
69
All we really need is `[type="text"]`, because that is sufficient for targeting text inputs.
73
70
@@ -76,7 +73,7 @@ In summary: *attribute selectors should be used alone*, just like classes.
76
73
77
74
## Class Naming Convention
78
75
79
-
We use a modified version of BEM that we call CSM (Component, Sub-component, Modifier). At it's core, it's virtually identical to BEM, but our exact syntax is slightly different. Basically:
76
+
We use a modified version of BEM that we call CSM (Component, Sub-component, Modifier). At its core, it's virtually identical to BEM, but our syntax is slightly different. Basically:
80
77
81
78
```
82
79
.c-component
@@ -93,14 +90,18 @@ When authoring CSS, you should be always aware of the selectors that you are cre
93
90
94
91
Strive to create selectors that actually fully describe where it is authored. Put another way, any given selector should tell you which file and where in the file it is written.
95
92
96
-
This can be down by following this simple rule: the first class in a selector is the file it can be found
93
+
This can be done by following this simple rule: the first class in a selector is the file it can be found.
97
94
98
95
For example `.t-pdp .c-product` would be written in `_pdp.scss` and NOT `_product.scss`.
99
96
100
-
The exception to this rule are when a base or root class is dependant on a global state.
97
+
The exception to this rule is when a base or root class is dependent on a global state:
98
+
99
+
`.x-landscape .t-about .c-contact-form` would be found in `_about.scss` and NOT a `_landscape.scss` file (modifier classes alone don't have their own files). Using `x-` prefixes is an old convention and won't be found very often.
101
100
102
-
For example `.x-landscape .t-about .c-contact-form` would be found in `_about.scss` and NOT a `_landscape.scss` file (modifier classes alone don't have their own files).
103
101
102
+
## Mobile First
103
+
104
+
When styling responsively, we use min-width queries and build on top of them when we need to. Learn more about our other responsive best practices [here](responsive-best-practices/readme.md).
104
105
105
106
## Single Direction Rule
106
107
@@ -115,9 +116,7 @@ This principle is talked about in depth by [Harry Roberts on csswizardy.com](htt
115
116
116
117
## Name Spacing
117
118
118
-
The first thing you'll notice when going through Customer Success's CSS is that all of our class names are prefixed (aka: name-spaced) to one of two letters: `c-` or `t-`, meaning `_component_` or `_template_` respectively. See the [below table](../class-naming-conventions#class-prefix-conventions) for more details on Mobify's namespacing practices.
119
-
120
-
Because we work on top of our client's markup and javascript, we need to make sure our class names will never conflict with their class names. By prefixing/namespacing our classes with `c-` or `t-`, we can be assured that 99.9% of those situations are avoided.
119
+
The first thing you'll notice when going through our CSS is that all of our class names are prefixed (aka: name-spaced) to one of two letters: `c-` or `t-`, meaning `_component_` or `_template_` respectively. See the [below table](../class-naming-conventions#class-prefix-conventions) for more details on Mobify's namespacing practices.
121
120
122
121
123
122
## Size Units
@@ -230,6 +229,7 @@ And as before, we use [Sass-Lint](https://github.com/sasstools/sass-lint) to hel
230
229
1. Pseudo-elements
231
230
1. Modifier elements
232
231
1. Child elements
232
+
1. Media Queries
233
233
234
234
```scss
235
235
.c-selector {
@@ -309,7 +309,7 @@ And as before, we use [Sass-Lint](https://github.com/sasstools/sass-lint) to hel
309
309
Sometimes we break out of this convention to add to the readability of our stylesheets. This occurs especially often with long comma separated property values like gradients, shadows, transitions, or includes. These can be arrange across multiple lines and indentation levels to help with diffs and readability.
Copy file name to clipboardExpand all lines: css/csscomb/readme.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,3 +40,4 @@ Before you start, make sure you have [Package Control](https://packagecontrol.io
40
40
*[CSSComb for VIM](https://github.com/csscomb/vim-csscomb)
41
41
*[jetbrains-csscomb](https://github.com/csscomb/jetbrains-csscomb) which should work for all JetBrains IDEs, such as IDEA, PyCharm, WebStorm, PhpStorm, RubyMine, and perhaps others.
42
42
*[CSSComb for Emacs](https://github.com/channikhabra/css-comb.el), including Aquamacs
43
+
*[CSSComb for VS Code](https://marketplace.visualstudio.com/items?itemName=mrmlnc.vscode-csscomb)
0 commit comments