evancz/elm-html/4.0.2

NOTE: this package moved to elm/html

NOTE: this package is not compatible with Elm 0.19.1

Html.Attributes

Helper functions for HTML attributes. They are organized roughly by category. Each attribute is labeled with the HTML tags it can be used with, so just search the page for video if you want video stuff.

If you cannot find what you are looking for, go to the Custom Attributes section to learn how to create new helpers.

Special Attributes

key : String -> Attribute

A special attribute that uniquely identifies a node during the diffing process. If you have a list of 20 items and want to remove the 4th one, adding keys ensures that you do not end up doing misaligned diffs on the following 15 items.

style : List ( String, String ) -> Attribute

This function makes it easier to specify a set of styles.

myStyle : Attribute
myStyle =
  style
    [ ("backgroundColor", "red")
    , ("height", "90px")
    , ("width", "100%")
    ]


greeting : Html
greeting =
  div [ myStyle ] [ text "Hello!" ]

There is no Html.Styles module because best practices for working with HTML suggest that this should primarily be specified in CSS files. So the general recommendation is to use this function lightly.

Super Common Attributes

class : String -> Attribute

Often used with CSS to style elements with common properties.

classList : List ( String, Bool ) -> Attribute

This function makes it easier to build a space-separated class attribute. Each class can easily be added and removed depending on the boolean value it is paired with.

renderMessage : Msg -> Html
renderMessage msg =
  div
    [
      classList [
        ("message", True),
        ("message-important", msg.isImportant),
        ("message-read", msg.isRead)
      ]
    ]
    [ text msg.content ]
id : String -> Attribute

Often used with CSS to style a specific element. The value of this attribute must be unique.

title : String -> Attribute

Text to be displayed in a tooltip when hovering over the element.

hidden : Bool -> Attribute

Indicates the relevance of an element.

Inputs

type' : String -> Attribute

Defines the type of a button, input, embed, object, script, source, style, or menu.

value : String -> Attribute

Defines a default value which will be displayed in a button, option, input, li, meter, progress, or param.

checked : Bool -> Attribute

Indicates whether an input of type checkbox is checked.

placeholder : String -> Attribute

Provides a hint to the user of what can be entered into an input or textarea.

selected : Bool -> Attribute

Defines which option will be selected on page load.

Input Helpers

accept : String -> Attribute

List of types the server accepts, typically a file type. For form and input.

acceptCharset : String -> Attribute

List of supported charsets in a form.

action : String -> Attribute

The URI of a program that processes the information submitted via a form.

autocomplete : Bool -> Attribute

Indicates whether a form anor input can have their values automatically completed by the browser.

autofocus : Bool -> Attribute

The element should be automatically focused after the page loaded. For button, input, keygen, select, and textarea.

autosave : String -> Attribute

Previous entries into an input will be persisted across page loads, associated with a unique ID. The previous entries will be displayed as suggestions when the user types into an input that has an autosave attribute with the same unique ID.

disabled : Bool -> Attribute

Indicates whether the user can interact with a button, fieldset, input, keygen, optgroup, option, select or textarea.

enctype : String -> Attribute

How form data should be encoded when submitted with the POST method. Options include: application/x-www-form-urlencoded, multipart/form-data, and text/plain.

formaction : String -> Attribute

Indicates the action of an input or button. This overrides the action defined in the surrounding form.

list : String -> Attribute

Associates an input with a datalist tag. The datalist gives some pre-defined options to suggest to the user as they interact with an input. The value of the list attribute must match the id of a datalist node. For input.

maxlength : Int -> Attribute

Defines the maximum number of characters allowed in an input or textarea.

minlength : Int -> Attribute

Defines the minimum number of characters allowed in an input or textarea.

method : String -> Attribute

Defines which HTTP method to use when submitting a form. Can be GET (default) or POST.

multiple : Bool -> Attribute

Indicates whether multiple values can be entered in an input of type email or file. Can also indicate that you can select many options.

name : String -> Attribute

Name of the element. For example used by the server to identify the fields in form submits. For button, form, fieldset, iframe, input, keygen, object, output, select, textarea, map, meta, and param.

novalidate : Bool -> Attribute

This attribute indicates that a form shouldn't be validated when submitted.

pattern : String -> Attribute

Defines a regular expression which an input's value will be validated against.

readonly : Bool -> Attribute

Indicates whether an input or textarea can be edited.

