Button
The Button component is one of the most fundamental components in web development. In FastStrap, the Button component wraps the standard HTML <button> or <a> element, adding beautiful Bootstrap styling, automatic loading states, and icon support—all in pure Python.
Goal
By the end of this guide, you will be able to create buttons for forms, navigation, and interactive actions, and customize them to fit your exact design needs, even if you've never used Bootstrap before.
Quick Start
Here is the simplest way to create a button.
Visual Examples & Use Cases
1. Variants (Colors = Meaning)
In Bootstrap (and FastStrap), colors carry semantic meaning. You don't just pick "blue"; you pick "Primary" (for the main action).
Button("Primary", variant="primary")
Button("Secondary", variant="secondary")
Button("Success", variant="success")
Button("Danger", variant="danger")
Button("Warning", variant="warning")
Button("Info", variant="info")
Button("Light", variant="light")
Button("Dark", variant="dark")
Button("Link", variant="link")
Understanding Semantic Colors:
| Variant | Meaning | Typical Usage |
|---|---|---|
primary |
Main Action | Submit, Save, Login, Sign Up |
secondary |
Secondary | Cancel, Back, More Info |
success |
Positive | Confirm, Complete, Uploaded |
danger |
Negative | Delete, Remove, Stop |
warning |
Caution | Pause, Archive, Reset |
info |
Information | Help, About, Status |
light |
Light | Backgrounds, App Bars |
dark |
Dark | Footer actions, Inverse styling |
link |
Link | Look like a text link but act like a button |
2. Outline Styles
Sometimes a solid color is too "heavy" for a UI. Use outline=True for a lighter look with a transparent background and colored border.
3. Sizes
Hierarchy matters. Make your most important buttons larger and secondary actions smaller.
4. Full Width Buttons
On mobile devices or in cards, you often want a button to stretch the full width of its container. Use full_width=True.
Practical Functionality
Advanced Customization
1. Override using CSS Variables (The Pro Way)
Bootstrap 5 is built on CSS Variables. This is the best way to customize a specific button (like making a "Purple" brand button) without fighting the framework's CSS rules.
The following variables are available on every button:
| CSS Variable | Description | Example Value |
|---|---|---|
--bs-btn-bg |
Background color of the button. | #6f42c1 (Purple) |
--bs-btn-color |
Text color of the button. | #ffffff (White) |
--bs-btn-border-color |
Border color. Usually same as bg. | #6f42c1 |
--bs-btn-hover-bg |
Background color when hovered. | #59359a (Darker Purple) |
--bs-btn-hover-color |
Text color when hovered. | #ffffff |
--bs-btn-hover-border-color |
Border color when hovered. | #59359a |
--bs-btn-border-radius |
Corner roundness. | 2rem (Pill), 0 (Square) |
--bs-btn-padding-y |
Vertical padding (height). | 1rem |
--bs-btn-padding-x |
Horizontal padding (width). | 2rem |
--bs-btn-font-size |
Text size. | 1.25rem |
Example: Creating a Custom Purple Button
# Create a dictionary of the variables you want to override
purple_btn_theme = {
"--bs-btn-bg": "#6f42c1",
"--bs-btn-border-color": "#6f42c1",
"--bs-btn-hover-bg": "#59359a",
"--bs-btn-hover-border-color": "#59359a",
"--bs-btn-color": "#fff"
}
# Pass it to the css_vars argument
Button("Purple Button", variant="primary", css_vars=purple_btn_theme)
2. Standard CSS Classes
You can pass standard Bootstrap utility classes (or your own classes) using cls.
3. Data Attributes & Accessibility
You can pass any standard HTML attribute. FastStrap cleans them up for you (converting python underscore_names to HTML hyphen-names).
Button(
"Menu",
# Data attributes (Python converts underscores to hyphens)
data_bs_toggle="dropdown",
data_custom_id="123",
# ARIA attributes for accessibility
aria_label="Open main menu",
aria_expanded="false"
)
# Renders: <button data-bs-toggle="dropdown" data-custom-id="123" aria-label="..." ...>
Common "Recipes"
The "Submit & Reset" Toolbar
A common pattern for forms, aligning buttons to the right or left.
The "Destructive Action"
For actions that can't be undone, use outline-danger to warn the user but not catch the eye too much (to avoid accidental clicks), but switch to solid danger for the confirmation.
Parameter Reference
This table maps every FastStrap specific parameter to what it actually does in HTML/Bootstrap.
| FastStrap Param | Type | Bootstrap Class / Attribute | Description |
|---|---|---|---|
variant |
str |
.btn-{variant} |
Color theme. Options: primary, secondary, success, danger, warning, info, light, dark, link. |
outline |
bool |
.btn-outline-{variant} |
If True, renders outline style instead of solid fill. |
size |
str |
.btn-{size} |
Size of button. Options: sm (Small), lg (Large). Default is Medium. |
full_width |
bool |
.w-100 |
Makes button span full width of parent. |
pill |
bool |
.rounded-pill |
Gives button fully rounded corners. |
as_ |
str |
<tag> |
Tag to render. Default button. Use a for links. |
href |
str |
href="..." |
URL destination (requires as_="a"). |
disabled |
bool |
disabled / .disabled |
Disables interactivity and applies disabled styling. |
active |
bool |
.active |
Forces the button to appear in a "pressed" state. |
icon |
str |
<i class="bi bi-{icon}"> |
Adds a Bootstrap Icon (e.g., "check", "house"). |
icon_pos |
str |
- | Position of icon: start (default) or end. |
spinner |
bool |
.spinner-border |
Adds a loading spinner. |
loading |
bool |
- | Helper that enables spinner and disabled state together. |
loading_text |
str |
- | Text to display when loading=True. |
css_vars |
dict |
style="--var: val" |
Dict of CSS variables to apply inline. |
faststrap.components.forms.button.Button(*children, as_='button', variant=None, size=None, outline=False, disabled=False, loading=False, spinner=True, loading_text=None, icon=None, icon_pos='start', icon_cls=None, spinner_pos='start', spinner_cls=None, full_width=False, active=False, pill=False, css_vars=None, style=None, **kwargs)
Bootstrap Button component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
Any
|
Button content (text, elements) |
()
|
as_
|
Literal['button', 'a']
|
Render as 'button' or 'a' tag (default: 'button') |
'button'
|
variant
|
VariantType | None
|
Bootstrap color variant |
None
|
size
|
SizeType | None
|
Button size (sm, lg) |
None
|
outline
|
bool
|
Use outline style |
False
|
disabled
|
bool
|
Disable button |
False
|
loading
|
bool
|
Show loading spinner and disable |
False
|
spinner
|
bool
|
Show spinner when loading |
True
|
loading_text
|
str | None
|
Text to show when loading |
None
|
icon
|
str | None
|
Bootstrap icon name |
None
|
icon_pos
|
Literal['start', 'end']
|
Icon position ('start' or 'end') |
'start'
|
icon_cls
|
str | None
|
Custom classes for icon |
None
|
spinner_pos
|
Literal['start', 'end']
|
Spinner position ('start' or 'end') |
'start'
|
spinner_cls
|
str | None
|
Custom classes for spinner |
None
|
full_width
|
bool
|
Make button 100% width. Note: Can also be achieved via |
False
|
active
|
bool
|
Set active state. Note: Can also be achieved via |
False
|
pill
|
bool
|
Use rounded-pill style. Note: Can also be achieved via |
False
|
css_vars
|
dict[str, Any] | None
|
Custom CSS variables dictionary |
None
|
style
|
dict[str, Any] | str | None
|
Custom inline style |
None
|
**kwargs
|
Any
|
Additional HTML attributes (cls, id, hx-, data-, etc.) |
{}
|
Source code in src/faststrap/components/forms/button.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 209 210 211 212 213 214 215 216 217 218 219 | |