css naming conventions

28
1 CSS Naming Conventions Mark Aplet

Upload: visual28

Post on 15-Apr-2017

779 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Css naming conventions

1

CSS Naming Conventions

Mark Aplet

Page 2: Css naming conventions

2

Page 3: Css naming conventions

3

Page 4: Css naming conventions

4

CSS is difficult* Specificity Inheritance Cascade Class Names

*For non-css developers

Page 5: Css naming conventions

5

Page 6: Css naming conventions

6

Page 7: Css naming conventions

7

Page 8: Css naming conventions

8

Poor Naming Conventions Brittle & easy to break styles Unmaintainable Difficult to collaborate

• Onboard new members

Spaghetti code Inline CSS

Page 9: Css naming conventions

9

CSS Methodologies BEM SMACSS SUIT OOCSS

Page 10: Css naming conventions

10

CSS MethodologiesPros Reduced conflict Maintainable Team friendly

Cons Verbose Less readable Limited code reuse

Page 11: Css naming conventions

11

BEM (Block, Element, Modifier) Common ground for teams Less prone to failure due to brittle naming

conventions Little Specificity/Inheritance issues

Page 12: Css naming conventions

12

Block logically and functionally independent component Blocks can be nested inside any other blocks

• Example: head block can include logo, and search block

Blocks should be location agnostic Acts as a namespace

Page 13: Css naming conventions

13

Elements Child element of a block that cannot be used

outside of the block If code can be reused, it's a block not element

Page 14: Css naming conventions

14

Modifier Defines the appearance or behavior of a block or

element

Page 15: Css naming conventions

15

BEM Example.block-name__element-name—modifier-name

. search-results

. search-results__hit-title

. search-results__hit-title--featured

Page 16: Css naming conventions

16

Naming Convention Lower case alpha numeric characters Hyphen between words (block-name) Double underscore to separate Blocks from

elements (block-name__elem-name) Double hyphen to separate elements from

modifiers (block-name__elem-name--mod-name)

Page 17: Css naming conventions

17

Best Practice Unique Block Names Only style class names Avoid cascading selectors Block names always prepended to elements and

modifiers CSS should be kept flat. Nesting should not be

required

Page 18: Css naming conventions

20

Semantic CSS Emphasis on complete separation of concerns HTML5 Hyper-minimal markup CSS l33t

Page 19: Css naming conventions

21

OOCSS My first methodology Predictable, performant Too Dry Difficult to change themes Productivity plumited

Page 20: Css naming conventions

22

Mark’s Approach Create a plan… then code Work in small modules Namespace or nest module Don’t nest more than 3-4 levels Don’t ration classes or characters Use semantic class names

Page 21: Css naming conventions

23

Naming Strategy Content, Context, Module Modifiers Functional Presentational

Page 22: Css naming conventions

24

Content or Module Opportunity to be semantic Hard part is not looking at the design Think core content or module Not .more-button – Use .button.more

Page 23: Css naming conventions

25

Modifiers or Extenders Extend the base class to include enhanced look

and feel or functionality Namespaced or nested .btn-primary, .btn-secondary, .btn-more

Page 24: Css naming conventions

26

Functional Never styled alone Use namespace or nested .active, .disabled, .open, .closed

Page 25: Css naming conventions

27

Presentational Styled alone Utility classes Ultra generic .small, .large, .muted, .img-left, .img-right

Page 26: Css naming conventions

29

Reuse Review mockups and note similar module Annotate potential class names

Page 27: Css naming conventions

30

.list-ruled.list-divided.list-separate

d .more

.headline-related

.header-divider

.header-overstated

.text-muted

Page 28: Css naming conventions

31

Lessons Learned Use any methodology, but stick to it Don’t ration classes or characters If you don’t have a plan, your just guessing Look to frameworks or methodologies for patterns