---
url: "https://web.dev/articles/building/a-fab-component"
titulek: "Building a floating action button (FAB) component | Articles | web.dev"
h1: "Building a floating action button (FAB) component Stay organized with collections Save and categorize content based on your preferences."
popis: "A foundational overview of how to build color-adaptive, responsive, and accessible FAB components."
jazyk: "en"
kanonicka: "https://web.dev/articles/building/a-fab-component"
drobecky: ["Articles", "Building a floating action button (FAB) component"]
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-14 01:47:42"
znaku: 11131
slov: 1769
kodovani: "iso-8859-2"
sha256: "1edf2e2a163f2d3cc8064db8308079b11338c77cb383a7630dc51b7f5cd6bb7c"
---
Building a floating action button (FAB) component
Stay organized with collections
Save and categorize content based on your preferences.
A foundational overview of how to build color-adaptive, responsive, and accessible FAB components.
Adam Argyle
In this post I want to share my thoughts on how to build color-adaptive,
responsive, and accessible FAB components. [Try the
demo](https://gui-challenges.web.app/FAB/dist/) and [view the
source](https://github.com/argyleink/gui-challenges)! If you prefer video, here's a YouTube version of this post: Overview FABs are more common on mobile than desktop, but they're prevalent in both
scenarios. They keep primary actions in view, making them convenient and
omnipresent. This user experience style was made famous by Material UI and their
suggestions for usage and placement can be [found
here](https://material.io/components/buttons-floating-action-button). Elements and styles The HTML for these controls involves a container element and a set of one or
more buttons. The container positions the FABs within the viewport and manages a
gap between the buttons. The buttons can be mini or default, giving some nice
variety between primary and secondary actions. FAB container This element can be a regular `
` but let's do our unsighted users a favor
and tag it with some helpful attributes to explain the purpose and contents of
this container. FABs markup Start with a `.fabs` class for CSS to hook into for style, then add
`role="group"` and `aria-label` so it's not just a generic container, it's
named and purposeful. `
` FABs style In order for FABs to be convenient they stick within the viewport at all times.
This is a great use case for position
[`fixed`](https://developer.mozilla.org/docs/Web/CSS/position). Within this
viewport position I chose to use
[`inset-block`](https://developer.mozilla.org/docs/Web/CSS/inset-block) and
[`inset-inline`](https://developer.mozilla.org/docs/Web/CSS/inset-inline) so the
position will compliment the user's document mode, like right-to-left or
left-to-right. Custom properties are also used to prevent repetition and ensure
equal distance from the bottom and side edges of the viewport: `.fabs { --_viewport-margin: 2.5vmin; position: fixed; z-index: var(--layer-1); inset-block: auto var(--_viewport-margin); inset-inline: auto var(--_viewport-margin); }` Next I give the container display
[`flex`](https://developer.mozilla.org/docs/Web/CSS/flex) and change its
layout direction to
[`column-reverse`](https://developer.mozilla.org/docs/Web/CSS/flex-direction).
This stacks the children on top of each other (column) and also reverses their
visual order. This has the effect of making the first focusable element the
bottom element instead of the top, which would be where focus goes normally per
the HTML document. Reversing the visual order unites the experience for sighted
users and keyboard users, as the styling of the primary action as larger than
the mini buttons indicates to sighted users that it's a primary action, and
keyboard users will focus it as the first item in the source. `.fabs { ⌠display: flex; flex-direction: column-reverse; place-items: center; gap: var(--_viewport-margin); }` Centering is handled with
[`place-items`](https://developer.mozilla.org/docs/Web/CSS/place-items), and
[`gap`](https://developer.mozilla.org/docs/Web/CSS/gap) adds space between any
FAB buttons placed in the container. FAB buttons Time to style some buttons to look like they're floating over top of everything. Default FAB The first button to style is the default button. This will serve as the base for
all the FAB buttons. Later we'll create a variant that achieves an alternative
appearance while modifying as little of these base styles as possible. FAB markup The `