Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Label for Vue

A form label for Vue on Ark UI that names an input, a checkbox or a switch, and focuses or toggles it when its text is clicked.

On this page9 sections
UI library: Ark UI

Label is a <label> for one form control. Its text becomes the name a screen reader reads for the control, and clicking it focuses an input or toggles a checkbox. Link it with for set to the control's id, or put the control inside it.

Use it on its own for a checkbox row or a quick one-off field. When the field also needs help text or an error message, build it with Field, whose FieldLabel sits in the same layout as both.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Label anatomy: label, control, text

Usage

<script setup lang="ts">
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
</script>

The label's for must match the input's id, and each id must be unique on the page.

<template>
  <div class="grid max-w-xs gap-2">
    <Label for="email">Email</Label>
    <Input id="email" type="email" />
  </div>
</template>

Accessibility

Label renders a native <label>, so the browser does the work: its text names the control for a screen reader, and clicking it focuses or toggles the control. That happens only for a native form element it is linked to. Ark UI's Checkbox and Switch keep the id you pass for their state machine and give their hidden input a different one, so point the label at the hidden input through ids instead, as the preview does and the checkbox answer explains. For a custom control that isn't a form element, set aria-labelledby to the label's id.

Demos

RTL

The terms checkbox row, in Arabic, with dir set to rtl on the row. The row is a flex container that runs in the reading direction, so the checkbox moves to the right and the label follows it. Clicking the Arabic text toggles the checkbox, because the checkbox's ids gives its hidden input the id the label points at.

Preview width

FAQ

How do I associate a Label with an input in Vue?

Give the input an id and set the Label's for to the same value, as the Usage example does. Clicking the label then focuses the input, and a screen reader reads the label's text as the input's name. The id has to be unique on the page, or the label points at the first element that has it.

You can also put the input inside the Label, which links the two without any ids.

How do I label a Checkbox or a Switch with Label?

Set the Checkbox's ids to an object whose hiddenInput is the id the Label points at, as the preview above does. Ark UI's Checkbox and Switch keep a plain id for their state machine and give the hidden input another, so a label pointed at that id names nothing. A FieldLabel in a Field with orientation set to horizontal links the two with no ids to write.

Can a Label wrap its control instead of using an id?

Yes, for an Input or a native element, which a label that contains it links to without any ids. Don't wrap a Checkbox or a Switch, though. Their root is a <label> already, and a label inside another label isn't valid HTML, so link those through ids or a Field, as the checkbox answer shows.

How do I use a Label for a custom control that isn't an input?

Give the Label an id and set aria-labelledby on the control to the same value. A <label> only links to a native form element such as an <input>, a <textarea>, a <select> or a <button>. A div with a role, such as a custom slider or a color swatch, gets no name from it, and clicking the label does nothing.

To make the click work too, focus the control from the label's click handler.

Why doesn't my Label dim when its control is disabled?

It dims only when the control comes before it in the same parent, has the peer class and carries data-disabled, as Ark UI's Checkbox and Switch do when placed first. A label above an Input comes before the input, so the peer styles can't reach it from there.

What works with every control is a wrapper: give the row's container the group class and data-disabled set to true, and the Label fades and stops taking clicks.

How do I mark a Label as required or optional?

Put required on the control, which is what makes the browser enforce it and a screen reader announce it. Then show it in the label, such as an asterisk in a <span> with aria-hidden, so the star isn't read as part of the name. On a form where most fields are required, mark the few optional ones with the word Optional instead.

Say what the asterisk means once, near the top of the form, for people who don't know the convention.

When should I use Field instead of a Label?

Use Label for a caption and a control with nothing else around them, such as a checkbox row or a search box beside its label. Use Field once the field also has a description or an error message, or sits among other fields in a form.

FieldLabel is a label in that layout, and Field adds the spacing, the disabled and invalid styles, and a place for a FieldError. Mixing the two works, but a form reads more evenly built from one.

API Reference

Label

<label> data-slot="label"

PropType
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

class?string

Merged with the part's classes.