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
| Element | Description | MDN Docs | Spec |
|---|
area | Image map area | MDN | Spec |
base | Base URL for relative URLs | MDN | Spec |
br | Line break | MDN | Spec |
col | Table column | MDN | Spec |
embed | External content | MDN | Spec |
hr | Horizontal rule / thematic break | MDN | Spec |
img | Image | MDN | Spec |
input | Form input | MDN | Spec |
link | External resource link | MDN | Spec |
meta | Metadata | MDN | Spec |
source | Media source | MDN | Spec |
track | Text track for media | MDN | Spec |
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 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
# 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