required : Bool -> Attribute

Indicates whether this element is required to fill out or not. For input, select, and textarea.

size : Int -> Attribute

For input specifies the width of an input in characters.

For select specifies the number of visible options in a drop-down list.

for : String -> Attribute

The element ID described by this label or the element IDs that are used for an output.

form : String -> Attribute

Indicates the element ID of the form that owns this particular button, fieldset, input, keygen, label, meter, object, output, progress, select, or textarea.

Input Ranges

max : String -> Attribute

Indicates the maximum value allowed. When using an input of type number or date, the max value must be a number or date. For input, meter, and progress.

min : String -> Attribute

Indicates the minimum value allowed. When using an input of type number or date, the min value must be a number or date. For input and meter.

step : String -> Attribute

Add a step size to an input. Use step "any" to allow any floating-point number to be used in the input.

Input Text Areas

cols : Int -> Attribute

Defines the number of columns in a textarea.

rows : Int -> Attribute

Defines the number of rows in a textarea.

wrap : String -> Attribute

Indicates whether the text should be wrapped in a textarea. Possible values are "hard" and "soft".

Links and Areas

href : String -> Attribute

The URL of a linked resource, such as a, area, base, or link.

target : String -> Attribute

Specify where the results of clicking an a, area, base, or form should appear. Possible special values include:

* _blank — a new window or tab * _self — the same frame (this is default) * _parent — the parent frame * _top — the full body of the window

You can also give the name of any frame you have created.

download : Bool -> Attribute

Indicates that clicking an a and area will download the resource directly.

downloadAs : String -> Attribute

Indicates that clicking an a and area will download the resource directly, and that the downloaded resource with have the given filename.

hreflang : String -> Attribute

Two-letter language code of the linked resource of an a, area, or link.

media : String -> Attribute

Specifies a hint of the target media of a a, area, link, source, or style.

ping : String -> Attribute

Specify a URL to send a short POST request to when the user clicks on an a or area. Useful for monitoring and tracking.

rel : String -> Attribute

Specifies the relationship of the target object to the link object. For a, area, link.

Maps

ismap : Bool -> Attribute

When an img is a descendent of an a tag, the ismap attribute indicates that the click location should be added to the parent a's href as a query string.

usemap : String -> Attribute

Specify the hash name reference of a map that should be used for an img or object. A hash name reference is a hash symbol followed by the element's name or id. E.g. "#planet-map".

shape : String -> Attribute

Declare the shape of the clickable area in an a or area. Valid values include: default, rect, circle, poly. This attribute can be paired with coords to create more particular shapes.

coords : String -> Attribute

A set of values specifying the coordinates of the hot-spot region in an area. Needs to be paired with a shape attribute to be meaningful.

Embedded Content

src : String -> Attribute

The URL of the embeddable content. For audio, embed, iframe, img, input, script, source, track, and video.

height : Int -> Attribute

Declare the height of a canvas, embed, iframe, img, input, object, or video.

width : Int -> Attribute

Declare the width of a canvas, embed, iframe, img, input, object, or video.

alt : String -> Attribute

Alternative text in case an image can't be displayed. Works with img, area, and input.

Audio and Video

autoplay : Bool -> Attribute

The audio or video should play as soon as possible.

controls : Bool -> Attribute

Indicates whether the browser should show playback controls for the audio or video.

loop : Bool -> Attribute

Indicates whether the audio or video should start playing from the start when it's finished.

preload : String -> Attribute

Control how much of an audio or video resource should be preloaded.

poster : String -> Attribute

A URL indicating a poster frame to show until the user plays or seeks the video.

default : Bool -> Attribute

Indicates that the track should be enabled unless the user's preferences indicate something different.

kind : String -> Attribute

Specifies the kind of text track.

srclang : String -> Attribute

A two letter language code indicating the language of the track text data.

iframes

sandbox : String -> Attribute

A space separated list of security restrictions you'd like to lift for an iframe.

seamless : Bool -> Attribute

Make an iframe look like part of the containing document.

srcdoc : String -> Attribute

An HTML document that will be displayed as the body of an iframe. It will override the content of the src attribute if it has been specified.

Ordered Lists

reversed : Bool -> Attribute

Indicates whether an ordered list ol should be displayed in a descending order instead of a ascending.

start : Int -> Attribute

Defines the first number of an ordered list if you want it to be something besides 1.

