Skip to content
Documentation
nextra-theme-docs
Advanced
Rendering Tables

Rendering Tables

Rendering Tables

GFM syntax

In markdown is preferable write table via GFM syntax (opens in a new tab).


_10
| left | center | right |
_10
| :----- | :----: | ----: |
_10
| foo | bar | baz |
_10
| banana | apple | kiwi |

will be rendered as:

leftcenterright
foobarbaz
bananaapplekiwi

HTML Literal Tables

If you'll try render the following literal <table /> element:


_21
<table>
_21
<thead>
_21
<tr>
_21
<th>left</th>
_21
<th align="center">center</th>
_21
<th align="right">right</th>
_21
</tr>
_21
</thead>
_21
<tbody>
_21
<tr>
_21
<td>foo</td>
_21
<td align="center">bar</td>
_21
<td align="right">baz</td>
_21
</tr>
_21
<tr>
_21
<td>banana</td>
_21
<td align="center">apple</td>
_21
<td align="right">kiwi</td>
_21
</tr>
_21
</tbody>
_21
</table>

you'll get the following result:

leftcenterright
foobarbaz
bananaapplekiwi
⚠️

Confused by unstyled elements? We explained here, why this happens.

Dynamic Tables

How to Write

Want to render dynamic table? You can use embedded JavaScript expressions into your table for it:


_19
<table>
_19
<thead>
_19
<tr>
_19
<th>Country</th>
_19
<th>Flag</th>
_19
</tr>
_19
</thead>
_19
<tbody>
_19
{[
_19
{ country: 'France', flag: '🇫🇷' },
_19
{ country: 'Ukraine', flag: '🇺🇦' }
_19
].map(item => (
_19
<tr key={item.country}>
_19
<td>{item.country}</td>
_19
<td>{item.flag}</td>
_19
</tr>
_19
))}
_19
</tbody>
_19
</table>

will be rendered as:

CountryFlag
France🇫🇷
Ukraine🇺🇦
⚠️

Confused by unstyled elements? We explain below 👇, why it's happens.

Unexpected Result

Table looks different compared to GFM syntax table:

  1. only children of table body <tbody /> is styled

  2. table header is unstyled

  3. table doesn't have margin top

Why This Happens

MDX2 doesn't replace literal HTML elements with <MDXProvider />.

Adam Wathan, creator of Tailwind CSS submitted an issue (opens in a new tab) in MDX2 to have some an escape hatch that we can name like:

please only transform markdown tags, not literal HTML tags

Table header looks unstyled since not replaced with Nextra's MDX components <tr />, <th /> and <td />, for the same reason <table /> literal is not replaced and doesn't have default margin-top aka mt-6.

Ways to Fix It

One-Time Fix

Just wrap your table with curly braces { and }, e.g.


_10
{<table>
_10
...
_10
</table>}

Changing Default Behaviour

If this thing is still confusing for you, and you want to use regular literal HTML elements for your tables, do the following:

Install remark-mdx-disable-explicit-jsx package


_10
pnpm add remark-mdx-disable-explicit-jsx

Setup

Configure plugin in nextra function inside next.config.mjs file

{2,8-13}

_17
import nextra from 'nextra'
_17
import remarkMdxDisableExplicitJsx from 'remark-mdx-disable-explicit-jsx'
_17
_17
const withNextra = nextra({
_17
theme: '@visulima/nextra-theme-docs',
_17
themeConfig: './theme.config.tsx',
_17
mdxOptions: {
_17
remarkPlugins: [
_17
[
_17
remarkMdxDisableExplicitJsx,
_17
{ whiteList: ['table', 'thead', 'tbody', 'tr', 'th', 'td'] }
_17
]
_17
]
_17
}
_17
})
_17
_17
export default withNextra()