The global CSS header element in Next.js is not being applied to parsed markdown files; instead, it is being replaced by Tailwind CSS

After attempting to convert Markdown into HTML, I encountered an issue. While the markdown parsed correctly, the rendered output did not match my expectations. Specifically, I expected the headers to automatically appear bold and with a larger font size, but they did not.

Upon inspecting the browser, I noticed that the font-size and font-weight properties were overridden by values inherited from tailwind.css:1. When I manually disabled this inheritance, the headers displayed as intended per my global CSS settings. I am confused about why the tailwind css is replacing the font-size and font-weight.

Here is a snippet of my global CSS styling:

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

h1,h2,h3,h4 {
    font-weight: 700;
    line-height: 1.3333333;
    letter-spacing: -.025em;
    padding: 1em;
}

h1 {
    font-size: 1.5em;
}

h2 {
    font-size: 1em;
}

The problematic code resides in the following page:

import Navbar from '../components/Navbar'
const showdown  = require('showdown');
const fs  = require("fs");

const sop = (props) =>{
    return (
        <>
        <Navbar />
        <div className="flex flex-col items-center ">
            <div className="w-2/4">
                <div dangerouslySetInnerHTML={{ __html:props.body}}/> 
            </div>
        </div>
        </>
    )
}
export async function getStaticProps(){
    let content = fs.readFileSync('./_posts/sop.md','utf8');
    let converter = new showdown.Converter();
    let html = converter.makeHtml(content);

    return {
       props : {body:html}
    }
}
export default sop

Any guidance on how to resolve this issue would be greatly appreciated.

Answer №1

I encountered a similar issue when I added Tailwind to my NextJS project and it affected the Markdown formatting. Despite using gray-matter and markdown-it for parsing markdown, Tailwind was overriding the styles in my project. To resolve this, I had to make changes in the globals.css file:

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

To solve this problem, I followed the steps outlined in the Tailwind documentation at https://tailwindcss.com/docs/installation and installed the Tailwind Typography plugin from https://tailwindcss.com/docs/typography-plugin

Here is how you can include them in your project:

npm install npm install -D tailwindcss
npx tailwindcss init
npm install -D @tailwindcss/typography

Add the plugin to your tailwind.config.js file:

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [
    require('@tailwindcss/typography'),
  ],
}

Finally, to ensure that the Markdown formatting is applied correctly from your .md files, remember to include the "prose" class. Within the pages directory where your markdown files are parsed, add className="prose" to the wrapping tags like div or article.

