Label
A caption for a form control.
Native <label>
<script lang="ts">
import { Checkbox } from '#lib/components/ui/checkbox/index.js';
import { Input } from '#lib/components/ui/input/index.js';
import { Label } from '#lib/components/ui/label/index.js';
</script>
<div class="field">
<Label for="name">Name</Label>
<Input id="name" placeholder="Ada Lovelace" />
</div>
<Label><Checkbox /> Wrap a control to label it without an id</Label>
<style>
.field {
display: grid;
gap: var(--dui-space-2);
width: min(100%, 20rem);
margin-bottom: var(--dui-space-4);
}
</style>
Installation
npx distill-ui add label- Set up the tokens once, if you haven't: see manual installation.
- Copy these files into your project:
import Root from './label.svelte';
export { Root, Root as Label };
<script lang="ts">
import type { HTMLLabelAttributes } from 'svelte/elements';
let {
ref = $bindable(null),
class: className,
children,
...restProps
}: HTMLLabelAttributes & { ref?: HTMLLabelElement | null } = $props();
</script>
<label {...restProps} bind:this={ref} class={['label', className]}>
{@render children?.()}
</label>
<!--
Options you can set from a parent or on the label itself:
--dui-label-fg
-->
<style>
.label {
display: flex;
align-items: center;
gap: var(--dui-space-2);
color: var(--dui-label-fg, var(--dui-color-foreground));
font-size: var(--dui-text-sm);
line-height: 1;
font-weight: var(--dui-font-weight-medium);
user-select: none;
}
</style>
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-label-fg | var(--dui-color-foreground) |