-
Notifications
You must be signed in to change notification settings - Fork 14
Research: Modular CSS
Modular, Reusable and Maintainable Code-base for Complex Web Projects
-
Separate Structure / from Skin / from Containers / from Content / from Behavior
-
Decouple Html from CSS: Keep a shallow depth of applicability (or don't make class styles location/structure specific).
-
Keep CSS selector specificity to a minimum; this allows for easy overrides.
-
Use "object-oriented" and component thinking in architecting css.
-
Use prefix name-spacing for organizing/categorizing selectors.
-
Modular CSS requires both visual and data semantics : use the class cascade and thoughtful naming to bring meaning to both.
- Use keyword prefixes to name-space the objects
Example:
scope-object
e-list
Example:
class-extension
nav nav-special
- These interplay with Javascript; reserve IDs for Javascript hooks
- Otherwise, class-names should follow native css convention, using lower-case-dash-notation for easy distinction from Javascript
Example:
#myId
.my-class
- Notate major object types (1) and their subtypes (2)
- Use specific keywords consistently and treat naming patterns consistently
- Proposed Reserved Prefixes:
-
Use "block" to refer to a reusable chunk of structured code.
-
Titles for blocks should be semantic to design objects (i.e. ux/design patterns: accordion, slider, billBoard, landingPage, content-primary, content-secondary)
-
Blocks gain meaning by their functional intent -- thus they are influenced by context.
-
Blocks have a patterned structure; keep this structure to maintain consistency across modules:
- wrapper bwp-object-title
- inner bin-object-title
- heading bhd-object-title
- body bbd-object-title
- footer bft-object-title
Example:
<div class="bwp-object-title">
<div class="bin-object-title">
<h2 class="bhd-object-title"></h2>
<div class="bbd-object-title">
... various html elements in here ...
</div>
<div class="bft-object-title">
... various html elements in here ...
</div>
</div>
</div>
- Use "element" to refer to the children content objects of a block
- Titles for elements should be semantic to data/information objects (i.e. content or data types: shopping-list, contact-information, author-bio, etc.)
Example:
e-object-title
-
Use "visuals" to refer to skin elements that have a high reuse factor
-
Visuals are design treatments and should be content/data structure agnostic
-
To keep visuals forward-proof keep names descriptive to design use but not specific to particulars (i.e. overlay-glow, editorial-masthead, fx-letter-press rather than 5px-line, red-border)
-
Visuals have various stylistic patterns for use:
- effects vfx-object-title
- type treatments vtt-object-title
- backgrounds vbg-object-title
- borders vbr-object-title
- shadows vsw-object-title
NOTE: See Sullivan's idea of visual semantics (link in references)
-
Use "layout" to refer to macro structural (i.e. container) code (i.e. major page sections or boxes)
-
Layouts help determine the major rendered flow of a page's structure
-
Layouts, like visuals are design objects-- they are context independent containers
-
To maintain forward-proof objects strive to keep names descriptive to use and base ratios rather than to specific "locations" (i.e. lgd-col-4, lnb-media-col-2 rather than right-column, left-column, top-box)
-
Layouts also have different facets, especially when working with frameworks and making decisions/changes in responsive design:
- grids lgd-object-title
- nested box lnb-object-title
NOTE: Grids are usually applied at/above the block wrapper level, while nested objects at/below the block inner level. Grids and Nested Objects are independent of structure (blocks) and content. You should be able to change out containers easily.
- Use "states" to refer to the behavioral changes (jQuery, animations, transitions, etc.)
- Naming should describe the behavior clearly from other states (i.e. an object's hidden state can be sIsHidden while it's visible state can be sNotHidden)
- States will be frequently used as JS hooks; use lowerCaseCamelNotation
Example:
sOnObjectTitle
sOffObjectTitle
- When tiling classnames together begin with the richest data semantic object first, then proceeding to structural context, stable layouts, followed by changing states and visuals last.
- This provides a reader (human and screen) with data rich information, first, and keeps a reliable pattern for the most change volatile classes towards the end of the string.
- Since CSS Cascade is applied from the style-sheet perspective and not on class call ordering, this pattern can remain fixed w/o impacting code behavior.
Example:
<ul class="e-shopping-list bbd-accordion-panel lnb-submenu sIsHidden vbg-gradient-glow">
- On Visual Semantics for CSS, Nichole Sullivan: http://www.stubbornella.org/content/2010/06/12/visual-semantics-in-html-and-css/
- Correct Granularity for CSS Objects, Nichole Sullivan: http://www.stubbornella.org/content/2010/06/21/css-granularity-architecture/
- Decoupling HTML from CSS, Jonathan Snook: http://coding.smashingmagazine.com/2012/04/20/decoupling-html-from-css/
- The BEM Method, from Yandex: http://bem.github.com/bem-method/pages/beginning/beginning.en.html
- Scoping Class Names with Prefixes, from Mark Otto: http://www.markdotto.com/2012/02/16/scope-css-classes-with-prefixes/
- CSS Conventions lower-case-dash-notation, from Stack Overflow: http://stackoverflow.com/questions/7560813/why-are-dashes-preferred-for-css-selectors-html-attributes
- CSS Dash Convention vs. Javascript camelCase, from Stack Overflow: http://stackoverflow.com/questions/2119629/javascript-and-css-using-dashes
- Naming Conventions, Practical Considerations, from Programmers Stack Exchange:http://programmers.stackexchange.com/questions/141988/practical-considerations-for-html-css-naming-conventions-syntax
- SMACSS, from Jonathan Snooks: http://smacss.com/book/
- OOCSS, from Nichole Sullivan: https://github.com/stubbornella/oocss/wiki
[ERIC] on JUNE 14
I don't see the purpose in using verbose naming conventions with module titles appended in front. Consider the example listed above: "e-shopping-list bbd-accordion-panel lnb-submenu sIsHidden vbg-gradient-glow". This sort of styling is very hard for the casual markup designer to understand. If we design our namespace right, modules can contribute to a shared namespace.
This discussion should probably be continued on the Conventions page rather than here.