Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
How to prevent bootstrap version collisions using shadow dom

Hello everyone hope you're all having a good day.

I'm making a web component that uses bootstrap 5. Since the shadow DOM is supposed to encapsulate styling I assumed that it would be a good way to prevent it from colliding with any sites that might be using older versions of bootstrap (since they tend to have the same class name). I feel like I'm missing something obvious, but whenever I include other versions of bootstrap in the html document they override what's in my component. I've been stuck on it for a few days now and haven't found anything to explain why this might be.

const template = document.createElement('template');
template.innerHtml = `
  <style>@import "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" </style>
  <div class="container">
    <p>All my web component stylings go on from here and use bootstrap classes as well</p>
  </div>
`

class MyWebComponent extends HTMLElement {
  constructor() {
    super();
    var shadow = this.attachShadow({ mode: 'open' });
    shadow.appendChild(template.content.cloneNode(true));
  }
}
window.customElements.define('my-web-component', MyWebComponent);

And my index.html file is simply

<head>
  <link rel="stylesheet" href="link to bootstrap3 or boostrap4.css" />
  <script type="module" src="linkToMyWebComponent"></script>
</head>
<body>
  <my-web-component />
</body>

I've noticed that if I manually define styles within the template html like

<style>.form-control { p: 10 }</style>

that it works just fine and overwrites all the stylings. Any help, or learning material would be much appreciated. Although feel like I've read at least two dozen long articles about templates, shadow dom, etc. on it at this point. The flattened shadow dom looks like it should be encapsulated from the older scripts, but no luck.

Thank you in advance!

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!