Tables

align : String -> Attribute

Specifies the horizontal alignment of a caption, col, colgroup, hr, iframe, img, table, tbody, td, tfoot, th, thead, or tr.

colspan : Int -> Attribute

The colspan attribute defines the number of columns a cell should span. For td and th.

rowspan : Int -> Attribute

Defines the number of rows a table cell should span over. For td and th.

headers : String -> Attribute

A space separated list of element IDs indicating which th elements are headers for this cell. For td and th.

scope : String -> Attribute

Specifies the scope of a header cell th. Possible values are: col, row, colgroup, rowgroup.

Header Stuff

async : Bool -> Attribute

Indicates that the script should be executed asynchronously.

charset : String -> Attribute

Declares the character encoding of the page or script. Common values include:

* UTF-8 - Character encoding for Unicode * ISO-8859-1 - Character encoding for the Latin alphabet

For meta and script.

content : String -> Attribute

A value associated with http-equiv or name depending on the context. For meta.

defer : Bool -> Attribute

Indicates that a script should be executed after the page has been parsed.

httpEquiv : String -> Attribute

This attribute is an indicator that is paired with the content attribute, indicating what that content means. httpEquiv can take on three different values: content-type, default-style, or refresh. For meta.

language : String -> Attribute

Defines the script language used in a script.

scoped : Bool -> Attribute

Indicates that a style should only apply to its parent and all of the parents children.

Less Common Global Attributes

Attributes that can be attached to any HTML tag but are less commonly used.

accesskey : Char -> Attribute

Defines a keyboard shortcut to activate or add focus to the element.

contenteditable : Bool -> Attribute

Indicates whether the element's content is editable.

contextmenu : String -> Attribute

Defines the ID of a menu element which will serve as the element's context menu.

dir : String -> Attribute

Defines the text direction. Allowed values are ltr (Left-To-Right) or rtl (Right-To-Left).

draggable : String -> Attribute

Defines whether the element can be dragged.

dropzone : String -> Attribute

Indicates that the element accept the dropping of content on it.

itemprop : String -> Attribute
lang : String -> Attribute

Defines the language used in the element.

spellcheck : Bool -> Attribute

Indicates whether spell checking is allowed for the element.

tabindex : Int -> Attribute

Overrides the browser's default tab order and follows the one specified instead.

Key Generation

challenge : String -> Attribute

A challenge string that is submitted along with the public key in a keygen.

keytype : String -> Attribute

Specifies the type of key generated by a keygen. Possible values are: rsa, dsa, and ec.

Miscellaneous

cite : String -> Attribute

Contains a URI which points to the source of the quote or change in a blockquote, del, ins, or q.

datetime : String -> Attribute

Indicates the date and time associated with the element. For del, ins, time.

pubdate : String -> Attribute

Indicates whether this date and time is the date of the nearest article ancestor element. For time.

manifest : String -> Attribute

Specifies the URL of the cache manifest for an html tag.

Custom Attributes

When using HTML and JS, there are two ways to specify parts of a DOM node.

  1. Attributes — You can set things in HTML itself. So the class in <div class="greeting"></div> is called an attribute.

  2. Properties — You can also set things in JS. So the className in div.className = 'greeting' is called a property.

So the class attribute corresponds to the className property. At first glance, perhaps this distinction is defensible, but it gets much crazier. There is not always a one-to-one mapping between attributes and properties! Yes, that is a true fact. Sometimes an attribute exists, but there is no corresponding property. Sometimes changing an attribute does not change the underlying property. For example, as of this writing the webkit-playsinline attribute can be used in HTML, but there is no corresponding property!

Pretty much all of the functions in Html.Attributes are defined with property and that is generally the preferred approach.

property : String -> Value -> Attribute

Create arbitrary properties.

import Json.Encode as Json


greeting : Html
greeting =
    div [ property "className" (Json.string "greeting") ] [
      text "Hello!"
    ]

Notice that you must give the property name, so we use className as it would be in JavaScript, not class as it would appear in HTML.

attribute : String -> String -> Attribute

Create arbitrary HTML attributes. Maps onto JavaScript’s setAttribute function under the hood.

greeting : Html
greeting =
    div [ attribute "class" "greeting" ] [
      text "Hello!"
    ]

Notice that you must give the attribute name, so we use class as it would be in HTML, not className as it would appear in JS.