perfect styling - how to write better css

25
Perfect Styling How to write better CSS ARTEM TABALIN

Upload: artem-tabalin

Post on 17-Feb-2017

4.121 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Perfect Styling - How to write better CSS

Perfect Styling!How to write better CSS!

ARTEM TABALIN!

Page 2: Perfect Styling - How to write better CSS

Styles Should Be!

Reusable!

Modular!

Consistent!

Compact!

Page 3: Perfect Styling - How to write better CSS

Common Styling Issues!Scoped with page id !

Mixed layout and component styling!

Coupled with markup!

Deeply nested!

Page 4: Perfect Styling - How to write better CSS

Styling Methodologies !

•  OOCSS!Object-Oriented CSS!

•  ACSS!Atomic CSS!

•  BEM!Block Element Modifier!

•  SMACSS!Scalable and Modular Architecture for CSS!

Page 5: Perfect Styling - How to write better CSS

Object-Oriented CSS!

•  Separate structure and skin!!!!

!

•  Separate container and content!

Page 6: Perfect Styling - How to write better CSS

Atomic CSS!!

•  Each class represents a single styling unit!

Page 7: Perfect Styling - How to write better CSS

BEM!

Block!.menu

Element!.menu_ _item

Modifier!.menu_ _item--active .menu--aside

Page 8: Perfect Styling - How to write better CSS

SMACSS!•  Base!

html, body, a, a:hover!

•  Layout!#header, #footer, .l-fixed #header, .l-fixed #footer!

•  Module!.form, .form-field, .form-field-label, .form-field-control!

•  State!.is-collapsed, .is-active, .is-hidden!

•  Theme!.theme-border, .theme-background!

Page 9: Perfect Styling - How to write better CSS

Guidelines!Componentize. Naming rules. !Don’ts rules. Keep it rules. !

Page 10: Perfect Styling - How to write better CSS

Componentize!

Separate content from container!

.search-box

.menu

.article

Page 11: Perfect Styling - How to write better CSS

Naming Rules!Components. Modifiers. States. JS-related classes.!

Page 12: Perfect Styling - How to write better CSS

Components!

Follow the pattern .component-subcmp-subcmp!

Page 13: Perfect Styling - How to write better CSS

Modifiers!

Prefix modifier classes with .mod-!

!

Page 14: Perfect Styling - How to write better CSS

States!

Prefix state classes with .is-!

!

Page 15: Perfect Styling - How to write better CSS

JS-related Classes!

Prefix behavior classes with .js-!

•  No styles for behavior classes!

•  No presentation classes in JS!

Page 16: Perfect Styling - How to write better CSS

Don’ts Rules!Avoid deep nesting. Avoid IDs. !Avoid type selectors. Avoid !important.!

Page 17: Perfect Styling - How to write better CSS

Avoid Deep Nesting!

Avoid unnecessary rules nesting!

•  Harder to reuse

•  More verbose

!

Hard to override!

Page 18: Perfect Styling - How to write better CSS

Avoid IDs!

Avoid using ID selector!

•  Harder to reuse

•  Can use only one on the page

!Single #navbar on the page

Have to use ID to override

Page 19: Perfect Styling - How to write better CSS

Avoid Type Selectors!

Avoid using HTML type selector!

•  Couples CSS and HTML

•  Redundant overqualified selectors

!What if decided to use <li>

Lowers performance and compactness

Page 20: Perfect Styling - How to write better CSS

Avoid using !important!

•  Harder to override

•  Accessibility software issues

!

Avoid !important!

Requires style with !important to override

Can be used for utility classes

Page 21: Perfect Styling - How to write better CSS

Keep It Rules!Keep it compact. Keep it ordered.!!

Page 22: Perfect Styling - How to write better CSS

Don’t put redundant rules or chars!

•  Framework over custom •  Use shorthands

•  Don’t specify units for 0

•  Don’t put leading zeros

!

Keep It Compact !

Page 23: Perfect Styling - How to write better CSS

Put rules in the particular order!

1.  mixins!.mixin(param)!

2.  positioning!position, z-index, top, right, bottom, left!

3.  box-model!display, overflow, width, height, padding, border, margin!

4.  color!background, color!

5.  text!font, line-height, text-align, text-decoration, text-transform!

6.  other!

Keep It Ordered!

Page 24: Perfect Styling - How to write better CSS

•  Separate content and container!

•  Dasherize class names!

•  Use special prefixes!•  .mod- for modifiers!•  .is- for states!•  .js- for js-related classes!

•  Avoid deep nesting, ids, type selectors, !important!

•  Don’t use redundant rules and chars!

•  Put rules in the particular order!

Wrapping Up!

Page 25: Perfect Styling - How to write better CSS

Thank you!!

Your questions, please!!ARTEM TABALIN!