Skip to main content
Void HTML elements don’t accept content and never have a closing tag. They are self-closing and only accept attributes.

Usage

All void element methods follow this signature:
element_name(**attributes)
Note that void elements do not accept a block for content.

Examples

class MyView < Phlex::HTML
  def view_template
    # Images
    img(src: "/logo.png", alt: "Company Logo")
    
    # Line breaks
    p do
      plain "First line"
      br
      plain "Second line"
    end
    
    # Form inputs
    input(type: "text", name: "username", placeholder: "Enter username")
    input(type: "email", name: "email", required: true)
  end
end
# Document metadata
head do
  meta(charset: "utf-8")
  meta(name: "viewport", content: "width=device-width, initial-scale=1")
  link(rel: "stylesheet", href: "/styles.css")
end
# Horizontal rule
hr(class: "my-4")

# Base URL
base(href: "https://example.com/")

Available Elements

ElementDescriptionMDN DocsSpec
areaImage map areaMDNSpec
baseBase URL for relative URLsMDNSpec
brLine breakMDNSpec
colTable columnMDNSpec
embedExternal contentMDNSpec
hrHorizontal rule / thematic breakMDNSpec
imgImageMDNSpec
inputForm inputMDNSpec
linkExternal resource linkMDNSpec
metaMetadataMDNSpec
sourceMedia sourceMDNSpec
trackText track for mediaMDNSpec

Total Count

12 void HTML elements are available in Phlex.

Common Use Cases

Images with Attributes

img(
  src: "/photos/product.jpg",
  alt: "Product photo",
  width: 800,
  height: 600,
  loading: "lazy"
)

Form Inputs

form do
  input(type: "text", name: "username", placeholder: "Username", required: true)
  input(type: "password", name: "password", placeholder: "Password")
  input(type: "submit", value: "Login")
end

Document Head Elements

head do
  meta(charset: "utf-8")
  meta(name: "viewport", content: "width=device-width, initial-scale=1")
  meta(name: "description", content: "Page description")
  link(rel: "stylesheet", href: "/app.css")
  link(rel: "icon", href: "/favicon.ico")
end

Media Elements

# Picture with sources
picture do
  source(srcset: "/image.webp", type: "image/webp")
  source(srcset: "/image.jpg", type: "image/jpeg")
  img(src: "/image.jpg", alt: "Fallback")
end

# Video with track
video(controls: true) do
  source(src: "/video.mp4", type: "video/mp4")
  track(kind: "subtitles", src: "/subtitles.vtt", srclang: "en")
end

Table Columns

table do
  colgroup do
    col(style: "width: 30%")
    col(style: "width: 70%")
  end
  # ... table content
end

Important Notes

Void elements do not accept blocks or content. Attempting to pass a block to a void element will result in an error.
# INCORRECT - Will not work
img(src: "/photo.jpg") { "Alt text" }

# CORRECT - Use attributes only
img(src: "/photo.jpg", alt: "Alt text")

See Also

Build docs developers (and LLMs) love