JavaScript How to sandboxing a <style> declaration in html?

I'm building an interface for a webmail: I receive the content of the mail, I put it in a <div> element where it display the message. The problem is that every mail has the <style> tag with the rules to style the message, but is possible that some of these rules are valid even for my html code, the one outer of the div where I display the message.

e.g.

<body>
<h1>inbox</h1>
<div class="message">{here i put the message html}</div> 
</body>

the message html contain this:

<style>
body { background: red}
</style>

So, when my page renders, it has the red background. I want that the properties declared in the <div class="message"> are rendered only here.

Answer:1

You could use an iframe with srcdoc property, like

<iframe srcdoc="&lt;style&gt;body { background: red}&lt;/style&gt;"></iframe>

The whole e-mail content must be placed in the srcdoc, properly escaped.

You can read more about it here.

Answer:2

You can do this by changing the CSS rule from:

<style>
body { background: red}
</style>

To:

<style>
.message { background: red}
</style>

And prefix every CSS rule that must be applied only inside <div> with class message, with the .message selector.

Answer:3

Scoped CSS is what you need.

<div>
  <style scoped>
    h1 { color: FireBrick;   }
    p  { color: SaddleBrown; }
  </style>
  <h1>This is an H1 in a scoped div. Regardless of global styles the text should be "FireBrick".</h1>
  <p>This is a paragraph in a scoped div. The text should be "SaddleBrown".</p>
</div>

Now, the styles will only apply to h1 and p that are inside the div.

Answer:4

i am creating a youtube chrome extension and try to load extension from background page automatically when any youtube video or channel page open but extension will not load untill i refresh same page ...

i am creating a youtube chrome extension and try to load extension from background page automatically when any youtube video or channel page open but extension will not load untill i refresh same page ...

  1. chrome extension will not stay enabled
  2. chrome extension will not remove
  3. chrome extension will not install

I have result array on script like this: var myurlsave = [{"url_field":"yahoo","enabled":true},{"url_field":"google","enabled":true},{"url_field":"bing","enabled":true}]; I want to extract it to ...

I have result array on script like this: var myurlsave = [{"url_field":"yahoo","enabled":true},{"url_field":"google","enabled":true},{"url_field":"bing","enabled":true}]; I want to extract it to ...

I'm trying to limit boundaries and I'm running into issues. I'm upscaling an image from another canvas and then implementing zoom and pan. My issue (code below) is with limiting/capping the offsetx/...

I'm trying to limit boundaries and I'm running into issues. I'm upscaling an image from another canvas and then implementing zoom and pan. My issue (code below) is with limiting/capping the offsetx/...

Within an ng-repeat block I have textboxes. To detect when the content differs from the original, the original data is stored in a variable. <tr data-ng-repeat="p in products"> <td> ...

Within an ng-repeat block I have textboxes. To detect when the content differs from the original, the original data is stored in a variable. <tr data-ng-repeat="p in products"> <td> ...

  1. angular best practice file structure
  2. angular best practice error handling
  3. angular best practice
  4. angular best practice structure
  5. angular best practice folder structure
  6. angular best practices guidelines for project structure
  7. angular best practice service
  8. angular best practice for app structure (public)
  9. angular best practice modules
  10. angular best practice interface
  11. angular best practice documentation
  12. angular best practice routing
  13. angular best practices directory structure
  14. angular best practice crud
  15. angular best practice medium
  16. angular best practices 2019
  17. angular best practices 2018
  18. angular best practices github
  19. angular best practices architecture
  20. angular best practices pdf