How to format code blocks?

Posting code is great when the question you have is very technical. By formatting code blocks properly you make it easier for other people to read your post and answer your question. The easier your question is to digest, the more likely you will get an answer.

You can use Markdown for any kind of formatting.

Particularly for code blocks, add three back-ticks (```) before and after the code snippet. Let’s look at an example:

Writing this:

```
public String getHelloWorld() {
  return “Hello World!”;
}
```

produces this formatted code block:

public String getHelloWorld() {
  return "Hello World!";
}
5 Likes

Additionally you can indicate which language the snippet.

For instance if you want Javascript use ```js

function jsFunction() {
  var val = 'awesome';
  return val + 'ness has a possee';
}

To highlight HTML, you need to use ```xml

<html>
  <head>
    <title>For real</title>
  </head>
  <body>
    ...
  </body>
</html>

CSS (SCSS or less too)

body {
  background-color: pink;
}

More about supported languages

3 Likes