HTML in Markdown: What Works and What Gets Stripped

Written by Last updated

Yes, you can write HTML inside a Markdown file. Most renderers, including GitHub, GitLab and Obsidian, pass it through. Use it only for what Markdown cannot do: centering, collapsible sections, merged table cells, keyboard keys and light/dark images.

Two things decide whether it works. The renderer may strip tags it considers unsafe, and Markdown written inside an HTML block is often not processed. Both are covered below, with the fix for each.

🟒 Practice as you read: Try every example in the Markdown Editor with live preview, or keep the Markdown Cheat Sheet open for quick syntax reference.

Open EditorDownload PDF

What to use HTML for

I want to... Use this
Center text or an image <p align="center">...</p>
Set an image size <img src="logo.png" alt="Logo" width="300">
Hide content until clicked <details><summary>...</summary>...</details>
Show a key press <kbd>Ctrl</kbd>
Write x squared or H2O x<sup>2</sup>, H<sub>2</sub>O
Merge table cells HTML <table> with rowspan or colspan
Swap an image in dark mode <picture> with prefers-color-scheme
Force a line break <br> (see the line breaks guide)

Everything else (headings, lists, links, bold, code, normal tables) is shorter and more portable in plain Markdown, so keep it there.

The blank-line rule (read this first)

This is the most common reason HTML and Markdown do not mix. In CommonMark, a block tag such as <div> or <details> starts an HTML block that ends at the next blank line. Markdown inside it is treated as raw text.

Does not work, the ** shows up literally:

<details>
<summary>Steps</summary>
**Run this:** `npm install`
</details>

Works, with a blank line after the opening tag block and before the closing tag:

<details>
<summary>Steps</summary>

**Run this:** `npm install`

</details>

The opposite also matters: do not put blank lines inside an HTML snippet that has no Markdown in it. A blank line ends the HTML block, and the rest of the snippet can fall apart. Keep the centering example below as a single block.

Center text or an image

Markdown has no centering syntax. On GitHub, the align attribute is the usual way, even though HTML5 marks it as deprecated.

<p align="center">
  <img src="logo.png" alt="Project logo" width="160">
</p>

For image syntax, resizing and captions, see the image syntax guide and adding images step by step.

Collapsible sections

<details>
<summary>Troubleshooting</summary>

Steps and **Markdown** go here, after the blank line.

</details>

Add open to the tag (<details open>) to show it expanded by default. This works well for FAQs, long logs and optional steps.

Merged table cells

Markdown tables cannot merge cells. Use an HTML table when you need rowspan or colspan.

<table>
  <tr>
    <th rowspan="2">Feature</th>
    <th colspan="2">Plan</th>
  </tr>
  <tr>
    <th>Basic</th>
    <th>Pro</th>
  </tr>
  <tr>
    <td>Exports</td>
    <td>3</td>
    <td>Unlimited</td>
  </tr>
</table>

If you do not need merged cells, use a normal Markdown table. See the tables guide.

Light and dark mode images on GitHub

GitHub's own blog documents this pattern. The browser picks the image that matches the reader's color scheme, and <img> is the fallback.

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="logo-dark.png">
  <source media="(prefers-color-scheme: light)" srcset="logo-light.png">
  <img alt="Project logo" src="logo-light.png">
</picture>

Always keep the alt text on the fallback <img>.

Small tags worth knowing

Tag Use Example
<kbd> Keyboard keys Press <kbd>Ctrl</kbd> + <kbd>S</kbd>
<sup>, <sub> Superscript, subscript x<sup>2</sup>, H<sub>2</sub>O
<br> Line break, including inside table cells Line one<br>Line two

What gets stripped

Renderers sanitize HTML to prevent script injection. Expect these to be removed or ignored on most platforms:

  • <script>, <iframe>, <embed>, <object> and <form>
  • Event handlers such as onclick and onerror
  • style attributes and <style> blocks
  • Link attributes such as target="_blank"

If something vanishes, the tag is usually being sanitized, not mistyped. Use a plain Markdown alternative or an allowed tag instead of styling.

Platform What to know
GitHub Allows common tags (<details>, <img>, <picture>, <table>, <kbd>, <sup>, <sub>). Removes scripts, iframes and unsafe attributes.
GitLab Sanitizes HTML. Common formatting tags work.
Obsidian Renders common HTML in notes.
Hugo Raw HTML is off by default in recent versions and must be enabled in the Markdown renderer config (goldmark unsafe setting).
Docusaurus (MDX) Tags must be closed (<br />), and <!-- --> comments can break the build.
MkDocs Generally passes HTML through.

Platforms differ and change their rules, so check the exact tags you rely on against GitHub's sanitization rules or your site's docs, and preview on the platform where the file will be published.

HTML in Markdown not working?

What you see Likely cause Fix
**bold** or a code fence shows as plain text inside <div> or <details> No blank line after the opening HTML Add a blank line before the Markdown content and before the closing tag.
Centered image is not centered Blank lines inside the <p align="center"> block Keep the <p> and <img> together with no blank lines.
Tag does not appear at all Platform sanitized it Use an allowed tag, or a Markdown equivalent.
Everything after one tag is formatted wrong Unclosed tag Close every tag, and use <br /> style in MDX.
Styling or colors are ignored style attributes are removed Do not rely on CSS in Markdown.
You want to show a tag as text, not render it Renderer treats it as HTML Put it in backticks, or write &lt;div&gt;.
Works on GitHub, breaks on your docs site Different renderer settings Check the platform table above.

Frequently Asked Questions

  1. 1

    Can I use HTML in Markdown?

    Yes. Most Markdown renderers allow raw HTML and pass it through. Some strip unsafe tags, and some, like Hugo, have it switched off until you enable it. Preview your file on the platform where it will be published.

  2. 2

    How do I center an image in a GitHub README?

    Markdown cannot center anything. Wrap an <img> in <p align="center"> and keep the whole snippet without blank lines. The align attribute is deprecated in HTML5 but still works on GitHub.

  3. 3

    Why isn't my Markdown working inside a <div> or <details>?

    The HTML block runs until the next blank line, and Markdown inside it is not parsed. Add a blank line after the opening tag and before the closing tag, and the formatting will render.

  4. 4

    Why does GitHub remove my style attribute, iframe or script?

    GitHub sanitizes Markdown to block script injection, so scripts, iframes, event handlers and inline styles are dropped. Use allowed tags such as <details>, <img> and <table>, and link to embedded content instead of embedding it.

  5. 5

    Which HTML tags work in GitHub Markdown?

    Common ones work, including <details>, <summary>, <img>, <picture>, <table>, <kbd>, <sup>, <sub>, <br> and <p align>. Anything that can run code is removed.