docs.rodeo

MDN Web Docs mirror

Code examples on MDN

On MDN, you’ll see numerous code examples that demonstrate how to use web platform features that we document. This article describes the ways you can add code examples to pages, along with the types you can use and when to use them.

[!NOTE] This page describes how code gets included in MDN pages. If you want linting and style hints for adding code in an MDN page, see our Code style guide.

What types of code example are on MDN?

There are four types of code examples available:

When should you use each one?

Each type of code example has its own use cases:

General guidelines

There are style and content considerations to keep in mind when adding or updating samples on MDN.

Static examples

Static examples are code blocks that show how a feature looks like in source code. These are put on a page using Markdown “code fences”, as described in Example code blocks. When used in documentation pages, they look like this:

// This is a JS example
const test = "Hello";
console.log(test);

Interactive examples

The InteractiveExample macro is used to embed interactive examples at the top of MDN reference pages. They are for readers who want to try an example without having to read through the full article for a topic or feature.

[!WARNING] Currently only JavaScript examples are supported. See https://github.com/orgs/mdn/discussions/782 for implementation details.

The InteractiveExample macro accepts a title for the example as a string, followed by a keyword to specify the height of the example. The code blocks to include in the example appear after the macro call and contain the keyword interactive-example in the info string after the code block’s language. The JavaScript Array.concat() usage is a good example of this macro, which looks like this in the markdown source:

\`{{InteractiveExample("JavaScript Demo: Array.concat()", "shorter")}}` 

```javascript
const array1 = ["a", "b", "c"];
const array2 = ["d", "e", "f"];
const array3 = array1.concat(array2);

console.log(array3);
// Expected output: Array ["a", "b", "c", "d", "e", "f"]
```

There are a few limitations to interactive examples:

Live samples

Live samples are inserted into the page using the EmbedLiveSample macro. An \{{EmbedLiveSample}}  macro takes code blocks from a page, combines them into an {{htmlelement("iframe")}} , and inserts the result into the page. See the Live samples guide for more information.

GitHub live samples

GitHub live samples are embedded into the page using the EmbedGHLiveSample macro. An \{{EmbedGHLiveSample}}  takes the content at a specified URL (which must be an MDN GitHub repository), and inserts it into the page in an {{htmlelement("iframe")}} .

The macro has three parameters:

  1. The URL of the document to embed — this is relative to the MDN organization, the top level directory of which is at https://mdn.github.io/. So this parameter needs to contain the part of the URL after that, e.g. my-subdirectory/example.html. You can omit the filename if it is called index.html.
  2. The width of the <iframe>, which can be expressed as a percentage or in pixels.
  3. The height of the <iframe>, which can be expressed as a percentage or in pixels.

Let’s look at an example. Say we wanted to embed the code at https://mdn.github.io/learning-area/css/styling-boxes/backgrounds/. We could use the following call:

\{{EmbedGHLiveSample("learning-area/css/styling-boxes/backgrounds/", '100%', 100)}} 

This looks like so when rendered:

{{EmbedGHLiveSample("learning-area/css/styling-boxes/backgrounds/", '100%', 100)}} 

In this article

View on MDN