2

I have to integrate my webpage code into a big project. Entire project code is inside container div which is included inside <main> tag. When I add the container div in <main> tag, the project css overwrites my css. Is there a way where we can write a div that creates its own css environment and stops from outside css affecting inner tags?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Smith
  • 1,266
  • 13
  • 31

2 Answers2

2

Option 1: iframe will scope your html, but you will not be able to access dom.

Option 2:

reset CSS with

.your-div{
  all: initial;
  * {
    all: unset;
  }
}
segu
  • 321
  • 2
  • 12
0

You can use an iframe with the srcdoc attribute. Things inside an iframe are immune to outside css!

iframe {display:block; border:none}

/* css to demonstrate the immune div */

div {color:red; background:yellow; border:solid;}
<div>This is a standard div</div>
<iframe srcdoc="<div>This is a framed div, immune to css</div>"></iframe>

See <iframe> on MDN for more info and pitfalls.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150