Skip to main content

How do I format my posts using Markdown or HTML?

When posting on Stack Overflow, you can use the visual editor (for questions and answers, but not comments), Markdown, and some HTML. If you're new to Markdown, you may want to visit the official Markdown syntax reference page.

Here are some tips for common formatting scenarios; more details are available on our advanced editing help page.

Code Blocks

Indent four spaces to create an escaped <pre> <code> block:

    printf("%d\n", 42);  /* what was the
                        question again? */

The text will be wrapped in tags, and displayed in a monospaced font. The first four spaces will be stripped off, but all other whitespace will be preserved. You can select a block of code and press ctrl K to automatically add four spaces to the beginning of every selected line.

Alternatively, you can use fenced code blocks instead of indenting your code samples. Wrap your code in three or more backtick (```) or tilde (~~~) characters:

```
console.log("Hello, World");
```

Markdown and HTML are ignored within a code block:

    <blink>
      You would hate this if it weren't
      wrapped in a code block.
    </blink>

Code snippets

If you're including code that demonstrates HTML, CSS or JavaScript, you can embed a runnable snippet directly into the post. Click the snippet icon on the toolbar or press ctrl+M.

For a step-by-step walkthrough, see: I've been told to create a "runnable" example with "Stack Snippets". How do I do that?

Inline Code

Use backticks (in the upper left corner of most keyboards) to create an inline <code> span:

You can use a `<div>` for this with `display: inline-block;` set.

This will be displayed as:

You can use a <div> for this with display: inline-block; set.

Like code blocks, code spans will be displayed in a monospaced font. Markdown and HTML will not work within them. Note that, unlike code blocks, code spans require you to manually escape any HTML within them.

Syntax highlighting for code

Code blocks can be highlighted using highlight.js. In many cases, the syntax highlighting language will be inferred from the question's tags.

To manually specify the language of a code block, include the highlighting language in the fenced code block:

```lang-js
setTimeout(function () { alert("JavaScript"); }, 1000);
```

You cannot specify the language for a single code block when using indented code blocks.

You can use either one of the supported language codes, like lang-cpp or lang-sql, or you can specify a tag, and the syntax highlighting language associated with this tag will be used:

```c#
public static bool IsAwesome { get { return true; } }
```

To specify a syntax highlighting language to be used not only for the next, but for all following code blocks, use

<!-- language-all: lang-html -->

To specify that you don't want any syntax highlighting for a code block, use

<!-- language-all: lang-none -->

Linebreaks

End a line with two spaces to add a <br/> linebreak:

How do I love thee?  
Let me count the ways

Italics and Bold

*This is italicized*, and so is _this_.
**This is bold**, and so is __this__.
Use ***italics and bold together*** if you ___have to___.

Links

There are three ways to write links. Each is easier to read than the last:

Here's an inline link to [Google](http://www.google.com/).
Here's a reference-style link to [Google][1].
Here's a very readable link to [Yahoo!][yahoo].

  [1]: http://www.google.com/
  [yahoo]: http://www.yahoo.com/

The link definitions can appear anywhere in the document -- before or after the place where you use them. The link definition names [1] and [yahoo] can be any unique string, and are case-insensitive; [yahoo] is the same as [YAHOO].

Advanced Links

Links can have a title attribute, which will show up on hover. Title attributes can also be added; they are helpful if the link itself is not descriptive enough to tell users where they're going.

Here's a [poorly-named link](http://www.google.com/ "Google").
Never write "[click here][^2]".
Visit [us][web].

  [^2]: http://www.w3.org/QA/Tips/noClickHere
        (Advice against the phrase "click here")
  [web]: http://programmers.stackexchange.com/ "Programmers Stack Exchange"

You can also use standard HTML hyperlink syntax.

<a href="http://example.com" title="example">example</a>

Simple lists

A bulleted <ul> list:

- Use a minus sign for a bullet
+ Or plus sign
* Or an asterisk

A numbered <ol> list:

1. Numbered lists are easy
2. Markdown keeps track of the numbers for you
7. So this will be item 3.

A double-spaced list:

- This list gets wrapped in `<p>` tags

- So there will be extra space between items

Simple blockquotes

Add a > to the beginning of any line to create a <blockquote>.

> The syntax is based on the way email programs
> usually do quotations. You don't need to hard-wrap
> the paragraphs in your blockquotes, but it looks much nicer if you do.  Depends how lazy you feel.

Tags

To talk about a tag on this site, , use

See the many questions tagged [tag:elephants] to learn more.

The tag will automatically be linked to the corresponding tag info page.

Comment Formatting

Comments support only bold, italic, code and links; in addition, a few shorthand links are available.

_italic_ and **bold** text,
inline `code in backticks`,
and [basic links](http://example.com).

Supported shorthand links:

  • [meta] – link to the current site's Meta; link text is the site name (e.g. "Super User Meta"). Does nothing if the site doesn't have (or already is) a Meta site.

  • [edit] – link to the edit page for the post the comment is on, i.e. /posts/{id}/edit. Link text is "edit" (capitalization is respected).

  • [tag:tagname] and [meta-tag:tagname] – link to the given tag's page. Link text is the name of the tag. meta-tag only works on meta sites.

  • [about] – link to the About page. Link text is "about" (capitalization is respected).

  • [help] - link to the Help Center.

  • [chat] – link to the current site's chat site, the link text being "{site name} Chat".

For more shorthand links, see the expanded formatting guide.

Images

Images can be added primarily by using the the editor toolbar button insert image toolbar button. This brings up a special interface that allows you to upload an image online (through the imgur hosting service) through us - even from your clipboard. Alternatively, it can be input similarly to adding a link:

  • HTML <img src="http://example.com/img.jpg">
  • Markdown ![sample image](http://example.com/img.jpg)