SlideShare a Scribd company logo
Web Components and Modular CSS 
@AndrewRota | CSS Dev Conf 2014
Modularity
UI Libraries
CSS Features 
Encapsulation 
Scope 
Interfaces 
Modularity
Modular CSS Patterns 
BEM 
SMACSS 
Atomic CSS 
OOCSS
BEM 
/* Block */ 
.nav { } 
/* Element */ 
.nav__item { } 
/* Block with Modifier */ 
.nav--hidden { } 
/* Element with Modifier */ 
.nav__item--current { }
SMACSS 
/* Module */ 
.nav { } 
/* Module with State */ 
.nav.is-current { } 
/* Module with Semantic Element Selector */ 
.nav > h2 { } 
/* Layout Style */ 
.l-inline { }
Web Components 
Web Components usher in a 
new era of web development 
based on encapsulated and 
interoperable custom elements 
that extend HTML itself. - Polymer
Web Components APIs 
Custom Elements 
HTML Templates 
HTML Imports 
Shadow DOM
Custom Elements 
<my-element>Hello World.</my-element> 
var MyElement = document.registerElement('my-element', { 
prototype: Object.create(HTMLElement.prototype) 
});
HTML Templates 
<template id="my-template"> 
<p>Hello World.</p> 
<!-- This image won't be downloaded on page load --> 
<img src="example.jpg" alt="Example"> 
</template> 
document.importNode( 
document.getElementById('my-template').content, 
true 
);
HTML Imports 
<link rel="import" href="/imports/my-component.html">
Shadow DOM 
// Create Shadow Root 
document.getElementById('my-element').createShadowRoot(); 
// Access Shadow Root 
document.getElementById('my-element').shadowRoot;
User Agent Shadow DOM 
<video src="#" controls></video> 
0:00
User Agent Shadow DOM 
<input type="date"> 
mm/dd/yyyy
Shadow DOM 
Shadow DOM. 
Light DOM. 
<div id="my-first-element"></div><p>Light DOM.</p> 
// Create Shadow Root 
var s = document.getElementById('my-first-element').createShadowRoot 
// Add Styles and Text 
s.innerHTML += '<style>p { color: crimson; margin: 5px 0 5px 0;}</s.innerHTML += '<p>Shadow DOM.</p>';
Content Insertion Points 
<div id="my-second-element"> 
<content></content> 
</div>
Shadow DOM and <content> 
Shadow DOM Start. 
Hello! 
Shadow DOM End. 
<div id="my-second-element"><p>Hello!</p></div> 
var s = document.getElementById('my-second-element').createShadowRoot 
s.innerHTML += '<p>Shadow DOM Start.</p>'; 
s.innerHTML += '<style>p { color: crimson; margin: 5px 0; }</style>' 
s.innerHTML += '<content></content>'; 
s.innerHTML += '<p>Shadow DOM End.</p>';
Into the Light 
/* pseudo-class for host element*/ 
:host { } 
/* functional pseudo-class, for host if it matches the selector argument :host() { } 
/* functional pseudo-class, for host context that matches selector :host-context() { } 
/* pseudo-element, for distributed notes rendered via a <content> ::content { }
Into the Dark 
/* pseudo-element for shadow roots */ 
::shadow { } 
/* combinator for selecting through shadow boundaries */ 
body /deep/ p { } 
[/deep/] is basically a super-descendant 
combinator. 
- CSS Scoping Module Draft, Issue 6
Let's Write a Component 
Hello world, I am a web component. 
<link rel="import" href="../assets/hello-world.html"> 
<hello-world>I am a <strong>web component</strong></hello-world
Let's Write a Component 
Hello world, I am a web component. 
<template id="hw"> 
<style> 
::content strong { color: crimson; } 
p { margin: 2px 20px 2px 0; } 
:host { border: 1px solid FireBrick; display: block; margin-right 
.hello { color: #91D4D; } 
</style> 
<p><span class="hello">Hello world</span>, <content></content 
</template>
Let's Write a Component 
Hello world, I am a web component. 
var importedDoc = document.currentScript.ownerDocument; 
var elementPrototype = Object.create(HTMLElement.prototype); 
elementPrototype.createdCallback = function() { 
var template = importedDoc.getElementById('hw').content; 
var clone = document.importNode(template, true); 
this.createShadowRoot().appendChild(clone); 
}; 
document.registerElement('hello-world', {prototype: elementPrototype
Can I Use??? 
Custom 
Elements 
HTML 
Templates 
HTML 
Imports 
Shadow 
DOM 
- - - - 
- - - - 
Flag - Flag Flag 
X - X X 
X X X X
Polyfills
When To Use Web Components? 
Third Party Widgets? 
Third Party UI Libraries? 
Internal UI Libraries? 
Web Component All the Things!?
Third Party Widgets 
<google-map 
latitude="29.954356" 
longitude="-90.067863"> 
</google-map>
Third Party UI Libraries 
CSS HTML JS 
<paper-radio-group selected="css"> 
<paper-radio-button name="css" label="CSS"></paper-radio-button 
</paper-radio-group> 
<paper-slider value="10"></paper-slider>
Internal UI Libraries 
<acme-corp--menu> 
<acme-corp--menu-item>Home</acme-corp--menu-item> 
<acme-corp--menu-item selected>About</acme-corp--menu-item> 
<acme-corp--menu-item>Contact Us</acme-corp--menu-item> 
</acme-corp--menu> 
<acme-corp--login-form 
ajax 
url="login.php"> 
</acme-corp--login-form>
Web Component Everything?? 
<acme-corp--app> 
<acme-corp--menu></acme-corp--menu> 
<acme-corp--content></acme-corp--content> 
<acme-corp--footer></acme-corp--footer> 
</acme-corp--app>
Probably Not (and that's OK) 
I don't ever see us going all in 
on Custom Elements for every 
possible thing ... Use native 
elements and controls when 
possible and supplement with 
custom elements. 
- Joshua Peek, Github Programmer
Small 
Open for Extension 
Documented 
Unit Tested 
Accessible 
Responsive 
Best Practices
Tooling
Frameworks
Towards a Component Driven Web
Thanks! 
Resources 
- WebComponents.org 
- Web Components: A Tectonic Shift for Web Development by Eric Bidelman 
- Web Components by Jarrod Overson and Jason Strimpel 
- Ten Principles for Great General Purpose Web Components 
Colophon 
This presentation was built with Shadow DOM, HTML Templates, HTML 
Imports, and the Custom Elements <slide-show> and <slide-content> 
using Web Component Slides.
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS
Web Components and Modular CSS

More Related Content

PDF
Introduction to web components
PDF
Web Components
PDF
Introduction to Web Components
PDF
Levent-Gurses' Introduction to Web Components & Polymer
PDF
Web Components Everywhere
PPT
Reaching for the Future with Web Components and Polymer
PPTX
Polymer and web component
PDF
Introduction to Web Components
Introduction to web components
Web Components
Introduction to Web Components
Levent-Gurses' Introduction to Web Components & Polymer
Web Components Everywhere
Reaching for the Future with Web Components and Polymer
Polymer and web component
Introduction to Web Components

What's hot (20)

PDF
Google Polymer Framework
PDF
Web Components + Backbone: a Game-Changing Combination
PDF
Polymer, A Web Component Polyfill Library
PDF
Polymer
PPTX
Polymer
PPTX
Web Components
PDF
A brave new web - A talk about Web Components
PPTX
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
PDF
Web Components & Polymer 1.0 (Webinale Berlin)
PDF
HTML5 - Introduction
PPTX
Google Developer Group(GDG) DevFest Event 2012 Android talk
PDF
Polymer & the web components revolution 6:25:14
PDF
The Complementarity of React and Web Components
PDF
Web components
PPTX
Web Components
PDF
Build Reusable Web Components using HTML5 Web cComponents
PDF
Intro to html 5
PDF
Web Components with Polymer (extra Polymer 2.0)
PDF
Custom Elements with Polymer Web Components #econfpsu16
PDF
Web components the future is here
Google Polymer Framework
Web Components + Backbone: a Game-Changing Combination
Polymer, A Web Component Polyfill Library
Polymer
Polymer
Web Components
A brave new web - A talk about Web Components
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
Web Components & Polymer 1.0 (Webinale Berlin)
HTML5 - Introduction
Google Developer Group(GDG) DevFest Event 2012 Android talk
Polymer & the web components revolution 6:25:14
The Complementarity of React and Web Components
Web components
Web Components
Build Reusable Web Components using HTML5 Web cComponents
Intro to html 5
Web Components with Polymer (extra Polymer 2.0)
Custom Elements with Polymer Web Components #econfpsu16
Web components the future is here
Ad

Viewers also liked (20)

PPTX
Καβάφης Κωνσταντίνος
PPTX
ΟΙ ΖΟΥΛΟΥ Παναγιώτα
PPT
Mεσαιωνικο καστρο λεμεσου
DOC
بحث د عهد حوري
PPTX
Μάγια Ζαχαρίας
PDF
49201940 schaffer-psihologia-copilului-partea-1
PPTX
Το καστρο της λεμεσου
PPTX
What is a startup
ODP
Short film festivals
PPTX
Το μεσαιωνικό κάστρο λεμεσού
PPT
Packpin SV2B presentation
DOCX
PPTX
3d printing....science....
PPTX
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
PPTX
Καβάφης Κωνσταντίνος
DOCX
Senarai nama tahun 1 (linus)
PDF
RENNIE COWAN - DIRECTOR REEL
PPTX
ΟΔΟΣ Δ. ΜΙΤΣΗ ΛΕΜΕΣΟΣ
PPTX
κάστρο του κολοσσιού
PPTX
RENNIE COWAN PHOTOGRAPHY
Καβάφης Κωνσταντίνος
ΟΙ ΖΟΥΛΟΥ Παναγιώτα
Mεσαιωνικο καστρο λεμεσου
بحث د عهد حوري
Μάγια Ζαχαρίας
49201940 schaffer-psihologia-copilului-partea-1
Το καστρο της λεμεσου
What is a startup
Short film festivals
Το μεσαιωνικό κάστρο λεμεσού
Packpin SV2B presentation
3d printing....science....
ΟΔΟΣ ΔΗΜΟΣΘΕΝΗ ΜΙΤΣΗ ΛΕΜΕΣΟΣ
Καβάφης Κωνσταντίνος
Senarai nama tahun 1 (linus)
RENNIE COWAN - DIRECTOR REEL
ΟΔΟΣ Δ. ΜΙΤΣΗ ΛΕΜΕΣΟΣ
κάστρο του κολοσσιού
RENNIE COWAN PHOTOGRAPHY
Ad

Similar to Web Components and Modular CSS (20)

PPTX
Web Components Revolution
PDF
Real World Web components
PDF
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
PDF
Web Components: The future of Web Application Development
PDF
Web Component
PDF
2014 03-25 - GDG Nantes - Web Components avec Polymer
PDF
Web components are the future of the web - Take advantage of new web technolo...
PDF
Modern Web UI - Web components
PPTX
Magic of web components
PPTX
Web components
PPTX
Introduction to Webcomponents
PPTX
Web Components: back to the future
PDF
BreizhBeans - Web components
PDF
Devoxx 2014-webComponents
PDF
Web Components - The Future is Here
PDF
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 3/3 - Web components avec ...
PPTX
Web components. Compose the web.
PPTX
Web components
PPTX
Web Components: The Future of Web Development is Here
PPTX
Web Components: The Future of Web Development is Here
Web Components Revolution
Real World Web components
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
Web Components: The future of Web Application Development
Web Component
2014 03-25 - GDG Nantes - Web Components avec Polymer
Web components are the future of the web - Take advantage of new web technolo...
Modern Web UI - Web components
Magic of web components
Web components
Introduction to Webcomponents
Web Components: back to the future
BreizhBeans - Web components
Devoxx 2014-webComponents
Web Components - The Future is Here
ENIB 2015-2016 - CAI Web - S01E01- Côté navigateur 3/3 - Web components avec ...
Web components. Compose the web.
Web components
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here

More from Andrew Rota (16)

PDF
Integrating React.js Into a PHP Application: Dutch PHP 2019
PDF
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
PDF
Getting Started with GraphQL && PHP
PDF
Tutorial: Building a GraphQL API in PHP
PPTX
Building a GraphQL API in PHP
PDF
Ten practical ways to improve front-end performance
PDF
Component Based UI Architectures for the Web
PDF
Client-Side Performance Monitoring (MobileTea, Rome)
PDF
Integrating React.js Into a PHP Application
PDF
Effectively Monitoring Client-Side Performance
PDF
UI Rendering at Wayfair
PDF
Better PHP-Frontend Integration with Tungsten.js
PDF
Tungsten.js: Building a Modular Framework
PDF
Why Static Type Checking is Better
PDF
An Exploration of Frameworks – and Why We Built Our Own
PDF
Bem methodology
Integrating React.js Into a PHP Application: Dutch PHP 2019
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Getting Started with GraphQL && PHP
Tutorial: Building a GraphQL API in PHP
Building a GraphQL API in PHP
Ten practical ways to improve front-end performance
Component Based UI Architectures for the Web
Client-Side Performance Monitoring (MobileTea, Rome)
Integrating React.js Into a PHP Application
Effectively Monitoring Client-Side Performance
UI Rendering at Wayfair
Better PHP-Frontend Integration with Tungsten.js
Tungsten.js: Building a Modular Framework
Why Static Type Checking is Better
An Exploration of Frameworks – and Why We Built Our Own
Bem methodology

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Approach and Philosophy of On baking technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity

Web Components and Modular CSS

  • 1. Web Components and Modular CSS @AndrewRota | CSS Dev Conf 2014
  • 4. CSS Features Encapsulation Scope Interfaces Modularity
  • 5. Modular CSS Patterns BEM SMACSS Atomic CSS OOCSS
  • 6. BEM /* Block */ .nav { } /* Element */ .nav__item { } /* Block with Modifier */ .nav--hidden { } /* Element with Modifier */ .nav__item--current { }
  • 7. SMACSS /* Module */ .nav { } /* Module with State */ .nav.is-current { } /* Module with Semantic Element Selector */ .nav > h2 { } /* Layout Style */ .l-inline { }
  • 8. Web Components Web Components usher in a new era of web development based on encapsulated and interoperable custom elements that extend HTML itself. - Polymer
  • 9. Web Components APIs Custom Elements HTML Templates HTML Imports Shadow DOM
  • 10. Custom Elements <my-element>Hello World.</my-element> var MyElement = document.registerElement('my-element', { prototype: Object.create(HTMLElement.prototype) });
  • 11. HTML Templates <template id="my-template"> <p>Hello World.</p> <!-- This image won't be downloaded on page load --> <img src="example.jpg" alt="Example"> </template> document.importNode( document.getElementById('my-template').content, true );
  • 12. HTML Imports <link rel="import" href="/imports/my-component.html">
  • 13. Shadow DOM // Create Shadow Root document.getElementById('my-element').createShadowRoot(); // Access Shadow Root document.getElementById('my-element').shadowRoot;
  • 14. User Agent Shadow DOM <video src="#" controls></video> 0:00
  • 15. User Agent Shadow DOM <input type="date"> mm/dd/yyyy
  • 16. Shadow DOM Shadow DOM. Light DOM. <div id="my-first-element"></div><p>Light DOM.</p> // Create Shadow Root var s = document.getElementById('my-first-element').createShadowRoot // Add Styles and Text s.innerHTML += '<style>p { color: crimson; margin: 5px 0 5px 0;}</s.innerHTML += '<p>Shadow DOM.</p>';
  • 17. Content Insertion Points <div id="my-second-element"> <content></content> </div>
  • 18. Shadow DOM and <content> Shadow DOM Start. Hello! Shadow DOM End. <div id="my-second-element"><p>Hello!</p></div> var s = document.getElementById('my-second-element').createShadowRoot s.innerHTML += '<p>Shadow DOM Start.</p>'; s.innerHTML += '<style>p { color: crimson; margin: 5px 0; }</style>' s.innerHTML += '<content></content>'; s.innerHTML += '<p>Shadow DOM End.</p>';
  • 19. Into the Light /* pseudo-class for host element*/ :host { } /* functional pseudo-class, for host if it matches the selector argument :host() { } /* functional pseudo-class, for host context that matches selector :host-context() { } /* pseudo-element, for distributed notes rendered via a <content> ::content { }
  • 20. Into the Dark /* pseudo-element for shadow roots */ ::shadow { } /* combinator for selecting through shadow boundaries */ body /deep/ p { } [/deep/] is basically a super-descendant combinator. - CSS Scoping Module Draft, Issue 6
  • 21. Let's Write a Component Hello world, I am a web component. <link rel="import" href="../assets/hello-world.html"> <hello-world>I am a <strong>web component</strong></hello-world
  • 22. Let's Write a Component Hello world, I am a web component. <template id="hw"> <style> ::content strong { color: crimson; } p { margin: 2px 20px 2px 0; } :host { border: 1px solid FireBrick; display: block; margin-right .hello { color: #91D4D; } </style> <p><span class="hello">Hello world</span>, <content></content </template>
  • 23. Let's Write a Component Hello world, I am a web component. var importedDoc = document.currentScript.ownerDocument; var elementPrototype = Object.create(HTMLElement.prototype); elementPrototype.createdCallback = function() { var template = importedDoc.getElementById('hw').content; var clone = document.importNode(template, true); this.createShadowRoot().appendChild(clone); }; document.registerElement('hello-world', {prototype: elementPrototype
  • 24. Can I Use??? Custom Elements HTML Templates HTML Imports Shadow DOM - - - - - - - - Flag - Flag Flag X - X X X X X X
  • 26. When To Use Web Components? Third Party Widgets? Third Party UI Libraries? Internal UI Libraries? Web Component All the Things!?
  • 27. Third Party Widgets <google-map latitude="29.954356" longitude="-90.067863"> </google-map>
  • 28. Third Party UI Libraries CSS HTML JS <paper-radio-group selected="css"> <paper-radio-button name="css" label="CSS"></paper-radio-button </paper-radio-group> <paper-slider value="10"></paper-slider>
  • 29. Internal UI Libraries <acme-corp--menu> <acme-corp--menu-item>Home</acme-corp--menu-item> <acme-corp--menu-item selected>About</acme-corp--menu-item> <acme-corp--menu-item>Contact Us</acme-corp--menu-item> </acme-corp--menu> <acme-corp--login-form ajax url="login.php"> </acme-corp--login-form>
  • 30. Web Component Everything?? <acme-corp--app> <acme-corp--menu></acme-corp--menu> <acme-corp--content></acme-corp--content> <acme-corp--footer></acme-corp--footer> </acme-corp--app>
  • 31. Probably Not (and that's OK) I don't ever see us going all in on Custom Elements for every possible thing ... Use native elements and controls when possible and supplement with custom elements. - Joshua Peek, Github Programmer
  • 32. Small Open for Extension Documented Unit Tested Accessible Responsive Best Practices
  • 35. Towards a Component Driven Web
  • 36. Thanks! Resources - WebComponents.org - Web Components: A Tectonic Shift for Web Development by Eric Bidelman - Web Components by Jarrod Overson and Jason Strimpel - Ten Principles for Great General Purpose Web Components Colophon This presentation was built with Shadow DOM, HTML Templates, HTML Imports, and the Custom Elements <slide-show> and <slide-content> using Web Component Slides.