基础语法

原始设计文档中概述的 Markdown 元素。

概述

几乎所有 Markdown 应用程序都支持原始 Markdown 设计文档中概述的基本语法。Markdown 处理器之间存在细微的差异和差异——尽可能内联。

标题

要创建标题, 请在词组或短语前添加井号 (#) 。 你使用的井号的数量应与标题级别相对应。例如,要创建三级标题 (<h3>),请使用三个井号(例如, ### 我的标题)。

Markdown HTML 渲染输出
# 一级标题 <h1>一级标题/h1>

一级标题

## 二级标题 <h2>二级标题</h2>

二级标题

### 三级标题 <h3>三级标题</h3>

三级标题

#### 四级标题 <h4>四级标题</h4>

四级标题

##### 五级标题 <h5>五级标题</h5>
五级标题
###### 六级标题 <h6>六级标题</h6>
六级标题

替代语法

或者,在文本下方的行中,为一级标题添加任意数量的 == 或为二级标题添加任意数量的 --

Markdown HTML 渲染输出
一级标题
===============
<h1>一级标题</h1>

一级标题

二级标题
---------------
<h2>二级标题</h2>

二级标题

标题的最佳实践

Markdown 应用程序如何处理井号(#)和标题名称之间的缺失空格上没有统一标准。为了兼容性,请始终在数字符号和标题名称之间放置一个空格。

✅  正确做法 ❌  错误做法
# 这是标题

#这是标题

为了兼容性,你还应该在标题前后放置空行。

✅  正确做法 ❌  错误做法
尝试在前面放一个空行...

# 标题

...标题之后也放一个空行。
如果没有空行,这可能看起来不正确。
# 标题
不要这样做!

段落

要创建段落,请使用空行分隔一行或多行文本。

Markdown HTML 渲染输出
我真的很喜欢使用 Markdown。

我想从现在开始我会用它来格式化我的所有文档。
<p>我真的很喜欢使用 Markdown。</p>

<p>我想从现在开始我会用它来格式化我的所有文档。</p>

我真的很喜欢使用 Markdown。

我想从现在开始我会用它来格式化我的所有文档。

段落的最佳实践

除非段落在列表中,否则不要使用空格或制表符缩进段落。

注意: 如果你需要在输出中缩进段落,请参阅如何 缩进(制表符)部分。
✅  正确做法 ❌  错误做法
不要在段落前面放置制表符或空格。

像这样保持行左对齐。

    这可能会导致意外的 格式问题。

  不要在段落前面添加制表符或空格。

换行

要换行或创建换行符 (<br>), 请以两个或多个空格结束一行,然后按回车键。

Markdown HTML 渲染输出
这是第一行。
这是第二行。
<p>这是第一行。<br>
这是第二行。</p>

这是第一行。
这是第二行。

换行的最佳实践

你几乎可以在每个 Markdown 应用程序中使用两个或多个空格(通常称为“尾随空格”)作为换行符,但这是有争议的。在编辑器中很难看到尾随空格,而且许多人在每个句子后不小心或有意地放置了两个空格。出于这个原因,你可能希望使用尾随空格以外的其他内容作为换行符。如果你的 Markdown 应用程序支持 HTML,你可以使用<br> HTML 标签。

为了兼容性,, 请在行尾使用尾随空格或<br> HTML 标签。

这里有两个我不建议使用的选项。CommonMark 和其他一些轻量级标记语言允许你在行尾键入反斜杠(\),但并非所有 Markdown 应用程序都支持这一点,因此从兼容性角度来看,这不是一个很好的选择。也有一些轻量级标记语言不需要在行尾添加任何内容——只需按回车键,它们就会创建一个换行符。

✅  正确做法 ❌  错误做法
第一行后面有两个空格。  
第二行。

第一行后面有两个空格。<br>
第二行。

第一行后面有两个空格。\
第二行。

第一行后面有两个空格。
第二行。

强调

你可以通过将文本设置为粗体或斜体来增加重点。

粗体

要加粗文本,请在单词或短语前后添加两个星号或下划线。为了强调单词的中间部分,请在字母周围添加两个不带空格的星号。

Markdown HTML 渲染输出
我只是喜欢**粗体字**。 我只是喜欢 <strong>粗体字</strong>。 I just love bold text.
我只是喜欢 __粗体字__。 我只是喜欢 <strong>粗体字</strong>。 我只是喜欢 粗体字
Love**is**bold 爱<strong>是</strong>大胆的 大胆的

粗体的最佳实践

Markdown 应用程序在如何处理单词中间的下划线上没有统一标准。为了兼容性,使用星号在单词中间加粗以表示强调。

✅  正确做法 ❌  错误做法
爱**是**大胆的 爱__是__大胆的

斜体

要使文本变为斜体,请在单词或短语前后添加一个星号或下划线。为了强调单词中间的斜体,请在字母周围添加一个不带空格的星号。

Markdown HTML 渲染输出
Italicized text is the *cat's meow*. Italicized text is the <em>cat's meow</em>. Italicized text is the cat’s meow.
Italicized text is the _cat's meow_. Italicized text is the <em>cat's meow</em>. Italicized text is the cat’s meow.
A*cat*meow A<em>cat</em>meow Acatmeow

斜体的最佳实践

Markdown 应用程序在如何处理单词中间的下划线方面没有达成一致。为了兼容性,使用星号将单词中间斜体表示强调。

✅  正确做法 ❌  错误做法
A*cat*meow A_cat_meow

粗体和斜体

要同时强调粗体和斜体文本,请在单词或短语前后添加三个星号或下划线。要在单词中间加粗和斜体以表示强调,请在字母周围添加三个不带空格的星号。

Markdown HTML 渲染输出
这段文字***真的很重要***。 这段文字<em><strong>真的很重要</strong></em>。 这段文字真的很重要
这段文字 ___真的很重要___。 这段文字<em><strong>真的很重要</strong></em>。 这段文字真的很重要
这段文字 __*真的很重要*__。 这段文字 <em><strong>真的很重要</strong></em>。 这段文字真的很重要
这段文字 **_真的很重要_**。 这段文字 <em><strong>真的很重要</strong></em>。 这段文字真的很重要
这是***非常***重要的文本。 这是<em><strong>非常</strong></em>重要的文本。 这是非常重要的文本。
注意:emstrong 标签的顺序可能会根据你使用的 Markdown 处理器而颠倒。

粗体和斜体的最佳实践

Markdown 应用程序不同意如何处理单词中间的下划线。为了兼容性,使用星号在单词中间加粗和斜体表示强调。

✅  正确做法 ❌  错误做法
这是***非常***重要的文本。 这是___非常___重要的文本。

块引用

要创建块引用,请在段落前添加一个 > 符号。

> Dorothy followed her through many of the beautiful rooms in her castle.

渲染输出如下所示:

Dorothy followed her through many of the beautiful rooms in her castle.

带有多个段落的块引用

块引用可以包含多个段落。在段落之间的空白行上添加一个 > 符号。

> Dorothy followed her through many of the beautiful rooms in her castle.
>
> The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

渲染输出如下所示:

Dorothy followed her through many of the beautiful rooms in her castle.

The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

嵌套块引用

块引用可以嵌套。在要嵌套的段落前面添加一个 >> 符号。

> Dorothy followed her through many of the beautiful rooms in her castle.
>
>> The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

渲染输出如下所示:

Dorothy followed her through many of the beautiful rooms in her castle.

The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

带有其他元素的块引用

块引用可以包含其他 Markdown 格式的元素。并非所有元素都可以使用——你需要尝试看看哪些元素有效。

> #### The quarterly results look great!
>
> - Revenue was off the chart.
> - Profits were higher than ever.
>
>  *Everything* is going according to **plan**.

渲染输出如下所示:

The quarterly results look great!

  • Revenue was off the chart.
  • Profits were higher than ever.

Everything is going according to plan.

块引用的最佳实践

For compatibility, put blank lines before and after blockquotes.

✅  正确做法 ❌  错误做法
尝试在前面放一个空行...

> 这是一个块引用

...在一个块引用之后。
如果没有空行,这可能看起来不正确。
> 这是一个块引用
不要这样做!

列表

You can organize items into ordered and unordered lists.

有序列表

To create an ordered list, add line items with numbers followed by periods. The numbers don’t have to be in numerical order, but the list should start with the number one.

Markdown HTML 渲染输出
1. First item
2. Second item
3. Third item
4. Fourth item
<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ol>
  1. First item
  2. Second item
  3. Third item
  4. Fourth item
1. First item
1. Second item
1. Third item
1. Fourth item
<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ol>
  1. First item
  2. Second item
  3. Third item
  4. Fourth item
1. First item
8. Second item
3. Third item
5. Fourth item
<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ol>
  1. First item
  2. Second item
  3. Third item
  4. Fourth item
1. First item
2. Second item
3. Third item
    1. Indented item
    2. Indented item
4. Fourth item
<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item
    <ol>
      <li>Indented item</li>
      <li>Indented item</li>
    </ol>
  </li>
  <li>Fourth item</li>
</ol>
  1. First item
  2. Second item
  3. Third item
    1. Indented item
    2. Indented item
  4. Fourth item

Ordered List Best Practices

CommonMark and a few other lightweight markup languages let you use a parenthesis ()) as a delimiter (e.g., 1) First item), but not all Markdown applications support this, so it isn’t a great option from a compatibility perspective. For compatibility, use periods only.

