FloatingLabel
The FloatingLabel component creates modern form inputs with animated labels that float above the input when focused or filled. Perfect for clean, space-efficient forms with a contemporary aesthetic.
Goal
Master creating floating label inputs, understand Bootstrap form-floating classes, and build modern, elegant forms that provide excellent user experience.
Bootstrap Reference
Quick Start
Visual Examples & Use Cases
1. Login Form - Modern Design
from faststrap import FloatingLabel, Button, Card
Card(
H3("Sign In", cls="mb-4"),
FloatingLabel(
"email",
label="Email address",
input_type="email",
required=True
),
FloatingLabel(
"password",
label="Password",
input_type="password",
required=True
),
Button("Sign In", variant="primary", cls="w-100 mt-3"),
cls="p-4 shadow"
)
2. With Values - Pre-filled Inputs
# Label automatically floats when input has value
FloatingLabel(
"name",
label="Your Name",
value="John Doe" # ← Label floats automatically
)
3. Different Input Types
# Email
FloatingLabel("email", label="Email address", input_type="email")
# Password
FloatingLabel("password", label="Password", input_type="password")
# Number
FloatingLabel("age", label="Age", input_type="number")
# Date
FloatingLabel("birthdate", label="Birth Date", input_type="date")
# Tel
FloatingLabel("phone", label="Phone Number", input_type="tel")
4. Disabled and Readonly States
# Disabled
FloatingLabel(
"disabled_field",
label="Disabled Field",
value="Cannot edit",
disabled=True
)
# Readonly
FloatingLabel(
"readonly_field",
label="Readonly Field",
value="Can read only",
readonly=True
)
Bootstrap CSS Classes Explained
Core Classes
| Class | Purpose | Applied To |
|---|---|---|
.form-floating |
Container - Enables floating label effect | Wrapper <div> |
.form-control |
Input styling - Standard form control | <input> element |
placeholder attribute |
Required - Triggers float animation | <input> element |
Critical: The placeholder attribute is required for the floating effect to work, even if you don't want visible placeholder text. Set it to the same value as the label.
Core Faststrap Features
Global Defaults
from faststrap import set_component_defaults, FloatingLabel
# All floating labels required by default
set_component_defaults("FloatingLabel", required=True)
FloatingLabel("email", label="Email") # ← Automatically required
Common Recipes
Complete Registration Form
from faststrap import FloatingLabel, Button, Card
Card(
H3("Create Account"),
FloatingLabel("name", label="Full Name", required=True),
FloatingLabel("email", label="Email", input_type="email", required=True),
FloatingLabel("password", label="Password", input_type="password", required=True),
FloatingLabel("confirm", label="Confirm Password", input_type="password", required=True),
Button("Register", variant="primary", cls="w-100 mt-3"),
cls="p-4"
)
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute |
label |
str |
Required | Label text (floats on focus/fill) |
input_type |
str |
"text" |
HTML input type |
value |
str |
"" |
Initial value |
placeholder |
str |
Same as label | Placeholder (required for float effect) |
disabled |
bool |
False |
Disable input |
readonly |
bool |
False |
Make readonly |
required |
bool |
False |
Mark as required |
input_id |
str \| None |
Auto-generated | Input ID |
input_cls |
str |
"" |
Additional input classes |
label_cls |
str |
"" |
Additional label classes |
**kwargs |
Any |
- | Additional HTML attributes |
faststrap.components.forms.inputgroup.FloatingLabel(name, *, label, input_type='text', value='', placeholder='', disabled=False, readonly=False, required=False, input_id=None, input_cls='', label_cls='', **kwargs)
Bootstrap FloatingLabel input component.
An input with an animated floating label that moves when focused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute |
required |
label
|
str
|
Label text (required for floating labels) |
required |
input_type
|
Literal['text', 'password', 'email', 'number', 'url', 'tel', 'search', 'date', 'time', 'datetime-local']
|
HTML input type |
'text'
|
value
|
str
|
Initial value |
''
|
placeholder
|
str
|
Placeholder text (usually same as label) |
''
|
disabled
|
bool
|
Disable the input |
False
|
readonly
|
bool
|
Make input read-only |
False
|
required
|
bool
|
Mark as required field |
False
|
input_id
|
str | None
|
ID for the input (auto-generated from name if not provided) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with form-floating structure |
Example
Basic:
FloatingLabel("email", label="Email address", input_type="email")
With value:
FloatingLabel("name", label="Your name", value="John Doe")
Password:
FloatingLabel("password", label="Password", input_type="password", required=True)
See Also
Bootstrap docs: https://getbootstrap.com/docs/5.3/forms/floating-labels/
Source code in src/faststrap/components/forms/inputgroup.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |