Markdown: Using HTML Inside Markdown

Markdown isn't all-powerful — when the syntax falls short, just write HTML directly.

1. What You'll Learn


2. A Frontend Developer's Real Story

(1) Pain Point: Markdown's limits block requirements

Lisa was writing technical docs for her company and needed to show colored status labels (like red "Failed" and green "Passed") inside table cells. Markdown tables don't support background colors or text colors. She tried all sorts of Markdown workarounds, wasted two hours, and eventually had to screenshot the labels and paste the image — but images aren't searchable.

(2) Solution: Write HTML directly in Markdown

Senior engineer Tom told her: "You can write HTML inside Markdown." Lisa used <span> tags with the style attribute to create colored labels. No screenshots needed, the text remains searchable, and it only adds a few lines of HTML.

MARKDOWN
| Test Case | Status |
|:----------|:------|
| User Login | <span style="color: green;">✅ Passed</span> |
| Payment API | <span style="color: red;">❌ Failed</span> |

3. HTML Rules in Markdown

Markdown was designed as "an easier way to write HTML." As a result, it natively supports embedding HTML in documents:

100%
graph TB
    A[Markdown Document] --> B[Markdown Syntax]
    A --> C[HTML Syntax]
    B --> D[Headings / Lists / Tables]
    C --> E[Block-level HTML]
    C --> F[Inline HTML]
    E --> G[div / table / pre]
    F --> G[span / img / br]
HTML Type Characteristics Example
Inline HTML Written directly inside a Markdown paragraph <span style="color:red">text</span>
Block-level HTML Standalone block, surrounded by blank lines <div>content</div>
Markdown inside blocks Markdown inside block-level HTML may not render <div>**bold** might not work</div>

(1) Inline HTML

HTML
This is a paragraph with <span style="color: red;">red text</span> and
<strong>bold text</strong> (using HTML tags).

Press Ctrl + <br> to break line (<br> is an HTML tag).

▶ Example: Using HTML to achieve what Markdown can't

HTML
This is text in regular Markdown.

<kbd>Ctrl</kbd> + <kbd>S</kbd> to save file.

Upgrade to <abbr title="Version 3.0">v3.0</abbr> release.
▶ Try it Yourself
⚠️ Note: Inside block-level HTML tags, standard Markdown syntax (like **bold**) is usually not parsed. Use HTML tags directly: <strong>bold</strong>.

(3) Common Block-Level HTML Uses

HTML
<!-- Custom container -->
<div style="border: 1px solid #ddd; padding: 16px; border-radius: 8px;">
  <h3>Important Update</h3>
  <p>Scheduled maintenance this weekend.</p>
</div>

<!-- Multi-column layout -->
<div style="display: flex; gap: 16px;">
  <div style="flex: 1;">Left column</div>
  <div style="flex: 1;">Right column</div>
</div>

<!-- Styled table -->
<table>
  <tr>
    <th style="background: #4CAF50; color: white;">Name</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Product A</td>
    <td>$29</td>
  </tr>
</table>

5. Common HTML-Fills-the-Gap Scenarios

Scenario Markdown Limit HTML Solution
Text color ❌ Not supported <span style="color:red">text</span>
Image dimensions ❌ Not supported <img src="url" width="200">
Open link in new tab ❌ Not supported <a href="url" target="_blank">text</a>
Merged table cells ❌ Not supported <td colspan="2">merged</td>
Custom styling ❌ Not supported <div style="...">content</div>
Line break inside table ❌ Not supported <br> tag

▶ Example: HTML doing what Markdown can't

HTML
<!-- Open in new tab -->
<a href="https://example.com" target="_blank">Open in new window</a>

<!-- Custom image size -->
<img src="logo.png" width="150" alt="Logo" style="border-radius: 8px;">

<!-- Keystroke styling -->
<kbd>Enter</kbd> or <kbd>Ctrl</kbd> + <kbd>V</kbd>

<!-- Callout with background color -->
<blockquote style="background: #fff3cd; border-left-color: #ffc107;">
  This is a custom-styled callout box.
