mirror of
https://github.com/tabler/tabler.git
synced 2026-08-29 21:31:28 +04:00
480 lines
18 KiB
Plaintext
480 lines
18 KiB
Plaintext
---
|
|
title: Elements
|
|
seoTitle: Form elements
|
|
summary: Forms are one of the most important types of interaction with a website or app. Since their aim is to enable users to make a purchase, subscribe to a service or sign up to create an account, it's important to make sure they are easy to complete and help increase conversion rates. Use the available elements to create forms which are well-structured and user-friendly.
|
|
docs-libs: [nouislider]
|
|
description: Build user-friendly forms with styled inputs, selects, checkboxes, and radios. Learn about states, sizes, and layout options.
|
|
order: 1
|
|
related: [/ui/forms/validation, /ui/forms/helpers, /ui/forms/fieldset, /ui/plugins/date-picker, /ui/plugins/advanced-select]
|
|
---
|
|
import Example from '@components/Example.astro';
|
|
import CodeDocs from '@components/CodeDocs.astro';
|
|
import Icon from '@ui/Icon.astro'
|
|
|
|
## Classic inputs
|
|
|
|
Use classic, user-friendly inputs, label them appropriately and include input placeholders that will help users avoid confusion when completing a form. Add the `form-control` class to style your input controls.
|
|
|
|
```html
|
|
<input type="text" class="form-control" name="example-text-input" placeholder="Input placeholder" />
|
|
```
|
|
|
|
All variants of the input control are available in the examples below:
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Text</label>
|
|
<input type="text" class="form-control" name="example-text-input" placeholder="Input placeholder" />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Password</label>
|
|
<input type="password" class="form-control" name="example-password-input" placeholder="Input placeholder" />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Disabled</label>
|
|
<input type="text" class="form-control" name="example-password-input" placeholder="Input placeholder" disabled />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Readonly</label>
|
|
<input type="text" class="form-control" name="example-password-input" value="Readolny value" readonly />
|
|
</div>
|
|
</Example>
|
|
|
|
## Form control rounded
|
|
|
|
Use the `form-control-rounded` class if you prefer form controls with rounded corners.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Form control rounded</label>
|
|
<input type="text" class="form-control form-control-rounded mb-2" name="Form control rounded" placeholder="Text.." />
|
|
<div class="input-icon">
|
|
<input type="text" value="" class="form-control form-control-rounded" placeholder="Search…" />
|
|
<span class="input-icon-addon">
|
|
<Icon name="search" />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Form control flush
|
|
|
|
You can remove borders from your form control by adding the `form-control-flush` class.
|
|
|
|
<Example column>
|
|
<label class="form-label">Form control flush</label>
|
|
<input type="text" class="form-control form-control-flush" name="Form control flush" placeholder="Text.." />
|
|
</Example>
|
|
|
|
## Input with icon
|
|
|
|
Add [icons](/ui/components/icon) to your input controls to suggest users what they should enter or inform them of the current state of a form element.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Icon input</label>
|
|
<div class="input-icon mb-3">
|
|
<input type="text" value="" class="form-control" placeholder="Search…" />
|
|
<span class="input-icon-addon">
|
|
<Icon name="search" />
|
|
</span>
|
|
</div>
|
|
<div class="input-icon mb-3">
|
|
<span class="input-icon-addon">
|
|
<Icon name="user" />
|
|
</span>
|
|
<input type="text" value="" class="form-control" placeholder="Username" />
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Loader input</label>
|
|
<div class="input-icon mb-3">
|
|
<input type="text" value="" class="form-control" placeholder="Loading…" />
|
|
<span class="input-icon-addon">
|
|
<div class="spinner-border spinner-border-sm text-secondary" role="status"></div>
|
|
</span>
|
|
</div>
|
|
<div class="input-icon mb-3">
|
|
<span class="input-icon-addon">
|
|
<div class="spinner-border spinner-border-sm text-secondary" role="status"></div>
|
|
</span>
|
|
<input type="text" value="" class="form-control" placeholder="Loading…" />
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Separated inputs
|
|
|
|
Include an additional element in your input section, such as a [button](/ui/components/button) which can be used to submit the information entered in the input control.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Separated inputs</label>
|
|
<div class="row g-2">
|
|
<div class="col"><input type="text" class="form-control" placeholder="Search for…" /></div>
|
|
<div class="col-auto">
|
|
<a href="#" class="btn btn-icon" aria-label="Button">
|
|
<Icon name="search" />
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Textarea and select
|
|
|
|
Use a multi-line text input control to enable users to enter longer pieces of text. The control will automatically adjust to the length of the text entered.
|
|
|
|
Add one of the available selects - either a dropdown or a multiple choice select - to let users choose from a predefined set of options.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Textarea</label>
|
|
<textarea class="form-control" name="example-textarea" placeholder="Textarea placeholder"></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<div class="form-label">Select</div>
|
|
<select class="form-select">
|
|
<option value="1">One</option>
|
|
<option value="2">Two</option>
|
|
<option value="3">Three</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<div class="form-label">Select multiple</div>
|
|
<select class="form-select" multiple>
|
|
<option value="1">One</option>
|
|
<option value="2">Two</option>
|
|
<option value="3">Three</option>
|
|
</select>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input size
|
|
|
|
Choose the size of an input control that will go well with your form design. Besides the default size, you can also use small and large input controls.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input sizing</label>
|
|
<input class="form-control form-control-lg mb-2" type="text" placeholder=".form-control-lg" />
|
|
<input class="form-control mb-2" type="text" placeholder="Default input" />
|
|
<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm" />
|
|
</div>
|
|
</Example>
|
|
|
|
## Datalists
|
|
|
|
Use the `datalist` element to add an autocomplete feature to your input control. The list of available options will display once a user starts to type and will make it quicker to complete form sections.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Datalist example</label>
|
|
<input class="form-control" list="datalistOptions" placeholder="Type to search..." />
|
|
<datalist id="datalistOptions">
|
|
<option value="Aruba" />
|
|
<option value="Afghanistan" />
|
|
<option value="Angola" />
|
|
<option value="Anguilla" />
|
|
<option value="Albania" />
|
|
<option value="Andorra" />
|
|
<option value="United Arab Emirates" />
|
|
<option value="Argentina" />
|
|
<option value="Armenia" />
|
|
<option value="American Samoa" />
|
|
</datalist>
|
|
</div>
|
|
</Example>
|
|
|
|
## Toggle switches
|
|
|
|
Use toggle switches for the elements of your form which require choosing between two opposing states.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<div class="form-label">Toggle switches</div>
|
|
<label class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" checked />
|
|
<span class="form-check-label">Option 1</span>
|
|
</label>
|
|
<label class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" />
|
|
<span class="form-check-label">Option 2</span>
|
|
</label>
|
|
<label class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" />
|
|
<span class="form-check-label">Option 3</span>
|
|
</label>
|
|
</div>
|
|
<div class="mb-3">
|
|
<div class="form-label">Single switch</div>
|
|
<label class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" />
|
|
<span class="form-check-label">I agree with terms and conditions</span>
|
|
</label>
|
|
</div>
|
|
</Example>
|
|
|
|
## Radios
|
|
|
|
Use radio buttons for the parts of your form which require users to choose one option from a set of two or more mutually exclusive options.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<div class="form-label">Radios</div>
|
|
<div>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="radio" name="radios" checked />
|
|
<span class="form-check-label">Option 1</span>
|
|
</label>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="radio" name="radios" />
|
|
<span class="form-check-label">Option 2</span>
|
|
</label>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="radio" disabled />
|
|
<span class="form-check-label">Option 3</span>
|
|
</label>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="radio" checked disabled />
|
|
<span class="form-check-label">Option 4</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<div class="form-label">Inline Radios</div>
|
|
<div>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="radio" name="radios-inline" checked />
|
|
<span class="form-check-label">Option 1</span>
|
|
</label>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="radio" name="radios-inline" />
|
|
<span class="form-check-label">Option 2</span>
|
|
</label>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="radio" name="radios-inline" disabled />
|
|
<span class="form-check-label">Option 3</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Checkboxes
|
|
|
|
Use checkboxes if you want to allow users to select more than one option from a set of predefined options or to turn an option on or off.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<div class="form-label">Checkboxes</div>
|
|
<div>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="checkbox" />
|
|
<span class="form-check-label">Checkbox input</span>
|
|
</label>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="checkbox" disabled />
|
|
<span class="form-check-label">Disabled checkbox input</span>
|
|
</label>
|
|
<label class="form-check">
|
|
<input class="form-check-input" type="checkbox" checked />
|
|
<span class="form-check-label">Checked checkbox input</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<div class="form-label">Inline Checkboxes</div>
|
|
<div>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" />
|
|
<span class="form-check-label">Option 1</span>
|
|
</label>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" disabled />
|
|
<span class="form-check-label">Option 2</span>
|
|
</label>
|
|
<label class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" checked />
|
|
<span class="form-check-label">Option 3</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Range input
|
|
|
|
Add a [range slider](/ui/plugins/range-slider) to make it possible for users to set a value or range, such as a price range or a time frame.
|
|
|
|
<Example column>
|
|
<div class="mb-3"> <label class="form-label">Range input</label> <input type="range" class="form-range mb-2" value="40" min="0" max="100" step="10" /> <div class="form-range mb-2 text-green" id="range-color"></div> </div> <script>{`document.addEventListener("DOMContentLoaded", function () { window.noUiSlider && noUiSlider.create(document.getElementById("range-color"), { start: 40, connect: [true, false], step: 10, range: { min: 0, max: 100, }, }); });`}</script>
|
|
</Example>
|
|
|
|
## Input group
|
|
|
|
Create a group of input controls and place add-ons on either side of an input.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input group</label>
|
|
<div class="input-group mb-2">
|
|
<span class="input-group-text">@</span>
|
|
<input type="text" class="form-control" placeholder="username" autocomplete="off" />
|
|
</div>
|
|
<div class="input-group mb-2">
|
|
<input type="text" class="form-control" placeholder="subdomain" autocomplete="off" />
|
|
<span class="input-group-text">.tabler.io</span>
|
|
</div>
|
|
<div class="input-group">
|
|
<span class="input-group-text">https://</span>
|
|
<input type="text" class="form-control" placeholder="subdomain" autocomplete="off" />
|
|
<span class="input-group-text">.tabler.io</span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input with checkboxes or radios
|
|
|
|
Add checkboxes or radio buttons on either side of your input control.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input with checkbox or radios</label>
|
|
<div class="input-group mb-2">
|
|
<span class="input-group-text">
|
|
<input class="form-check-input m-0" type="checkbox" checked />
|
|
</span>
|
|
<input type="text" class="form-control" autocomplete="off" />
|
|
</div>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" autocomplete="off" />
|
|
<span class="input-group-text">
|
|
<input class="form-check-input m-0" type="radio" checked />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input with prepended or appended text
|
|
|
|
Add text to your input control, either before or after the text which is to be entered by a user.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input with prepended text</label>
|
|
<div class="input-group input-group-flat mb-2">
|
|
<span class="input-group-text">https://tabler.io/users/</span>
|
|
<input type="text" class="form-control ps-0" value="yourfancyusername" autocomplete="off" />
|
|
</div>
|
|
<div class="input-group input-group-flat">
|
|
<input type="text" class="form-control text-end pe-0" value="yourfancydomain" autocomplete="off" />
|
|
<span class="input-group-text">.tabler.io</span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input with appended link
|
|
|
|
Include a link in your input control to add a clickable element within the control.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input with appended link</label>
|
|
<div class="input-group input-group-flat">
|
|
<input type="password" class="form-control" value="ultrastrongpassword" autocomplete="off" />
|
|
<span class="input-group-text"><a href="#" class="input-group-link">Show password</a></span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input with appended kbd
|
|
|
|
Include a `<kbd>` in your input control to add a shortcut hint to the control.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input with appended kbd</label>
|
|
<div class="input-group input-group-flat">
|
|
<input type="password" class="form-control" value="ultrastrongpassword" autocomplete="off" />
|
|
<span class="input-group-text"><kbd>ctrl + K</kbd></span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Input with appended icon links
|
|
|
|
Add an icon link which you want to display at the end of your input control to visually represent actions which a user can take.
|
|
|
|
<Example column>
|
|
<div class="mb-3">
|
|
<label class="form-label">Input with appended icon links</label>
|
|
<div class="input-group input-group-flat">
|
|
<input type="text" class="form-control" autocomplete="off" />
|
|
<span class="input-group-text">
|
|
<a href="#" class="link-secondary" title="Clear search" data-bs-toggle="tooltip">
|
|
<Icon name="x" />
|
|
</a>
|
|
<a href="#" class="link-secondary ms-2" title="Search settings" data-bs-toggle="tooltip">
|
|
<Icon name="adjustments" />
|
|
</a>
|
|
<a href="#" class="link-secondary ms-2 disabled" title="Add notification" data-bs-toggle="tooltip">
|
|
<Icon name="bell" />
|
|
</a>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Horizontal form
|
|
|
|
Put the label and the field in one `row` to place them side by side. The label takes `col-form-label` instead of `form-label`, which keeps it aligned with the field rather than sitting above it.
|
|
|
|
Add `required` to the label to append a red asterisk.
|
|
|
|
<Example>
|
|
<div class="mb-3 row">
|
|
<label class="col-3 col-form-label required" for="form-horizontal-email">Email address</label>
|
|
<div class="col">
|
|
<input type="email" id="form-horizontal-email" class="form-control" placeholder="you@example.com" />
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-3 col-form-label" for="form-horizontal-company">Company</label>
|
|
<div class="col">
|
|
<input type="text" id="form-horizontal-company" class="form-control" placeholder="Optional" />
|
|
</div>
|
|
</div>
|
|
</Example>
|
|
|
|
## Form footer
|
|
|
|
Wrap the buttons that close a form in `form-footer`. It only adds the top margin that separates them from the last field, so the spacing is the same in every form.
|
|
|
|
<Example>
|
|
<div class="mb-3">
|
|
<label class="form-label" for="form-footer-name">Name</label>
|
|
<input type="text" id="form-footer-name" class="form-control" />
|
|
</div>
|
|
<div class="form-footer">
|
|
<button type="submit" class="btn btn-primary w-100">Create account</button>
|
|
</div>
|
|
</Example>
|
|
|
|
## Accessibility
|
|
|
|
- Every field needs a `label` with a `for` that matches the field id. A placeholder is not a label - it disappears as soon as the user types.
|
|
- When the design has no room for a visible label, keep the `label` and hide it with `visually-hidden` rather than dropping it.
|
|
- Tie help text and errors to the field with `aria-describedby`, so they are read together with it. See [form helpers](/ui/forms/helpers) and [validation states](/ui/forms/validation).
|
|
- Mark a required field with the `required` attribute, not only with an asterisk in the label.
|
|
- Group related checkboxes or radios in a `fieldset` with a `legend`. The legend is the only thing that names the group. See [fieldsets](/ui/forms/fieldset).
|
|
- Keep `input` types honest - `email`, `tel`, `url` and `number` change the on-screen keyboard on phones and let the browser validate for you.
|
|
- Do not disable a field to make it read-only. `disabled` removes it from the tab order and from the submitted data; `readonly` keeps both.
|
|
|
|
## SCSS variables
|
|
|
|
Use these SCSS variables to customize form controls. The default values are:
|
|
|
|
<CodeDocs name="input-variables" file="core/scss/_variables.scss" />
|