Last active
August 25, 2020 16:10
-
-
Save jpierson/95d21a3233a93f51bbe9c7faa8a7c81f to your computer and use it in GitHub Desktop.
Shadow DOM example for isolating HTML content embedded from a CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an example of the page rendered in Chrome.
