Keeping a dangerouslySetInnerHTML div separate from the global CSS to prevent any potential risks

As I render an email HTML string with all its styles defined in-line in my NextJS React app, I notice discrepancies in the styling:

<div id='emailContent' dangerouslySetInnerHTML={{ __html: sanitizedHtml }} />

It seems like there is a clash between global CSS styles and inline styles in the HTML string. This assumption is supported by the fact that rendering using an iframe (which isolates contents from global CSS) results in perfect styling:

<iframe srcDoc={sanitizedHtml} />

However, due to performance concerns, using the dangerousSetInnerHTML method is preferred over the iframe.

Is there a way to ensure that this div only applies the CSS styles defined within the HTML itself, ignoring any globally set styles?

My attempts so far:

I have tried setting the following CSS globally:

#emailContent { 
  all: revert; /* also tried unset */
}

#emailContent * { 
  all: revert; /* also tried unset */
}

This approach has resulted in inconsistent styling fixes - some emails are corrected while others remain unaffected.

If there is a simpler solution available, I would greatly appreciate your insights. Thank you in advance.

Answer №1

It was discovered that the use of iframe was causing slow loading times due to an unrelated problem. After resolving this issue, it appears that this is now the preferred solution!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Stacked Layout with Bootstrap 4 Carousel: A Step-by-Step Guide

Is it possible to stack the contents of each slide (an h2 and button) vertically instead of the default inline format of Bootstrap 4 Carousel? Below is the code I currently have: <div id="slider" class="carousel slide" data-ride="carousel"> < ...

Pick the item when the checkbox is selected

I am currently attempting to toggle the visibility of a select element based on whether a checkbox is checked or not, but it doesn't seem to be working as expected. My desired functionality is for the select element to be hidden upon page load and th ...

Changing the content of a DOM element containing nested elements using JavaScript/jQuery

Need some help with a DOM element that looks like this: <span>text</span> <b>some more text</b> even more text here <div>maybe some text here</div> How can I replace text with candy to achieve this result: <span> ...

Issue with Material Table not refreshing data after mutation occurs

Whenever a user includes more details, a mutation is performed in the database to add the new information. Subsequently, the local state is updated to include the new data in the lead. Although my mutation and state update successfully, there seems to be ...

What is the best way to remedy the bottom scroll issue?

I am currently working on a Messaging system and I need the scrollbar to stay fixed at the bottom so that when new data arrives, it automatically scrolls down. How can this be achieved using HTML/CSS? Please ignore any unprofessional formatting in the code ...

Exploring the Syntax of React

As I am still fairly new to react and have a basic understanding of Javascript. Following a tutorial, everything was clear until the instructor moved forward. Now, when I go back over the material, I find myself struggling to grasp this concept. render() ...

`My GitHub webpage is having trouble displaying images online`

I am experiencing difficulty with GitHub loading the images in my images folder. Strangely, it loads the background image without any issues, and when I launch the page offline, all the images load perfectly. I have checked that the images only start with ...

Turn off base64 encoding for background URLs in the Minify package

When I use the minify tool to compress my js and css files, I noticed that the files containing background:url(...) statements actually increased in size. This happened because the urls were encoded to base64. I tried to disable this base64 encoding featu ...

Querying MySQL with PHP code

I am trying to create a MySQL query that will search for names and surnames in the columns nome and cognome within the Prenotazioni table. The issue I'm facing is that the query needs to be able to search for both name and surname, or surname and nam ...

Customizing Thickness for Tab Labels in MUI 5

I have been trying to adjust the thickness of the label inside a tab using MUI 5, but so far I haven't had success. Here is what I have attempted: interface TabPanelProps { children?: React.ReactNode; index: number; value: number; } funct ...

Navigating through DOM object in jQuery - A Step by Step Guide

I'm facing a challenge with my HTML code. How can I navigate to the <br> tag using jQuery? Constraints: Avoid using class attributes. Do not utilize the split method. For instance: <html> <body> <div> <div> hello ...

Getting a server side variable from the client using socket.io

I am currently working on a React application using Node.js, Express, and socket.io. In my project, I have saved a variable that holds a number to be displayed on the frontend. However, I would like the frontend to request this variable from the backend. I ...

Alignment Troubles with Icons

I am having trouble positioning some icons on the far right of my 960 grid template header, next to my logo which is on the far left. I have tried multiple ways but can't seem to get it right. Any assistance would be greatly appreciated. Below is the ...

There is an issue with parsing: An unexpected * token was found

My React application has been trans-piled using babel and webpack. Our extensive lint configuration has never raised any issues with our code until today. I'm encountering an error Parsing error: Unexpected token * in files that do not have any impor ...

I'm just beginning to delve into Python and have a question about Beautiful Soup

Recently, I attempted to extract the web crawling element from Google Drive. Specifically, I was looking for the file's modification date. To find the necessary elements, I used F12 and discovered the following selector: body > div.ndfHFb-c4Y ...

Issues with Linear-Gradient functionality in NativeScript 8 on Android devices

I recently added a linear-gradient to an image in my NativeScript 8 app. Surprisingly, it seems to work perfectly on iOS, but I'm encountering some issues on Android. Despite trying solutions like using -webkit-linear-gradient(), the desired effect is ...

"Utilizing a background image within an email using a table data cell (

Struggling to add a background image quickly for a cell within a table in order to showcase dynamic text on top of it I attempted : <td style="background-image:url('bla... But it's not working as expected. What would be the most effective m ...

iOS search button with a unique blue outline border

On iOS, I'm seeing a strange blue border that I can't seem to get rid of. see image for reference I've exhausted all options like :focus and :active, but I can't pinpoint the source of the border. I've searched through different ...

Steps for deploying a pre-built next.js application on Vercel without using Git integration.(Note: Make sure to follow these

I am in search of a straightforward method to deploy my next.js application from a private Gitlab repository and existing build pipeline to Vercel without uploading my source code or running builds on their platform; just deploying the already "built" next ...

Anticipating the arrival of data from an unspecified child component prior to executing the effect

Suppose there is a parent component: function Parent({ children }) { useEffet(() => { // Perform action only after all children have returned their values via props (or context) }) return <div>{ children }</div> } How do I ensure ...