Hiccup provides a concise syntax for representing HTML using Clojure data structures. This syntax is not only used by the Hiccup library itself, but also many other tools in the Clojure ecosystem.

Basic syntax

Vectors that begin with a keyword represent HTML elements. For example:




Any value placed after the keyword is treated as the body of the element. This allows elements to be nested. For example:

[:div [:span] [:span]]

Strings are rendered as plain text. So:

[:span "Hello world"]
<span>Hello world</span>

And adjacent strings are concatenated together.

[:span "foo" "bar"]

A map can be optionally specified as the second value in the vector. This map represents the attributes of the element. The keys should be keywords, and the values should be strings.

[:span {:class "foo"} "Hello world"]
<span class="foo">Hello world</span>

Syntax sugar

Beyond the basic syntax, Hiccup supplies additional syntax sugar that can make your code more concise.


Values of types that have no special meaning are converted into strings before being rendered. So for example:

[:span 42]

Is equivalent to:

[:span "42"]


Seqs (including lists) are automatically expanded out. This means that an structure like:

[:span '("foo" "bar")]

Is equivalent to:

[:span "foo" "bar"]

This is particularly useful for iteration. For example:

[:ul (for [i (range 3)] [:li i])]

CSS selectors

The # character can be used to concisely specify the id attribute. For example:

[:span#foo "Hello world"]
<span id="foo">Hello world</span>

While the . character can be used to specify the class attribute.

[ "Hello world"]
<span class="foo">Hello world</span>

These two syntaxes can be combined to produce elements with both an id and one or more classes.

[ "Hello world"]
<span id="foo" class="bar baz">Hello world</span>