Skip to content

Instantly share code, notes, and snippets.

@jpierson
Last active August 25, 2020 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpierson/95d21a3233a93f51bbe9c7faa8a7c81f to your computer and use it in GitHub Desktop.
Save jpierson/95d21a3233a93f51bbe9c7faa8a7c81f to your computer and use it in GitHub Desktop.
Shadow DOM example for isolating HTML content embedded from a CMS
<!doctype html>
<html>
<head>
<style>
.row {
display: flex;
}
.column {
flex: 50%;
padding: 10px;
height: 300px;
}
* {
color: Red;
}
</style>
</head>
<body>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<div id="content1">
SOME CONTENT FROM CMS
</div>
</div>
<div class="column" style="background-color:#bbb;">
<h2>Column 2</h2>
<div id="content2">
SOME MORE CONTENT FROM CMS
</div>
</div>
</div>
<script>
document
.getElementById("content1")
.attachShadow({ mode: 'open' })
.innerHTML = `
<style>
*{all:initial}
style{display: none}
div{display: block}
</style>
<h3>This text is not red</h3>
<div>slot content: <slot></slot></div>`;
document
.getElementById("content2")
.attachShadow({ mode: 'open' })
.innerHTML = `
<style>
*{all:initial}
style{display: none}
div{display: block}
</style>
<h3>This text is not red</h3>
<div>slot content: <slot></slot></div>`;
</script>
</body>
</html>
@jpierson
Copy link
Author

jpierson commented Aug 25, 2020

Here is an example of the page rendered in Chrome.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment