--- 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 `` FAB style First let's turn the button into a padded round button with a strong shadow, as these are the first defining features of the button: `.fab { --_size: 2rem; padding: calc(var(--_size) / 2); border-radius: var(--radius-round); aspect-ratio: 1; box-shadow: var(--shadow-4); }` Next let's add color. We'll use a strategy we've used in GUI Challenges before. Create a clearly named set of custom properties that statically hold the light and dark colors, then an adaptive custom property that will be set to either the light or the dark variables depending on the user's system preference for colors: `.fab { … /* light button and button hover */ --_light-bg: var(--pink-6); --_light-bg-hover: var(--pink-7); /* dark button and button hover */ --_dark-bg: var(--pink-4); --_dark-bg-hover: var(--pink-3); /* adaptive variables set to light by default */ --_bg: var(--_light-bg); /* static icon colors set to the adaptive foreground variable */ --_light-fg: white; --_dark-fg: black; --_fg: var(--_light-fg); /* use the adaptive properties on some styles */ background: var(--_bg); color: var(--_fg); &:is(:active, :hover, :focus-visible) { --_bg: var(--_light-bg-hover); @media (prefers-color-scheme: dark) { --_bg: var(--_dark-bg-hover); } } /* if users prefers dark, set adaptive props to dark */ @media (prefers-color-scheme: dark) { --_bg: var(--_dark-bg); --_fg: var(--_dark-fg); } }` Next add some styles to help the SVG icons fit the space. `.fab { … & > svg { inline-size: var(--_size); block-size: var(--_size); stroke-width: 3px; } }` Last, remove the tap highlight from the button since we've added our own visual feedback for interaction: `.fab { -webkit-tap-highlight-color: transparent; }` Mini FAB The goal of this section is to create a variant for the FAB button. By making some of the FABs smaller than the default action, we can promote the action the user performs the most often. Mini FAB markup The HTML is the same as a FAB but we add a ".mini" class to give CSS a hook into the variant. `` Mini FAB style Thanks to usage of custom properties, the only change needed is an adjustment to the `--_size` variable. `.fab.mini { --_size: 1.25rem; }` Accessibility The most important part to remember for accessibility with FABs is placement within the keyboard flow of the page. This demo only has the FABs, there's nothing to compete with in terms of keyboard order and flow, which means it doesn't have an opportunity to demonstrate a meaningful keyboard flow. In a scenario where there's competing elements for focus, I suggest thinking deeply about where in that flow should a user find themselves entering into the FAB button flow. Keyboard interaction demonstration Once the user has focused into the FAB container, we've already added `role="group"` and `aria-label="floating action buttons"` which inform screen reader users about the contents of what they have focused. Strategically I've placed the default FAB first, so that users find the primary action first. I then use `flex-direction: column-reverse;` to visually order the primary button on the bottom, close to the users fingers for easy access. This is a nice win because the default button is visually prominent and also first for keyboard users, giving them very similar experiences. Lastly, don't forget to hide your icons from screen reader users and ensure you provide them with a label for the button so it's not a mystery. This has been done in the HTML already with `aria-hidden="true"` on the `` and `aria-label="Some action"` on the `