This is a common problem that does not (yet) have a simple, universal solution.
Shadow DOM
Ideally you would use Shadow DOM, as it is specifically intended to solve such a problem:
Shadow DOM provides encapsulation for DOM and CSS in a Web Component.
Shadow DOM makes it so these things remain separate from the DOM of
the main document. You can also use Shadow DOM by itself, outside of a
web component.
Why would you want to keep some code separate from the rest of the
page? One reason is that on a large site, for example, if the CSS is
not carefully organized, the styling for the navigation can "leak"
into the main content area where it was not intended to go, or
vice-versa. As a site or an app scales, this kind of thing becomes
difficult to avoid.
Shadow DOM is well-supported as of 2022.
Iframes
On the other end of the spectrum is an iframe
. iframes
are supported everywhere, but may require a larger change to the page than you are able to make. More importantly, iframes
introduce undesirable scrolling behaviors on iOS that may change from version to version.
Targeted Reset
A practical, though not foolproof, solution is to create a CSS reset that only targets the content you want isolated.
/* The start of a simple reset */
#email_content * {
border: 0;
margin: 0;
padding: 0;
}
This is one of the cases where it may be required to mark a rule as !important
.
Targeted Unset (all/initial/unset)
Reset/remove CSS styles for element only