Button
A customizable button built on Bootstrap with variants, sizes, icons and a CSS loading state.
- Version 1.2.0
- Bootstrap 5.3+
- Requires Bootstrap CSS
- Source on GitHub
html
<div class="d-flex flex-wrap gap-2">
<button type="button" class="btn bootcn-button bootcn-button--default">Default</button>
<button type="button" class="btn bootcn-button bootcn-button--secondary">Secondary</button>
<button type="button" class="btn bootcn-button bootcn-button--outline">Outline</button>
<button type="button" class="btn bootcn-button bootcn-button--ghost">Ghost</button>
<button type="button" class="btn bootcn-button bootcn-button--link">Link</button>
<button type="button" class="btn bootcn-button bootcn-button--destructive">Destructive</button>
</div>
<div class="d-flex flex-wrap gap-2 align-items-center mt-3">
<button type="button" class="btn bootcn-button bootcn-button--default bootcn-button--sm">
Small
</button>
<button type="button" class="btn bootcn-button bootcn-button--default">Medium</button>
<button type="button" class="btn bootcn-button bootcn-button--default bootcn-button--lg">
Large
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--default bootcn-button--icon"
aria-label="Add item"
>
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
aria-hidden="true"
focusable="false"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
<button
type="button"
class="btn bootcn-button bootcn-button--default mt-3"
aria-busy="true"
disabled
>
<span class="bootcn-button__spinner" aria-hidden="true"></span>
<span>Saving</span>
</button>Installation
bash
npx bootcn add buttonbash
pnpm dlx bootcn add buttonbash
yarn dlx bootcn add buttonbash
bunx --bun bootcn add buttonThe CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.
- Load Bootstrap 5.3 (Bootstrap CSS) and the design tokens.
- Install the components it uses: none.
- Copy the files from Source into your project. With the default
bootcn.jsonthey go here:
| File | Default location |
|---|---|
button.html | components/ui/button.html |
button.css | assets/css/button.css |
Usage
html
<button type="button" class="btn bootcn-button bootcn-button--default">Default</button>
<button type="button" class="btn bootcn-button bootcn-button--outline">Outline</button>
<button type="button" class="btn bootcn-button bootcn-button--destructive">Delete</button>Variants
bootcn-button--default— primary action.bootcn-button--secondary— neutral action.bootcn-button--outline— transparent with a border.bootcn-button--ghost— transparent until hovered.bootcn-button--link— looks like a link.bootcn-button--destructive— dangerous action.
Sizes
bootcn-button--sm— small.bootcn-button--lg— large.bootcn-button--icon— square icon button; always pair with an accessible name (e.g.aria-label).
Loading state
Set aria-busy="true", disable the button, and add a spinner element:
html
<button type="button" class="btn bootcn-button bootcn-button--default" aria-busy="true" disabled>
<span class="bootcn-button__spinner" aria-hidden="true"></span>
<span>Saving</span>
</button>Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css(shared design tokens).
No JavaScript.
Accessibility
- Icon-only buttons must have an accessible name (
aria-labelor visually hidden text); the icon itself isaria-hidden="true". - The focus ring uses the shared focus token and is visible on keyboard focus.
- For
<a>elements used as buttons, addrole="button"when the link does not navigate, and keep the loading pattern to<button>wheredisabledapplies. - Spinner animation is disabled under
prefers-reduced-motion: reduce.
Customizing
Variants set Bootstrap's own --bs-btn-* variables on the element, so Bootstrap's hover, focus, active, open (.show) and disabled rules keep the variant's colors. To make a custom variant, set the same variables:
css
.bootcn-button--brand {
--bs-btn-color: #fff;
--bs-btn-bg: #6f42c1;
--bs-btn-border-color: #6f42c1;
--bs-btn-hover-color: #fff;
--bs-btn-hover-bg: #5a32a3;
--bs-btn-hover-border-color: #5a32a3;
--bs-btn-active-color: #fff;
--bs-btn-active-bg: #4c2a8a;
--bs-btn-active-border-color: #4c2a8a;
--bs-btn-disabled-color: #fff;
--bs-btn-disabled-bg: #6f42c1;
--bs-btn-disabled-border-color: #6f42c1;
}Source
The exact files the CLI installs (button@1.2.0). View on GitHub · Report an issue
html
<div class="d-flex flex-wrap gap-2">
<button type="button" class="btn bootcn-button bootcn-button--default">Default</button>
<button type="button" class="btn bootcn-button bootcn-button--secondary">Secondary</button>
<button type="button" class="btn bootcn-button bootcn-button--outline">Outline</button>
<button type="button" class="btn bootcn-button bootcn-button--ghost">Ghost</button>
<button type="button" class="btn bootcn-button bootcn-button--link">Link</button>
<button type="button" class="btn bootcn-button bootcn-button--destructive">Destructive</button>
</div>
<div class="d-flex flex-wrap gap-2 align-items-center mt-3">
<button type="button" class="btn bootcn-button bootcn-button--default bootcn-button--sm">
Small
</button>
<button type="button" class="btn bootcn-button bootcn-button--default">Medium</button>
<button type="button" class="btn bootcn-button bootcn-button--default bootcn-button--lg">
Large
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--default bootcn-button--icon"
aria-label="Add item"
>
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
aria-hidden="true"
focusable="false"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
<button
type="button"
class="btn bootcn-button bootcn-button--default mt-3"
aria-busy="true"
disabled
>
<span class="bootcn-button__spinner" aria-hidden="true"></span>
<span>Saving</span>
</button>css
/*
* Variants set Bootstrap's own --bs-btn-* variables on the bootcn-button
* element, so every Bootstrap state rule (.btn:hover, :focus-visible, :active,
* .active, .show, :disabled) keeps the variant's colors. Sizes and colors
* follow the Tailwind-like tokens (h-9, text-sm, rounded-md).
*/
.bootcn-button {
--bs-btn-padding-x: 1rem;
--bs-btn-padding-y: 0.5rem;
--bs-btn-font-size: var(--bootcn-control-font-size);
--bs-btn-font-weight: 500;
--bs-btn-line-height: 1.25rem;
--bs-btn-border-width: var(--bootcn-border-width);
--bs-btn-border-radius: var(--bootcn-radius);
--bs-btn-focus-box-shadow: var(--bootcn-focus-ring);
--bs-btn-disabled-opacity: 0.5;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
min-height: var(--bootcn-control-height);
white-space: nowrap;
text-decoration: none;
transition:
background-color var(--bootcn-transition),
border-color var(--bootcn-transition),
color var(--bootcn-transition),
box-shadow var(--bootcn-transition);
}
.bootcn-button:focus-visible {
outline: 0;
}
.bootcn-button[aria-disabled='true'] {
opacity: var(--bs-btn-disabled-opacity);
pointer-events: none;
}
/* Solid variants: --bootcn-button-bg and -fg; hover is the color at 90%
over the page background (Tailwind's bg-primary/90). */
.bootcn-button--default {
--bootcn-button-bg: var(--bootcn-primary);
--bootcn-button-fg: var(--bootcn-primary-foreground);
}
.bootcn-button--secondary {
--bootcn-button-bg: var(--bootcn-secondary);
--bootcn-button-fg: var(--bootcn-secondary-foreground);
}
.bootcn-button--destructive {
--bootcn-button-bg: var(--bootcn-destructive);
--bootcn-button-fg: var(--bootcn-destructive-foreground);
}
.bootcn-button--default,
.bootcn-button--secondary,
.bootcn-button--destructive {
--bootcn-button-hover-bg: var(--bootcn-button-bg);
--bs-btn-color: var(--bootcn-button-fg);
--bs-btn-bg: var(--bootcn-button-bg);
--bs-btn-border-color: var(--bootcn-button-bg);
--bs-btn-hover-color: var(--bootcn-button-fg);
--bs-btn-hover-bg: var(--bootcn-button-hover-bg);
--bs-btn-hover-border-color: var(--bootcn-button-hover-bg);
--bs-btn-active-color: var(--bootcn-button-fg);
--bs-btn-active-bg: var(--bootcn-button-bg);
--bs-btn-active-border-color: var(--bootcn-button-bg);
--bs-btn-disabled-color: var(--bootcn-button-fg);
--bs-btn-disabled-bg: var(--bootcn-button-bg);
--bs-btn-disabled-border-color: var(--bootcn-button-bg);
box-shadow: var(--bootcn-shadow-sm);
}
@supports (color: color-mix(in srgb, red, blue)) {
.bootcn-button--default,
.bootcn-button--secondary,
.bootcn-button--destructive {
--bootcn-button-hover-bg: color-mix(
in srgb,
var(--bootcn-button-bg) 90%,
var(--bootcn-background)
);
}
.bootcn-button--secondary {
--bootcn-button-hover-bg: color-mix(
in srgb,
var(--bootcn-button-bg) 80%,
var(--bootcn-background)
);
}
/* Lightening red would drop white text below 4.5:1, so darken instead. */
.bootcn-button--destructive {
--bootcn-button-hover-bg: color-mix(in srgb, var(--bootcn-button-bg) 88%, #000);
}
}
/* Quiet variants: foreground text, the accent color on hover and when open. */
.bootcn-button--outline,
.bootcn-button--ghost {
--bs-btn-color: var(--bootcn-foreground);
--bs-btn-bg: transparent;
--bs-btn-hover-color: var(--bootcn-accent-foreground);
--bs-btn-hover-bg: var(--bootcn-accent);
--bs-btn-active-color: var(--bootcn-accent-foreground);
--bs-btn-active-bg: var(--bootcn-accent);
--bs-btn-disabled-color: var(--bootcn-foreground);
--bs-btn-disabled-bg: transparent;
}
.bootcn-button--outline {
--bs-btn-bg: var(--bootcn-background);
--bs-btn-border-color: var(--bootcn-input);
--bs-btn-hover-border-color: var(--bootcn-input);
--bs-btn-active-border-color: var(--bootcn-input);
--bs-btn-disabled-bg: var(--bootcn-background);
--bs-btn-disabled-border-color: var(--bootcn-input);
box-shadow: var(--bootcn-shadow-sm);
}
.bootcn-button--ghost {
--bs-btn-border-color: transparent;
--bs-btn-hover-border-color: transparent;
--bs-btn-active-border-color: transparent;
--bs-btn-disabled-border-color: transparent;
}
.bootcn-button--link {
--bs-btn-color: var(--bootcn-primary);
--bs-btn-bg: transparent;
--bs-btn-border-color: transparent;
--bs-btn-hover-color: var(--bootcn-primary);
--bs-btn-hover-bg: transparent;
--bs-btn-hover-border-color: transparent;
--bs-btn-active-color: var(--bootcn-primary);
--bs-btn-active-bg: transparent;
--bs-btn-active-border-color: transparent;
--bs-btn-disabled-color: var(--bootcn-primary);
--bs-btn-disabled-bg: transparent;
--bs-btn-disabled-border-color: transparent;
text-underline-offset: 4px;
}
.bootcn-button--link:hover {
text-decoration: underline;
}
/* Sizes (Tailwind h-8 / h-9 / h-10) */
.bootcn-button--sm {
--bs-btn-padding-y: 0.25rem;
--bs-btn-padding-x: 0.75rem;
--bs-btn-font-size: 0.8125rem;
gap: 0.375rem;
min-height: var(--bootcn-control-height-sm);
}
.bootcn-button--lg {
--bs-btn-padding-y: 0.5rem;
--bs-btn-padding-x: 1.5rem;
min-height: var(--bootcn-control-height-lg);
}
.bootcn-button--icon {
--bs-btn-padding-x: 0;
--bs-btn-padding-y: 0;
width: var(--bootcn-control-height);
min-width: var(--bootcn-control-height);
aspect-ratio: 1 / 1;
}
.bootcn-button--icon.bootcn-button--sm {
width: var(--bootcn-control-height-sm);
min-width: var(--bootcn-control-height-sm);
}
.bootcn-button--icon.bootcn-button--lg {
width: var(--bootcn-control-height-lg);
min-width: var(--bootcn-control-height-lg);
}
.bootcn-button svg {
flex-shrink: 0;
width: 1rem;
height: 1rem;
}
/* Loading state */
.bootcn-button[aria-busy='true'] {
cursor: progress;
}
.bootcn-button__spinner {
width: 1em;
height: 1em;
border: 0.15em solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: bootcn-button-spin 0.6s linear infinite;
}
@keyframes bootcn-button-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.bootcn-button__spinner {
animation: none;
}
}
