BSD-3-Clause licensed by Chris Done
Maintained by [email protected]
This version can be pinned in stack with:lucid-2.11.20230408@sha256:a8bbc65cd2b77e37140f8a89112e250a254591a8a341dc0cbb7db9db71686017,3046

Module documentation for 2.11.20230408

lucid Hackage Build Status

Clear to write, read and edit DSL for writing HTML

Table of Contents


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 style="color:red" colspan="2" class="top">
      <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.

Good to know

  • Attributes are escaped, so you cannot write arbitrary JavaScript in attributes. Instead, do something like onclick_ "foo()".
  • Attributes are rendered in the order that they are written in your Haskell code.


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>"




  • Don’t expect Control.Monad to be re-exported from mtl anymore


  • Use explicit imports for mtl, avoids the mtl-2.3 issue
  • Added minlength attribute.
  • Added loading attribute.


  • Change internal attributes to Seq Attribute. This preserves ordering. Attributes are merged in a left-biased way, preserving the key order as first encountered.


  • Change internal attributes representation from HashMap to Map. This introduces stable ordering, at a negligible performance cost for realistic element sizes. This may affect some test suites.
  • doctype no longer accepts attributes. You can use with with doctypeHtml now, if needed.

  • Allow different orderings of attributes in test-suite


  • Add MonadFix instance


  • Add GHC-8.6 support
  • row-fluid and container-fluid instead of camelCase


  • Drop GHC-7.8 and older (pre-AMP) support
  • Generalise type-signatures to require only Applicative or Functor, when that’s enough


  • Add commuteHtmlT to commute HtmlT m a into m (HtmlT n a).
  • Add MonadError e m => MonadError e (HtmlT m) and MonadWriter w m => MonadWriter w (HtmlT m) instances

  • Improve performance by adding INLINE pragmas to Monad etc. combinators.


  • Add integrity_, crossorigin_ attributes
  • Add classes_ smart attribute constructor
  • Add ToHtml (HtmlT m a) instance


  • Add Semigroup (HtmlT m a) instance
  • Add MonadState and MonadReader instances


  • Fix compilation of benchmarks
  • Add @athanclark’s version of relaxHtmlT
  • Add a utility to generalize the underlying monad from Identity: relaxHtmlT


  • Add ToHtml instance for ByteString (both)
  • Add MFunctor HtmlT instance, i.e. hoist from @mmorph@.


  • Small performance tweaks.
  • Make svg_ an element.


  • Restrict monoid instance’s a to ~ () (means you can use mempty without inference errors)


  • Export renderToFile from top-level Lucid module.


  • Add some extra HTML tags.


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


  • Initial version.