0

I have html stored in the database and I want to render it on my react page and for that I used

import low from "../css/low.module.css";
<div dangerouslySetInnerHTML={{__html: data}}/>

and this works perfectly. I am also using css modules and I want this html (from database) to detect the css. I can not use cssFileName.className as it is stored in the database.

For example

data = <div class="news_page_detail">
<p class="news_page_detail">My Para1<br class="Pc_only">My Para2</p>
<p class="news_page_detail">My Para3</p>
<p class="news_page_detail">My Para4
</p>
<a href="/someLink" class="btn-pink-b ovbg ">
<div class="btn-inner">
<div class="ovbg_pink"></div>
<span class="btn-pink-b-txt">My span1</span>
</div>
</a>

Is there any solution so that I apply the css modules here. I dont want to import the css as conventional way because they are interfering with the other css. Please help.

1 Answer 1

0

You can try the html-react-parser library. I have used it and I can style inline my content.

 import parse from 'html-react-parser'

 <div
      style={{ color: 'black' }}
 >
     {parse(item?.content)}
 </div>
Sign up to request clarification or add additional context in comments.

2 Comments

Lets say I have className myClassName in low.module.css and data = <div class="myClassName"></div>. So How I can apply this css ?
try <div className="myClassName>{parse(data}</div>

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.