Buttons
Learn how to style buttons, add icons, and use buttons well.
Button Structure
Every button must have a .btn class followed by size, and style classes.
Simple Button
Note, that the button text should be wrapped in a <span> element for a correct styling.
View codeHide code
<button type="button" class="btn md primary">
<span>Simple Button</span>
</button>Link Button
Note, that the button text should be wrapped in a <span> element for a correct styling.
View codeHide code
<a href="#link-button" class="btn md primary">
<span>Link Button</span>
</a>Button with Icon
Place the icon before the <span>. Don't add size or color classes to the icon: the button sets them.
View codeHide code
<button type="button" class="btn md primary">
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="m20.72 7.03-1.83 1.83-3.75-3.75 1.83-1.83a.95.95 0 0 1 .7-.28c.28 0 .52.1.7.28l2.35 2.35c.19.18.28.42.28.7s-.1.51-.28.7zm-16.72 9.22 10.06-10.06 3.75 3.75-10.06 10.06h-3.75z" fill-rule="evenodd">
</path>
</svg>
<span>Edit Profile</span>
</button>Icon on the Right
Place the icon after the <span>, e.g. an arrow that leads to the next step. A button that opens a menu has the arrow-drop-down icon after the label and a .btn-dropdown class, with aria-haspopup="menu" and aria-expanded.
View codeHide code
<button type="button" class="btn md secondary">
<span>Next</span>
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M3 12a1 1 0 0 1 1-1h13.586l-5.293-5.293a1 1 0 0 1 1.414-1.414l7 7a1 1 0 0 1 0 1.414l-7 7a1 1 0 0 1-1.414-1.414L17.586 13H4a1 1 0 0 1-1-1z" fill-rule="evenodd"/>
</svg>
</button>
<!-- Menu trigger -->
<button type="button" class="btn md secondary btn-dropdown" aria-haspopup="menu" aria-expanded="false">
<span>Sort by</span>
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M8.315 9c-.69 0-1.066.798-.625 1.323l3.685 4.387a.817.817 0 0 0 1.25 0l3.685-4.387c.441-.525.065-1.323-.625-1.323z"/>
</svg>
</button>Icon-Only Button
An icon-only button is square: 40×40px for .md, 32×32px for .sm and 28×28px for .xs. Give it a label for screen readers: a <span class="visually-hidden"> before the icon, or an aria-label on the button. A border (.ba) makes it 1px wider than it is tall, whichever label you use: the height is fixed, but the border adds to the width. Use icon-only buttons only for icons people recognize, such as edit, download, more or close.
View codeHide code
<button type="button" class="btn md borderless">
<span class="visually-hidden">More options</span>
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M12 10c.53 0 .998.201 1.403.605.406.403.609.868.609 1.395s-.203.992-.609 1.395c-.405.404-.873.605-1.403.605s-.998-.201-1.403-.605c-.406-.403-.609-.868-.609-1.395s.203-.992.609-1.395C11.002 10.201 11.47 10 12 10zm6.988 0c.53 0 .998.201 1.403.605.406.403.609.868.609 1.395s-.203.992-.609 1.395c-.405.404-.873.605-1.403.605s-.998-.201-1.403-.605c-.406-.403-.609-.868-.609-1.395s.203-.992.609-1.395c.405-.404.873-.605 1.403-.605zM5.012 10c.53 0 .998.201 1.403.605.406.403.609.868.609 1.395s-.203.992-.609 1.395c-.405.404-.873.605-1.403.605s-.998-.201-1.403-.605C3.203 12.992 3 12.527 3 12s.203-.992.609-1.395c.405-.404.873-.605 1.403-.605z" fill-rule="evenodd"/>
</svg>
</button>Sizes
Every button needs a size class and a style class. The examples below use .primary; any style class works with any size. The size class also sets the size of the icon.
| Classes | Height | Icon size | Result |
|---|---|---|---|
.btn.lg.primary |
48px | 24px | |
.btn.md.primary |
40px | 20px | |
.btn.sm.primary |
32px | 18px | |
.btn.xs.primary |
28px | 16px | |
.btn.md.primary.w-100 |
40px | 20px |
Styles
The examples below use .md; any size class works with any style.
Add .active to mark the selected or current button, e.g. the open view in a row of buttons. Add .pressed to keep the pressed look, e.g. while the button's menu is open; buttons look pressed by themselves while they are clicked. A dash means the style has no such state.
| Classes | Default | Active (.active) |
Pressed (.pressed) |
|---|---|---|---|
.btn.md.primary |
None | ||
.btn.md.secondary |
|||
.btn.md.borderless |
|||
.btn.md.borderless.secondary |
|||
.btn.md.danger |
None | ||
.btn.md.warning |
None | ||
.btn.md.success |
None | ||
.btn.md.link |
None | ||
.btn.md.inverted |
None | None | |
.btn.md.inverted.secondary |
None | ||
.btn.md.inverted.link |
None | None |
Loading and Disabled
These states look the same on every style. To disable a <button>, use the disabled attribute. A link can't be disabled with an attribute: add .disabled and aria-disabled="true", and remove the href. A loading button is also disabled, so it can't be clicked twice.
| State | Add to the button | Result |
|---|---|---|
| Loading | .loading + disabled |
|
| Disabled | disabled |
Best Practices
Do
- Give every button three classes:
.btn, a size (.lg,.md,.smor.xs) and a style (.primary,.secondary,.borderless…). - Wrap the label in a
<span>. - Use
<button type="button">for actions and<a href>for links to other pages. - Use one primary button per page, dialog or group of actions. Use secondary or borderless buttons for the other actions.
- Use
.dangerfor actions that delete or can't be undone. - Give icon-only buttons a label for screen readers: a
<span class="visually-hidden">or anaria-label. - To make a button full-width on phones only, add
.w-100.w-sm-auto.
Avoid
- A
.btnwithout a style class: it has no colors. A button without a size class doesn't get a height and padding from the stylesheet. - Size or color classes (
.size-*,.color-*,.text-*) on the button, its icon or its label. The button's size and style classes set the height, text size, icon size (24, 20, 18 or 16px) and colors. The exception is.text-baseon the label of a selected secondary tab or segmented control option. - Label text outside a
<span>, or wrapped in another element. - Icon-only buttons without a label. Screen readers then announce only “button”.
- Inverted buttons on a light background. They are made for images and dark backgrounds.
- Several primary buttons next to each other.
- A
<div>or<span>with button classes. It can't be reached with the keyboard.