Skip to content

HTML cell

Notebooks

Observable’s HTML cell allows you to write your own custom HTML in notebooks.

html
<p>Hello, HTML cell!</p>

Just like text cells, you can embed variables in your HTML cell using string interpolation ${}:

js
myVariable = new Date()
html
<p>Today is ${myDate}</p>

HTML cells function as you would expect: write valid HTML and see the output above the cell. The only caveat is when you use the <style> tag. Given Observable is a website like any other, writing an unscoped styling will affect tags on the entire page. It’s best practice to scope your styling to the contents of the HTML cell to return expected results:

html
<style> 
#parent-container p {
  text-decoration: underline;
}
</style>

<div id='parent-container'>
    <p> Hello, scoped HTML styling!</p>
</div>

Since SVG is a supported HTML tag, you can use the HTML cell to write SVG code.

html
<svg width=100 height=100 style='border: 1px solid #000;'>
    <circle cx=50 cy=50 r=25 style='fill: steelblue'></circle>
</svg>
A screenshot of the output of an HTML cell with SVG code. It shows a steelblue circle within an SVG with a black stroke
The output of an HTML cell with SVG code.

Note

HTML cells by default are unpinned, which means when you navigate to another cell, the code for your HTML cell will close.

It is possible to write HTML using Javascript with Hypertext Literal. This can be advantageous when working with data, like so:

js
myData = [1, 2, 3, 4]
html
${myData.map(d => htl.html`<p>Paragraph ${d}</p>`)}
A screenshot of the output of an HTML cell using JavaScript and Hypertext Literal, showing four paragraph tags based off a JavaScript array with four values.
The output of an HTML cell using JavaScript and Hypertext Literal.

Hypertext Literal is part of our standard library, and can be called by using htl.