Text blocks rendering at same size in block-content-to-react

8 replies
Last updated: Feb 1, 2021
Hi again, I'm currently working on my blog and realized that the text blocks aren't rendering properly in block-content-to-react. For example, when I have a
Heading 1
font style and a
Normal
font style on the line below, the lines both render at the same size
AI Update

I can help you with this! The issue you're experiencing is likely because block-content-to-react (which is now deprecated) needs custom serializers to properly render different block styles like headings versus normal paragraphs.

First, I should mention that if you're still using block-content-to-react, you should migrate to @portabletext/react - it's the modern, officially supported library for rendering Portable Text in React.

The reason your headings and normal text are rendering at the same size is that you need to define custom components (serializers) for different block styles. Here's how to fix it:

import {PortableText} from '@portabletext/react'

const components = {
  block: {
    // Define different styles for block types
    h1: ({children}) => <h1 className="text-4xl font-bold">{children}</h1>,
    h2: ({children}) => <h2 className="text-3xl font-bold">{children}</h2>,
    h3: ({children}) => <h3 className="text-2xl font-bold">{children}</h3>,
    normal: ({children}) => <p className="text-base">{children}</p>,
  },
}

// Then use it in your component
<PortableText value={yourContent} components={components} />

The key insight is that Portable Text stores different block styles (h1, h2, normal, etc.) as different block types in the JSON, but without custom serializers, they all get rendered as plain elements without proper styling. You need to explicitly tell the renderer how to display each style.

If you're using Tailwind, you can also leverage @tailwindcss/typography for automatic styling:

<article className="prose">
  <PortableText value={yourContent} />
</article>

The prose class will automatically apply appropriate sizing and spacing to headings, paragraphs, and other text elements.

For a comprehensive guide on customizing Portable Text rendering, check out this guide on customizing Portable Text from schema to React component. It covers everything from schema setup to custom React components for rendering.

Show original thread
8 replies
Hi Brian! As you alluded to in your following comment, the default serializer should handle the h# and p (“normal”) styles. A few things I’d check are:
• In your portable text schema, are you passing those values as
h1
and
normal
? Are they within
styles[]
?• Do you have a serializer file and is it possibly overriding your normal or h1 styles?
• What is the HTML that’s being output? If you’re getting
p
and
h1
tags as you’d expect, do you have any CSS resets that are removing the default styling on
h1
elements?
On the off chance it helps, this is the start of my portableText schema in my sanity folder:

export default {
  name: "content",
  title: "Content",
  type: "array",
  of: [
    {
      type: "block",
      styles: [
        { title: "Normal", value: "normal" },
        { title: "H1", value: "h1" },
        { title: "H2", value: "h2" },
        { title: "H3", value: "h3" },
        { title: "H4", value: "h4" },
        { title: "Quote", value: "blockquote" },
      ],
      // More stuff
    },
  ],
}
Then on the front end, my serializers file makes no reference to
styles[]
(I just let it do its thing). I call it like this (with an import):

<BlockContent
  className="post__body"
  blocks={content}
  serializers={serializers}
  renderContainerOnSingleChild
/>

I just checked my schema and that is present. After playing around with the readme in block-content-to-react, this solved my issue

https://github.com/sanity-io/block-content-to-react#customizing-the-default-serializer-for-block-type
I tested the bold, italics, strikethrough and code lines and they work without specifications
I do have custom serializers for image and code, but I don't believe that would affect the default serializers
For example,
I'm also using tailwind css in conjunction with sanity
Ahhh found it

https://tailwindcss.com/docs/preflight
Turns out tailwind does this by default

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?