Skip to main content
Login

Sign in to access your account

Floating Action Button - National Design System

A floating action button (FAB) stays at an edge of the screen while the page scrolls, for a main action that the user needs on every part of the page.

Overview

A FAB is a Button, or a button group, with the nds-fab class. The script moves it out of the page into a dock at the left, right or bottom edge of the screen. FABs on the same edge stack in one dock. A FAB usually opens a Panel with data-panel-toggle.

Pick another component when:

  • the action belongs to one place in the content: Button
  • the actions fit in a menu beside a button: Dropmenu
  • the actions sit above a table or a list: Toolbar

Markup

Options
Structure
Edge
Variant
Size
Shape
Direction
Gap
More
Preview
<button type="button" class="nds-btn nds-fab nds-primary nds-circle nds-icon-only" data-panel-toggle="fab-panel" aria-label="Open panel" hidden> <i class="hgi hgi-stroke hgi-menu-01"></i> </button> <dialog id="fab-panel" class="nds-panel" aria-label="Details"> <div class="nds-panel-header"> <div class="nds-panel-text"> <span class="nds-panel-title">Details</span> </div> <div class="nds-panel-action"> <button type="button" class="nds-btn nds-subtle nds-icon-only" data-panel-close aria-label="Close panel" data-i18n-aria-label="panel.close_panel"> <i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i> </button> </div> </div> <div class="nds-panel-body"> <div class="nds-content-placeholder"> <span>Swap with content component</span> <span>استبدل هذا العنصر بأي عنصر آخر</span> </div> </div> </dialog>

Behavior

Edge

The Edge options set the side of the panel, and the FAB follows it. To place a FAB on an edge of its own, write data-fab-side. start and end follow the reading direction. left, right and bottom stay on that edge in every language. If its panel is not on the page yet, the FAB reads data-panel-side from its toggle button. A FAB with no data-fab-side and no side, or with a top sheet, docks on the end edge.

Thumb

nds-fab-thumb puts the FAB against the screen edge. The corners on that side turn square and the border there goes away, so it looks like a tab. When its panel slides in from the same edge, the thumb moves aside with the panel and comes back when the panel closes. On the bottom edge, it sits on the bottom of the screen and rises with the sheet. A thumb with an explicit data-fab-side moves only when that edge is the panel’s edge.

Group

A button group with nds-fab docks as one item, with one shadow around the whole group. Put data-panel-toggle on one of its buttons: the group docks on that panel’s edge, and with nds-fab-thumb the whole group moves aside. Style it as any button group: the variant and size go on its buttons. Only the outer nds-fab docks: a button inside it with nds-fab stays in the group.

Gap

data-fab-gap adds space between a FAB and the one before it in the dock, on top of the dock’s own gap. Use it to split the FABs on one edge into sets, such as the page actions and a chat button. On a side edge the space goes below the FAB, and on the bottom edge to its left. The FAB with the gap is the first of its set, so give it the lowest data-fab-order in that set.

Built-in Features

  • Auto-initialization

    The script moves every nds-fab to its dock after the page shows. There is no dock to write and nothing to call.

  • Fixed Edge Docks

    There are three docks: left, right and the middle of the bottom. A start or end FAB lands in the left or the right dock, so two FABs on one edge always share a dock.

  • Direction-Aware Routing

    When the page direction changes, a start or end FAB moves to the other side, and so does a FAB that follows its panel. A left or right FAB stays.

  • Panel Following

    A FAB with no data-fab-side docks on the edge of the panel it opens. Change the panel's side and the FAB follows.

  • Automatic Stacking

    FABs on one edge stack, ordered by data-fab-order. On a side edge, the lowest number is at the bottom. On the bottom edge, they sit in a row from left to right.

  • Page-End Tuck

    Near the end of a page that scrolls, the docks slide out past their edge, so a FAB never covers the footer. They also slide out while a hero fills the bottom of the screen.

  • Mixed Shapes and Sizes

    Circles, thumbs and groups of any size share a dock. The dock lines them up on its edge and keeps each one's size and shape.

  • Floating Look

    A docked FAB is 16px larger than the same button in the page (8px on a phone), and it casts a shadow. Secondary, secondary outline and subtle get a solid fill, so the page does not show through.

  • Programmatic Control

    A FAB made by a script docks with one call. NDS.Fab.destroy() puts a FAB back in its view before an app removes that view.