✅  Do this ❌  Don't do this
1. First item
2. Second item
1) First item
2) Second item

无序列表

To create an unordered list, add dashes (-), asterisks (*), or plus signs (+) in front of line items. Indent one or more items to create a nested list.

Markdown HTML 渲染输出
- First item
- Second item
- Third item
- Fourth item
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
  • Fourth item
* First item
* Second item
* Third item
* Fourth item
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
  • Fourth item
+ First item
+ Second item
+ Third item
+ Fourth item
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
  • Fourth item
- First item
- Second item
- Third item
    - Indented item
    - Indented item
- Fourth item
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item
    <ul>
      <li>Indented item</li>
      <li>Indented item</li>
    </ul>
  </li>
  <li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
    • Indented item
    • Indented item
  • Fourth item

Starting Unordered List Items With Numbers

If you need to start an unordered list item with a number followed by a period, you can use a backslash (\) to escape the period.

Markdown HTML 渲染输出
- 1968\. A great year!
- I think 1969 was second best.
<ul>
  <li>1968. A great year!</li>
  <li>I think 1969 was second best.</li>
</ul>
  • 1968. A great year!
  • I think 1969 was second best.

Unordered List Best Practices

Markdown applications don’t agree on how to handle different delimiters in the same list. For compatibility, don’t mix and match delimiters in the same list — pick one and stick with it.