</blockquote>
▶ Try it Yourself
💡 Tip: These are all things Markdown itself cannot do. Judicious use of HTML makes your documents more professional. But don't overdo it — 80% of content is fine with standard Markdown.


6. The Boundary Between HTML and Markdown

(1) Markdown inside block-level HTML usually won't parse

HTML
<div>
  **This text will not be bold** (Markdown syntax fails inside div)
  <strong>This text is bold with HTML</strong>
</div>

Exception: Some parsers (like Pandoc) support Markdown parsing inside HTML tags, but GFM (GitHub) does not. To be safe, use HTML syntax throughout block-level HTML tags.

(2) Markdown inside inline HTML

HTML
<span style="color: red;">**This text may render bold in some parsers**</span>
⚠️ Note: Parser behavior varies. Recommendation: use HTML syntax consistently inside HTML tags — don't mix with Markdown.

▶ Example: Safe vs unsafe mixing

MARKDOWN
✅ Safe:
- Write Markdown for body text: **bold**
- Use HTML for custom needs: <span style="color: red;">red</span>

❌ Unsafe:
<div style="padding: 8px;">
  **This bold won't work on GitHub**
</div>

7. Security and Compatibility

(1) Don't do this

HTML
❌ Unsafe: <script>alert('XSS')</script>
❌ Unsafe: <img src="x" onerror="alert('attack')">
❌ Unsafe: <iframe src="https://malicious-site.com"></iframe>
⚠️ Note: Platforms like GitHub automatically filter XSS attack code and won't execute <script> and event handlers. However, when exporting Markdown source to other platforms, avoid embedding unsafe HTML.

(2) HTML compatibility checklist

HTML
<!-- ✅ Cross-platform compatible -->
<strong>bold</strong>
<em>italic</em>
<kbd>keystroke</kbd>
<br>
<hr>

<!-- ⚠️ Some platforms don't support -->
<details><summary>Collapsible content</summary>Hidden text</details>
<mark>highlighted text</mark>
💡 Tip: If your Markdown needs to move across platforms (GitHub, GitLab, local preview, blog), minimize HTML usage. More HTML means more compatibility risk.


8. Complete Example: An HTML-Enhanced Markdown Document

TEXT 📖 Display only
HTML-enhanced Markdown document preview:

Product Changelog v3.2:
- Green-styled callout box: summary of updates
- HTML table: module / status / owner (with colored status)
- Keystroke tags: F5 to refresh
- Bash code block: installation command npm install my-app@latest
- Email link: support@example.com (mailto protocol)

Expected result: A product log combining Markdown and HTML — Markdown handles standard structure, HTML handles colors, keystroke styling, and custom containers.


❓ FAQ

Q Is using HTML in Markdown a good practice?
A Use it in moderation. 80% of content works with standard Markdown. Only use HTML for things Markdown can't do (colors, dimensions, new tab links). More HTML = less portability.
Q Which HTML tags does GitHub support?
A GitHub supports most safe inline and block-level HTML tags, but filters out <script>, <iframe>, and other unsafe tags and event handlers (like onclick).
Q Why doesn't Markdown syntax work inside HTML tags?
A Because Markdown parsers skip internal Markdown parsing when processing HTML blocks. This is by spec. The fix: use HTML syntax throughout HTML blocks.
Q Do block-level HTML tags require surrounding blank lines?
A Yes, they must. Without blank lines, block-level HTML may render incorrectly — the parser may fail to identify the HTML block boundaries.
Q Can I use CSS class names in HTML?
A Yes, but class names only take effect if your target platform has matching CSS rules. On GitHub, custom class names do nothing. On your own website, you can define your own styles.

📖 Summary


📝 Exercises

  1. Beginner: Write a Markdown paragraph where you use <span> to color one word red, and use <kbd> to display the "Ctrl+S" shortcut.

  2. Intermediate: Create a custom-styled callout box (using <div> with background color and border) containing a paragraph and a link. Compare how it renders in VS Code vs. GitHub.

  3. Challenge: Build an HTML table (with <thead> and <tbody>) that replaces a Markdown table, with a styled header row and a merged cell in the first row. Put both the HTML table and a Markdown table in the same document and compare their rendering.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