Clear to write, read and edit DSL for HTML

Version on this page:2.5@rev:2
LTS Haskell 17.11:
Stackage Nightly 2021-05-15:
Latest on Hackage:

See all snapshots lucid appears in

BSD-3-Clause licensed by Chris Done
Maintained by
This version can be pinned in stack with:lucid-2.5@sha256:15e572eef336ec8891065a3333bcdf7a6e539a905f572aa8a054e2513cad2765,1782

Module documentation for 2.5

  • Lucid
    • Lucid.Base
    • Lucid.Bootstrap
    • Lucid.Html5

lucid Hackage

Clear to write, read and edit DSL for writing HTML


HTML terms in Lucid are written with a postfix ‘_’ to indicate data rather than code. Some examples:

p_, class_, table_, style_

See Lucid.Html5 for a complete list of Html5 combinators.

Plain text is written using the OverloadedStrings and ExtendedDefaultRules extensions, and is automatically escaped:

λ> "123 < 456" :: Html ()
123 &lt; 456

Elements nest by function application:

λ> table_ (tr_ (td_ (p_ "Hello, World!"))) :: Html ()
<table><tr><td><p>Hello, World!</p></td></tr></table>

Elements are juxtaposed via monoidal append:

λ> p_ "hello" <> p_ "sup" :: Html ()

Or monadic sequencing:

λ> div_ (do p_ "hello"; p_ "sup") :: Html ()

Attributes are set by providing an argument list:

λ> p_ [class_ "brand"] "Lucid Inc" :: Html ()
<p class="brand">Lucid Inc</p>

Here is a fuller example of Lucid:

table_ [rows_ "2"]
       (tr_ (do td_ [class_ "top",colspan_ "2",style_ "color:red"]
                    (p_ "Hello, attributes!")
                td_ "yay!"))
<table rows="2">
     <td class="top" colspan="2">
       <p>Hello, attributes!</p>


For proper rendering you can easily run some HTML immediately with:

λ> renderText (p_ "Hello!")

Or to bytes:

λ> renderBS (p_ [style_ "color:red"] "Hello!")
"<p style=\"color:red\">Hello!</p>"

For ease of use in GHCi, there is a Show instance, as demonstrated above.

If the above rendering functions aren’t suited for your purpose, you can run the monad directly via execHtml and use the more low-level blaze Builder, which has a plethora of output modes in Blaze.ByteString.Builder.

See the documentation for the Lucid module for information about using it as a monad transformer.


You can use lift to call parent monads.

λ> runReader (renderTextT (html_ (body_ (do name <- lift ask
                                            p_ [class_ "name"] (toHtml name)))))
             ("Chris" :: String)
"<html><body><p class=\"name\">Chris</p></body></html>"



  • Export renderToFile from top-level Lucid module.


  • Add some extra HTML tags.


  • Use variadic HTML terms.
  • Add lazy Text instance for ToHtml.


  • Initial version.