✅  Do this ❌  Don't do this
- First item
- Second item
- Third item
- Fourth item
+ First item
* Second item
- Third item
+ Fourth item

Adding Elements in Lists

To add another element in a list while preserving the continuity of the list, indent the element four spaces or one tab, as shown in the following examples.

Tip: If things don't appear the way you expect, double check that you've indented the elements in the list four spaces or one tab.

Paragraphs

* This is the first list item.
* Here's the second list item.

    I need to add another paragraph below the second list item.

* And here's the third list item.

The 渲染输出 looks like this:

  • This is the first list item.
  • Here’s the second list item.

    I need to add another paragraph below the second list item.

  • And here’s the third list item.

Blockquotes

* This is the first list item.
* Here's the second list item.

    > A blockquote would look great below the second list item.

* And here's the third list item.

The 渲染输出 looks like this:

  • This is the first list item.
  • Here’s the second list item.

    A blockquote would look great below the second list item.

  • And here’s the third list item.

Code Blocks

Code blocks are normally indented four spaces or one tab. When they’re in a list, indent them eight spaces or two tabs.

1. Open the file.
2. Find the following code block on line 21:

        <html>
          <head>
            <title>Test</title>
          </head>

3. Update the title to match the name of your website.

The 渲染输出 looks like this:

  1. Open the file.
  2. Find the following code block on line 21:

     <html>
       <head>
         <title>Test</title>
       </head>
    
  3. Update the title to match the name of your website.

Images

1. Open the file containing the Linux mascot.
2. Marvel at its beauty.

    ![Tux, the Linux mascot](/assets/images/tux.png)

3. Close the file.

The 渲染输出 looks like this:

  1. Open the file containing the Linux mascot.
  2. Marvel at its beauty.

    Tux, the Linux mascot

  3. Close the file.

Lists

You can nest an unordered list in an ordered list, or vice versa.

1. First item
2. Second item
3. Third item
    - Indented item
    - Indented item
4. Fourth item

