reactjstailwind-cssreact-markdown

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>
);

Solution

  • 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>