Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Tailwind CSS when using the react-markdown component

The markdown is not working except for italic and bold. I have figured out that the problem is caused by Tailwind CSS because of how it handles text-size and other styles. If I comment out the index.css import (which defines the directives for Tailwind) in my index.jsx, all markdown types like heading, code, etc. work fine.

News.jsx

import ReactMarkdown from 'react-markdown';
import { useState } from 'react';

function News() {
  const [markdown, setMarkdown] = useState('# I am heading');
  
  return (
    <div>
      <textarea value={markdown} onChange={e => setMarkdown(e.target.value)} />
      <ReactMarkdown>{markdown}</ReactMarkdown>
    </div>
  );
}

export default News;

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from "react-router-dom";
import './index.css'
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Router>
    <App />
    </Router>
  </React.StrictMode>
);
like image 656
ayex Avatar asked Feb 15 '26 09:02

ayex


2 Answers

You should add typography plugin in your tailwindcss

1.install npm install -D @tailwindcss/typography

2.add the plugin to your tailwind.config.js file:

module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('@tailwindcss/typography'),
    // ...
  ],
}
  1. use prose
<div class="prose lg:prose-xl">
  {{ markdown }}
</div>
like image 80
Eric Tsai Avatar answered Feb 17 '26 23:02

Eric Tsai


For any other viewers, i actually had to both...

  1. install @tailwindcss/typography as eric suggested.
  2. set <ReactMarkdown className="prose"># Heading 1</ReactMarkdown> as denis suggested.
like image 37
kevin Avatar answered Feb 18 '26 00:02

kevin