export default function Posts({posts}){  
  return (
    <>
      {posts.map((post, key) => {
            return (
              <>
                <div className="container" key={key}>
                  <article className="prose prose-base prose-slate mx-auto" key={key}>   
                      <div dangerouslySetInnerHTML={{ __html: md().render(content) }} />
                  </article>
                </div>
              </>
            )
        })}
    </>
)

For more information on parsing Markdown files with Next JS, check out this helpful guide: https://dev.to/alexmercedcoder/2022-tutorial-on-creating-a-markdown-blog-with-nextjs-3dlm

Answer №2

I discovered the solution after coming across this insightful article.

To summarize, I needed to establish a new class and incorporate my modifications within it. The overall CSS should be adjusted as follows:

//global.css
@tailwind base;
@tailwind components;
@tailwind utilities;

.parsedown h1 {
  @apply text-2xl font-extrabold py-4;
}
...

Then, apply the newly created class to the parsed markdown.

//page.js
...
<div className="w-2/4 parsedown">
  <div dangerouslySetInnerHTML={{ __html:props.body}}/> 
</div>
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The upcoming page.tsx export force-dynamic is failing to render dynamically

My dilemma involves a page that I am trying to render dynamically rather than statically. Unfortunately, the commonly suggested solution of simply using: export const dynamic = "force-dynamic"; has not yielded the desired results for me. I have ...

Tips for showcasing a table generated from various input types on a separate page after pressing the submit button

After creating two JavaScript functions, I am eager to utilize both of them upon pressing the submit button on my form. The first function is already integrated into the submit button and activates a paragraph display upon submission. Now, I intend to sh ...

Safari 5 experiences delays with JQuery and CSS3 animations

Encountering issues with jQuery animations specifically in Safari 5 on my Windows system. Struggling with a simple image fade slider that still has some flickering. The slider involves click events moving two pages at once, one active page right and the o ...

Tips for displaying Next.js dynamic paths on a static S3/CloudFront website

Help Needed with Next.js Setup on S3 and CloudFront While setting up Next.js with static website hosting on S3 and CloudFront, I have encountered an issue with dynamic routes. My directory structure is as follows: pages/ index.js about.js [id].js A ...

Combining and organizing user authentication data from nextauth.js provider and credentials in a session (nextjs)

I am currently working on a project that utilizes nextauth version 4.19 with 'credentials' (db) and Google as the provider. Everything runs smoothly with the 'credentials' provider, and I am able to retrieve a session.user containing a ...

Update Bootstrap Vue to change the colors of the breadcrumbs

Struggling to customize the look of bootstrap-vue breadcrumbs? I've attempted various combinations of classes and styles in the breadcrumb and breadcrumb-item tags, but to no avail. Every time, I end up with blue or grey text on a light grey backgroun ...

What is the best way to eliminate the gap between spans in Bootstrap?

Imagine you have the following code: <div class="row-fluid"> <div class="span6"></div><!--span6 END--> <div class="span6"></div><!--span6 END--> </div><!--row END--> Now envision two red b ...

How to instantly return progress bar to zero in bootstrap without any animations

I am currently working on a task that involves multiple actions, and I have implemented a bootstrap progress bar to visually represent the progress of each action. However, after completion of an action, the progress bar is reset to zero using the followi ...

Tips for creating and exporting a Next.js application that utilizes getServerSideProps functionality

I've recently started using next js and I've been attempting to build and export my next js application with getServerSideProps included, but unfortunately, an error keeps popping up. Let's take a look at my package.json: "scripts&qu ...

The behavior of the CSS Position property is exhibiting unique characteristics when used in

I am encountering an issue where an image is displaying differently based on the position attribute in Firefox compared to Chrome and IE11. Take a look at the preview on . Notice how the bottom tip of "Fountain Valley" is incorrectly positioned in Firefox ...

"An error occured on the server: ReferenceError - 'self' is not defined" when utilizing the powerbi-client-react library in a TypeScript project

Recently, I integrated the powerbi-client-react library into my TypeScript project. However, upon running the project, I encountered the following error: Here is a snippet of my component code: import { PowerBIEmbed } from 'powerbi-client-react' ...

Can PushState be used to Ajaxify CSS files?

Currently, I am in the process of developing a basic website and have decided to incorporate the Ajaxify library for seamless page transitions. One challenge I have encountered involves the combination of global CSS files (applied throughout the entire sit ...

The Enigma of the Empty Gap When Employing "display:table/table-cell"

Struggling to understand why there is a blank space between the image and the menu. Here is the HTML and CSS related to it: <header class="header_nav"> <img src="img/logo.png" class="logo"/> <nav class="nav_bar"> <u ...

Show flexibility in the grandchildren of a row layout

My goal is to achieve a layout similar to the image below using the flex "system", but I am facing a challenge with some generated directives that are inserted between my layout div and the flex containers: https://i.sstatic.net/nq4Ve.png <div id="i-c ...

Top approach to generate and store CSRF tokens for OAuth requests in a Node.js environment

Recently, I was given the task of developing a custom OAuth login and signup flow. One question that has been on my mind is the most effective way to generate and store CSRF tokens for OAuth requests. I came across various articles discussing this topic, s ...

The hover functionality fails to activate when a z-index is applied

My issue revolves around 2 divs: one containing a link and the other a box-shaped container. The link is set with position:fixed; causing it to fly over the container div. To resolve this, I attempted to assign a negative z-index value to the link, but unf ...

Encountering CORS issue specifically on Safari, yet functioning without any trouble on other

While working on a simple project, I came across an issue specifically with Safari browser. The error message I received was: '[Error] Fetch API cannot load https://randomuser.me/api due to access control checks.' The project is built using Nex ...

Issue encountered while attempting to run Next.js application. Halts abruptly shortly after initialization

After installing the Next.js app, I encountered an issue where the app would not start and would stop immediately. My terminal displayed the following: PS D:\Nextjs\MyApp> npm run dev > dev > next dev ready - started server on 0.0.0.0: ...

Why won't hover over function properly in separate divs for two items?

My goal is to show text when hovering over a logo, but the text and logo are in different divs. Despite trying various solutions like using display: none and display: block, I still can't get it to work. import styles from '../styles/Float.module ...

Tips for effectively utilizing CSS classes with current-device

Attempting to lock the screen orientation in portrait mode for mobile and iPad devices using a CSS snippet found on css-tricks.com. @media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) { html { transform: rotate ...