What is the best way to define the className
attribute for a div
that contains 'hello world'?
<div dangerouslySetInnerHTML={{__html: '<div>hello world</div>'}} />
One option is to set it on the outer div like this:
<div dangerouslySetInnerHTML={{__html: '<div>hello world</div>'}} className='class-name'/>
and then style the child in CSS:
.class-name div {
(add your CSS styles here)
}
However, my goal is to directly set the className
on the div
containing 'hello world'
EDIT: Adding a class name to the injected HTML does not resolve the issue. For example:
let content = '<div class="class-name">hello world</div>'
<div dangerouslySetInnerHTML={{__html: content}}
This approach does not work well when the same content is used multiple times and can lead to CSS collisions (especially when using style-loader with CSS modules)