The 渲染输出 looks like this:

  1. First item
  2. Second item
  3. Third item
    • Indented item
    • Indented item
  4. Fourth item

代码

To denote a word or phrase as code, enclose it in backticks (`).

Markdown HTML 渲染输出
At the command prompt, type `nano`. At the command prompt, type <code>nano</code>. At the command prompt, type nano.

Escaping Backticks

If the word or phrase you want to denote as code includes one or more backticks, you can escape it by enclosing the word or phrase in double backticks (``).

Markdown HTML 渲染输出
``Use `code` in your Markdown file.`` <code>Use `code` in your Markdown file.</code> Use `code` in your Markdown file.

代码块

To create code blocks, indent every line of the block by at least four spaces or one tab.

    <html>
      <head>
      </head>
    </html>

The 渲染输出 looks like this:

<html>
  <head>
  </head>
</html>
Note: To create code blocks without indenting lines, use fenced code blocks.

水平分隔线

To create a horizontal rule, use three or more asterisks (***), dashes (---), or underscores (___) on a line by themselves.

***

---

_________________

The 渲染输出 of all three looks identical:


Horizontal Rule Best Practices

For compatibility, put blank lines before and after horizontal rules.

✅  Do this ❌  Don't do this
Try to put a blank line before...

---

...and after a horizontal rule.
Without blank lines, this would be a heading.
---
Don't do this!

链接

To create a link, enclose the link text in brackets (e.g., [Duck Duck Go]) and then follow it immediately with the URL in parentheses (e.g., (https://duckduckgo.com)).

My favorite search engine is [Duck Duck Go](https://duckduckgo.com).

The 渲染输出 looks like this:

My favorite search engine is Duck Duck Go.

Note: To link to an element on the same page, see linking to heading IDs. To create a link that opens in a new tab or window, see the section on link targets.

Adding Titles

You can optionally add a title for a link. This will appear as a tooltip when the user hovers over the link. To add a title, enclose it in quotation marks after the URL.

My favorite search engine is [Duck Duck Go](https://duckduckgo.com "The best search engine for privacy").

The 渲染输出 looks like this:

My favorite search engine is Duck Duck Go.

URLs and Email Addresses

To quickly turn a URL or email address into a link, enclose it in angle brackets.

<https://www.markdownguide.org>
<fake@example.com>

The 渲染输出 looks like this:

https://www.markdownguide.org
fake@example.com

To emphasize links, add asterisks before and after the brackets and parentheses. To denote links as code, add backticks in the brackets.

I love supporting the **[EFF](https://eff.org)**.
This is the *[Markdown Guide](https://www.markdownguide.org)*.
See the section on [`code`](#code).

The 渲染输出 looks like this:

I love supporting the EFF.
This is the Markdown Guide.
See the section on code.

Reference-style links are a special kind of link that make URLs easier to display and read in Markdown. Reference-style links are constructed in two parts: the part you keep inline with your text and the part you store somewhere else in the file to keep the text easy to read.

The first part of a reference-style link is formatted with two sets of brackets. The first set of brackets surrounds the text that should appear linked. The second set of brackets displays a label used to point to the link you’re storing elsewhere in your document.

Although not required, you can include a space between the first and second set of brackets. The label in the second set of brackets is not case sensitive and can include letters, numbers, spaces, or punctuation.

This means the following example formats are roughly equivalent for the first part of the link:

  • [hobbit-hole][1]
  • [hobbit-hole] [1]

The second part of a reference-style link is formatted with the following attributes:

  1. The label, in brackets, followed immediately by a colon and at least one space (e.g., [label]: ).
  2. The URL for the link, which you can optionally enclose in angle brackets.
  3. The optional title for the link, which you can enclose in double quotes, single quotes, or parentheses.

This means the following example formats are all roughly equivalent for the second part of the link:

  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle "Hobbit lifestyles"
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle 'Hobbit lifestyles'
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle (Hobbit lifestyles)
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> "Hobbit lifestyles"
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> 'Hobbit lifestyles'
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> (Hobbit lifestyles)

You can place this second part of the link anywhere in your Markdown document. Some people place them immediately after the paragraph in which they appear while other people place them at the end of the document (like endnotes or footnotes).

An Example Putting the Parts Together

Say you add a URL as a standard URL link to a paragraph and it looks like this in Markdown:

In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends
of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to
eat: it was a [hobbit-hole](https://en.wikipedia.org/wiki/Hobbit#Lifestyle "Hobbit lifestyles"), and that means comfort.

Though it may point to interesting additional information, the URL as displayed really doesn’t add much to the existing raw text other than making it harder to read. To fix that, you could format the URL like this instead:

In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends
of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to
eat: it was a [hobbit-hole][1], and that means comfort.

[1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> "Hobbit lifestyles"

In both instances above, the 渲染输出 would be identical:

In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort.

and the HTML for the link would be:

<a href="https://en.wikipedia.org/wiki/Hobbit#Lifestyle" title="Hobbit lifestyles">hobbit-hole</a>

Markdown applications don’t agree on how to handle spaces in the middle of a URL. For compatibility, try to URL encode any spaces with %20. Alternatively, if your Markdown application supports HTML, you could use the a HTML tag.

✅  Do this ❌  Don't do this
[link](https://www.example.com/my%20great%20page)

<a href="https://www.example.com/my great page">link</a>
[link](https://www.example.com/my great page)

Images

To add an image, add an exclamation mark (!), followed by alt text in brackets, and the path or URL to the image asset in parentheses. You can optionally add a title in quotation marks after the path or URL.

![The San Juan Mountains are beautiful!](/assets/images/san-juan-mountains.jpg "San Juan Mountains")

The 渲染输出 looks like this:

The San Juan Mountains are beautiful!

Note: To resize an image, see the section on image size. To add a caption, see the section on image captions.

Linking Images

To add a link to an image, enclose the Markdown for the image in brackets, and then add the link in parentheses.

[![An old rock in the desert](/assets/images/shiprock.jpg "Shiprock, New Mexico by Beau Rogers")](https://www.flickr.com/photos/beaurogers/31833779864/in/photolist-Qv3rFw-34mt9F-a9Cmfy-5Ha3Zi-9msKdv-o3hgjr-hWpUte-4WMsJ1-KUQ8N-deshUb-vssBD-6CQci6-8AFCiD-zsJWT-nNfsgB-dPDwZJ-bn9JGn-5HtSXY-6CUhAL-a4UTXB-ugPum-KUPSo-fBLNm-6CUmpy-4WMsc9-8a7D3T-83KJev-6CQ2bK-nNusHJ-a78rQH-nw3NvT-7aq2qf-8wwBso-3nNceh-ugSKP-4mh4kh-bbeeqH-a7biME-q3PtTf-brFpgb-cg38zw-bXMZc-nJPELD-f58Lmo-bXMYG-bz8AAi-bxNtNT-bXMYi-bXMY6-bXMYv)

The 渲染输出 looks like this:

An old rock in the desert

Escaping Characters

To display a literal character that would otherwise be used to format text in a Markdown document, add a backslash (\) in front of the character.

\* Without the backslash, this would be a bullet in an unordered list.

The 渲染输出 looks like this:

* Without the backslash, this would be a bullet in an unordered list.

Characters You Can Escape

You can use a backslash to escape the following characters.

Character Name
\ backslash
` backtick (see also escaping backticks in code)
* asterisk
_ underscore
{ } curly braces
[ ] brackets
< > angle brackets
( ) parentheses
# pound sign
+ plus sign
- minus sign (hyphen)
. dot
! exclamation mark
| pipe (see also escaping pipe in tables)

HTML

Many Markdown applications allow you to use HTML tags in Markdown-formatted text. This is helpful if you prefer certain HTML tags to Markdown syntax. For example, some people find it easier to use HTML tags for images. Using HTML is also helpful when you need to change the attributes of an element, like specifying the color of text or changing the width of an image.

To use HTML, place the tags in the text of your Markdown-formatted file.

This **word** is bold. This <em>word</em> is italic.

The 渲染输出 looks like this:

This word is bold. This word is italic.

HTML Best Practices

For security reasons, not all Markdown applications support HTML in Markdown documents. When in doubt, check your Markdown application’s documentation. Some applications support only a subset of HTML tags.

Use blank lines to separate block-level HTML elements like <div>, <table>, <pre>, and <p> from the surrounding content. Try not to indent the tags with tabs or spaces — that can interfere with the formatting.

You can’t use Markdown syntax inside block-level HTML tags. For example, <p>italic and **bold**</p> won’t work.