1

I don't want to mix my application with the css that is included in the html element that i'm receiving.

Is there a way to prevent dangerouslySetInnerHTML from inheriting style from the current page?

return (<div dangerouslySetInnerHTML={{ __html: template }}></div>)

2 Answers 2

3

You could display the content in an iframe, which would have its own CSS.

Not the solution you're asking for, but you could instead add a class to your div and override the styles that you don't want to impact your HTML content. This might be reasonable if you don't have many styles to override.

Sign up to request clarification or add additional context in comments.

1 Comment

Good response. Unless it's a very simple page and you know exactly what you're getting from the innerHTML, (which, based on the word "dangerous" I wouldn't assume), the iframe is probably the safest bet.
0

You can do something like this:

return (
    <div 
        dangerouslySetInnerHTML={{ __html: template }} 
        style={{all : "initial"}}>
    </div>
)

This style specifies that all the element's properties should be changed to their initial values.

You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/CSS/all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.