Best Practices

  • Keep FABs few: one for the main action, such as add, compose, filters or chat. Put more actions in a Panel that the FAB opens.
  • Write the hidden attribute on every FAB. The script removes it once the FAB is in its dock, so the FAB never shows at the place you wrote it.
  • Leave out data-fab-side on a FAB that opens a panel, so the FAB stays on the panel’s edge.
  • Use start and end so the edge follows the reading direction. Use left or right only for an action that must stay on one side in every language.
  • Give the main action the lowest data-fab-order, so it sits nearest the edge.
  • Give an icon-only FAB an aria-label that names the action.
  • Do not write the square corners of a thumb yourself. The dock sets them from its edge, so they stay right when the FAB moves.
  • In an app that swaps views, call NDS.Fab.destroy(view) or NDS.init.destroy(view) before you remove a view. A FAB lives in a dock on <body>, so it stays on the screen after its view is gone.

API

Classes

Class Element Effect
nds-fab .nds-btn or .nds-btn-group Makes it a FAB: the script moves it to its dock
nds-fab-dock div A dock. The script makes one for each edge in use, at the end of <body>. To place a plain button at an edge, write a dock yourself with data-fab-dock-side and put the button in it. The script moves only buttons with nds-fab

Data Attributes

Attribute Element Effect
data-fab-side .nds-fab The edge: auto (the default), start, end, left, right or bottom. See Edge under Behavior
data-fab-order .nds-fab The place in the stack. A lower number sits nearer the edge. The default is 0
data-fab-gap .nds-fab Extra space between the FAB and the one before it: sm (8px), md (16px) or lg (32px). See Gap under Behavior
data-panel-toggle .nds-fab, or a button in a group The id of the Panel it opens. The FAB docks on that panel’s edge
data-fab-dock-side .nds-fab-dock The edge of a dock you write yourself: left, right (the default) or bottom
data-fab-riding .nds-fab-thumb The script writes it while the thumb moves aside for its panel
data-fab-tucked <html> The script writes it while the docks slide out, near the page end or over a hero

CSS Custom Properties

The script makes the docks, so set the dock properties on :root, or on a dock you write yourself. Set --fab-gap on the FAB.

Property Default Controls
--fab-dock-offset calc(var(--nds-viewport-padding) / 2) Space between a dock and the screen edges: half the page gutter. On a touch screen, a left or right dock sits twice this far from the bottom, clear of the rounded screen corners
--fab-dock-gap var(--spacing-md) Space between FABs in a dock
--fab-gap set by data-fab-gap Extra space of a FAB with data-fab-gap. Any value replaces the size
--fab-dock-z 899 Stack order of the docks. Panels (999) and modals sit above them

Tokens

It reads these shared tokens. A change to one also changes other components: see Tokens.

Source: the var() reads in _sass/components/_fab.scss.

TokenPreviewValue
--background-default
Read by box-shadow, --btn-bg, --btn-bg-disabled

var(--colors-base-white)
Dark mode var(--colors-neutral-800)
--border-neutral-secondary
Read by --btn-border, --btn-border-disabled

var(--colors-neutral-200)
Dark mode var(--colors-neutral-700)
--button-background-secondary-default
Read by --btn-bg, --btn-bg-disabled

var(--colors-neutral-100)
Dark mode var(--colors-neutral-700)
--button-background-secondary-hovered
Read by --btn-bg-hover

var(--colors-neutral-100)
Dark mode var(--colors-neutral-700)
--button-background-secondary-pressed
Read by --btn-bg-active

var(--colors-neutral-200)
Dark mode var(--colors-neutral-600)
--button-background-secondary-selected
Read by --btn-bg-selected

var(--colors-neutral-200)
Dark mode var(--colors-neutral-600)

JavaScript

The NDS.Fab script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

A FAB in the page docks by itself. Call the API for a FAB that a script makes after the page loads.

Method Effect
NDS.Fab.init() Docks every nds-fab that is not docked yet
NDS.Fab.register(fab, edge) Docks one FAB now and returns its dock. edge is optional and replaces the FAB’s data-fab-side
NDS.Fab.destroy(fab) Puts a FAB back where it was written, hidden again. Pass a container to do this for every FAB written inside it. Returns the count
NDS.Fab.getDock(edge) Returns the dock at left, right or bottom, and makes it if it is missing
NDS.Fab.resolvePosition(fab) Returns the edge the FAB docks on: left, right or bottom

A FAB fires no events.

// A FAB made after the page loads var fab = document.createElement('button'); fab.type = 'button'; fab.className = 'nds-btn nds-fab nds-primary nds-circle nds-icon-only'; fab.setAttribute('aria-label', 'Add request'); fab.innerHTML = '<i class="hgi hgi-stroke hgi-plus-sign"></i>'; NDS.Fab.register(fab, 'end'); // Before an app removes a view, put its FABs back in it NDS.Fab.destroy(view);

The full API is in the banner of _js/nds-fab.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks