|
140 | 140 | } |
141 | 141 | ``` |
142 | 142 |
|
143 | | -### Breakpoints (Media Queries) |
144 | | -```html |
145 | | -<article class="listing-card listing-card--featured"> |
146 | | - |
147 | | - <h1 class="listing-card__title">Adorable 2BR in the sunny Mission</h1> |
148 | | - |
149 | | - <div class="listing-card__content"> |
150 | | - <p>Vestibulum id ligula porta felis euismod semper.</p> |
151 | | - </div> |
152 | | - |
153 | | -</article> |
154 | | -``` |
| 143 | +### Nesting |
| 144 | +* Do not nest more than 3 levels deep. This way css doesn't become too specific making it easier to use on other parts of the site. |
155 | 145 |
|
156 | 146 | ```css |
157 | | -.listing-card { } |
158 | | -.listing-card--featured { } |
159 | | -.listing-card__title { } |
160 | | -.listing-card__content { } |
161 | | -``` |
162 | | - |
163 | | - * `.listing-card` is the “block” and represents the higher-level component |
164 | | - * `.listing-card__title` is an “element” and represents a descendant of `.listing-card` that helps compose the block as a whole. |
165 | | - * `.listing-card--featured` is a “modifier” and represents a different state or variation on the `.listing-card` block. |
166 | | - |
167 | | -### ID selectors |
168 | | - |
169 | | -While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) to your rule declarations, and they are not reusable. |
170 | | - |
171 | | -For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity. |
172 | | - |
173 | | -### JavaScript hooks |
174 | | - |
175 | | -Avoid binding to the same class in both your CSS and JavaScript. Conflating the two often leads to, at a minimum, time wasted during refactoring when a developer must cross-reference each class they are changing, and at its worst, developers being afraid to make changes for fear of breaking functionality. |
176 | | - |
177 | | -We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`: |
178 | | - |
179 | | -```html |
180 | | -<button class="btn btn-primary js-request-to-book">Request to Book</button> |
181 | | -``` |
182 | | - |
183 | | -## Sass |
184 | | - |
185 | | -### Syntax |
186 | | - |
187 | | -* Use the `.scss` syntax, never the original `.sass` syntax |
188 | | -* Order your `@extend`, regular CSS and `@include` declarations logically (see below) |
189 | | - |
190 | | -### Ordering of property declarations |
191 | | - |
192 | | -1. `@extend` declarations |
193 | | - |
194 | | - Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another. |
195 | | - |
196 | | - ```scss |
197 | | - .btn-green { |
198 | | - @extend %btn; |
199 | | - // ... |
200 | | - } |
201 | | - ``` |
202 | | - |
203 | | -2. Property declarations |
204 | | - |
205 | | - Now list all standard property declarations, anything that isn't an `@extend`, `@include`, or a nested selector. |
206 | | - |
207 | | - ```scss |
208 | | - .btn-green { |
209 | | - @extend %btn; |
210 | | - background: green; |
211 | | - font-weight: bold; |
212 | | - // ... |
213 | | - } |
214 | | - ``` |
215 | | - |
216 | | -3. `@include` declarations |
217 | | - |
218 | | - Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s. |
219 | | - |
220 | | - ```scss |
221 | | - .btn-green { |
222 | | - @extend %btn; |
223 | | - background: green; |
224 | | - font-weight: bold; |
225 | | - @include transition(background 0.5s ease); |
226 | | - // ... |
227 | | - } |
228 | | - ``` |
229 | | - |
230 | | -4. Nested selectors |
231 | | - |
232 | | - Nested selectors, _if necessary_, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors. |
233 | | - |
234 | | - ```scss |
235 | | - .btn { |
236 | | - @extend %btn; |
237 | | - background: green; |
238 | | - font-weight: bold; |
239 | | - @include transition(background 0.5s ease); |
240 | | - |
241 | | - .icon { |
242 | | - margin-right: 10px; |
243 | | - } |
244 | | - } |
245 | | - ``` |
246 | | - |
247 | | -### Mixins |
248 | | - |
249 | | -Mixins, defined via `@mixin` and called with `@include`, should be used sparingly and only when function arguments are necessary. A mixin without function arguments (i.e. `@mixin hide { display: none; }`) is better accomplished using a placeholder selector (see below) in order to prevent code duplication. |
250 | | - |
251 | | -### Placeholders |
252 | | - |
253 | | -Placeholders in Sass, defined via `%selector` and used with `@extend`, are a way of defining rule declarations that aren't automatically output in your compiled stylesheet. Instead, other selectors “inherit” from the placeholder, and the relevant selectors are copied to the point in the stylesheet where the placeholder is defined. This is best illustrated with the example below. |
254 | | -
|
255 | | -Placeholders are powerful but easy to abuse, especially when combined with nested selectors. **As a rule of thumb, avoid creating placeholders with nested rule declarations, or calling `@extend` inside nested selectors.** Placeholders are great for simple inheritance, but can easily result in the accidental creation of additional selectors without paying close attention to how and where they are used. |
256 | | -
|
257 | | -**Sass** |
258 | | -
|
259 | | -```sass |
260 | | -// Unless we call `@extend %icon` these properties won't be compiled! |
261 | | -%icon { |
262 | | - font-family: "Airglyphs"; |
263 | | -} |
264 | | - |
265 | | -.icon-error { |
266 | | - @extend %icon; |
267 | | - color: red; |
268 | | -} |
269 | | - |
270 | | -.icon-success { |
271 | | - @extend %icon; |
272 | | - color: green; |
273 | | -} |
274 | | -``` |
275 | | - |
276 | | -**CSS** |
277 | | - |
278 | | -```css |
279 | | -.icon-error, |
280 | | -.icon-success { |
281 | | - font-family: "Airglyphs"; |
282 | | -} |
283 | | - |
284 | | -.icon-error { |
285 | | - color: red; |
286 | | -} |
287 | | - |
288 | | -.icon-success { |
289 | | - color: green; |
290 | | -} |
291 | | -``` |
292 | | - |
293 | | -### Nested selectors |
294 | | - |
295 | | -**Do not nest selectors more than three levels deep!** |
296 | | - |
297 | | -```scss |
298 | | -.page-container { |
299 | | - .content { |
300 | | - .profile { |
| 147 | +.m-page-container { |
| 148 | + .o-content { |
| 149 | + .c-profile { |
301 | 150 | // STOP! |
302 | 151 | } |
303 | 152 | } |
304 | 153 | } |
305 | 154 | ``` |
306 | 155 |
|
307 | | -When selectors become this long, you're likely writing CSS that is: |
308 | | - |
309 | | -* Strongly coupled to the HTML (fragile) *—OR—* |
310 | | -* Overly specific (powerful) *—OR—* |
311 | | -* Not reusable |
312 | | - |
313 | | - |
314 | | -Again: **never nest ID selectors!** |
315 | | - |
316 | | -If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you are writing well formed HTML and CSS, you should **never** need to do this. |
0 commit comments