Floating Action Button
A circular primary action button that fans out into related actions.
AI prompt
Create a floating action button (56px circle, accent fill, soft shadow) pinned to the bottom-right of a 340x420 demo screen with 16px margins. It shows a plus icon. On click it toggles a speed dial: three labelled mini actions (Note, Photo, Voice) stack upward above it, each as a 40px circular button with a text label chip to its left. The plus icon rotates 45 degrees in 220ms; children animate in with translateY(12px) to 0 and opacity 0 to 1 over 180ms with a 40ms stagger from the nearest. A translucent scrim covers the screen behind the dial and closes it on click. Use aria-expanded and aria-controls on the main button. Escape closes the dial and returns focus to the main button. Respect prefers-reduced-motion by using opacity only.About this pattern
Definition
A prominent circular button that floats above the content in a corner and represents the screen's main action. In this version it expands into a small menu of related actions (a speed dial).
Purpose & UX principles
- Makes the most common creation action reachable from anywhere in a scrolling view.
- Use only one per screen. If everything is prominent, nothing is.
- Speed-dial children must have text labels; icons alone are not enough.
Interaction states
- Closed: plus icon.
- Open: icon rotates 45° to become a cross,
aria-expanded="true", a scrim dims the content. - Children appear in sequence and are reachable by Tab; Escape closes and returns focus to the main button.
Motion specification
- Icon rotation: 220ms ease.
- Children: translateY from 12px and fade, 180ms each, 40ms stagger, from nearest to farthest.
- Reduced motion: fade only, no movement.
Responsive behaviour
- Bottom-right on phones, 16px from the edges, above any bottom navigation.
- On wide layouts consider a labelled "extended" button in the page toolbar instead.
Code
<div class="screen">
<p class="hint">Tap the + button</p>
<div class="scrim" id="scrim"></div>
<div class="dial" id="dial">
<ul class="acts" id="acts">
<li style="--n:2"><span class="chip">Voice</span><button class="mini" aria-label="Voice">🎙</button></li>
<li style="--n:1"><span class="chip">Photo</span><button class="mini" aria-label="Photo">â–£</button></li>
<li style="--n:0"><span class="chip">Note</span><button class="mini" aria-label="Note">✎</button></li>
</ul>
<button class="fab" id="fab" aria-expanded="false" aria-controls="acts" aria-label="Create"><span>+</span></button>
</div>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.screen{position:relative;width:min(340px,100%);height:420px;border:1px solid var(--line);border-radius:24px;background:var(--surface);overflow:hidden}
.hint{text-align:center;color:var(--muted);margin-top:40px;font-size:14px}
.scrim{position:absolute;inset:0;background:rgba(0,0,0,.28);opacity:0;pointer-events:none;transition:opacity .2s}
.scrim.on{opacity:1;pointer-events:auto}
.dial{position:absolute;right:16px;bottom:16px;display:flex;flex-direction:column;align-items:flex-end;gap:12px}
.fab{width:56px;height:56px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font-size:30px;line-height:1;cursor:pointer;box-shadow:0 10px 24px rgba(0,0,0,.18)}
.fab span{display:block;transition:transform .22s ease}
.fab[aria-expanded=true] span{transform:rotate(45deg)}
.fab:focus-visible,.mini:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.acts{list-style:none;display:flex;flex-direction:column;gap:12px;align-items:flex-end}
.acts li{display:flex;align-items:center;gap:10px;opacity:0;transform:translateY(12px);pointer-events:none;transition:opacity .18s,transform .18s;transition-delay:calc(var(--n)*40ms)}
.dial.open .acts li{opacity:1;transform:none;pointer-events:auto}
.chip{background:var(--text);color:var(--bg);font-size:12px;font-weight:600;padding:5px 10px;border-radius:8px}
.mini{width:40px;height:40px;border-radius:50%;border:0;background:var(--surface-2);color:var(--text);font-size:16px;cursor:pointer}
@media (prefers-reduced-motion:reduce){.acts li{transform:none;transition:opacity .18s}.fab span{transition:none}}const dial=document.getElementById('dial'),fab=document.getElementById('fab'),scrim=document.getElementById('scrim');
function set(o){dial.classList.toggle('open',o);scrim.classList.toggle('on',o);fab.setAttribute('aria-expanded',o);}
fab.addEventListener('click',()=>set(!dial.classList.contains('open')));
scrim.addEventListener('click',()=>set(false));
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&dial.classList.contains('open')){set(false);fab.focus();}});
dial.querySelectorAll('.mini').forEach(b=>b.addEventListener('click',()=>set(false)));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Floating Action Button — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.screen{position:relative;width:min(340px,100%);height:420px;border:1px solid var(--line);border-radius:24px;background:var(--surface);overflow:hidden}
.hint{text-align:center;color:var(--muted);margin-top:40px;font-size:14px}
.scrim{position:absolute;inset:0;background:rgba(0,0,0,.28);opacity:0;pointer-events:none;transition:opacity .2s}
.scrim.on{opacity:1;pointer-events:auto}
.dial{position:absolute;right:16px;bottom:16px;display:flex;flex-direction:column;align-items:flex-end;gap:12px}
.fab{width:56px;height:56px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font-size:30px;line-height:1;cursor:pointer;box-shadow:0 10px 24px rgba(0,0,0,.18)}
.fab span{display:block;transition:transform .22s ease}
.fab[aria-expanded=true] span{transform:rotate(45deg)}
.fab:focus-visible,.mini:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.acts{list-style:none;display:flex;flex-direction:column;gap:12px;align-items:flex-end}
.acts li{display:flex;align-items:center;gap:10px;opacity:0;transform:translateY(12px);pointer-events:none;transition:opacity .18s,transform .18s;transition-delay:calc(var(--n)*40ms)}
.dial.open .acts li{opacity:1;transform:none;pointer-events:auto}
.chip{background:var(--text);color:var(--bg);font-size:12px;font-weight:600;padding:5px 10px;border-radius:8px}
.mini{width:40px;height:40px;border-radius:50%;border:0;background:var(--surface-2);color:var(--text);font-size:16px;cursor:pointer}
@media (prefers-reduced-motion:reduce){.acts li{transform:none;transition:opacity .18s}.fab span{transition:none}}
</style></head>
<body><div class="stage">
<div class="screen">
<p class="hint">Tap the + button</p>
<div class="scrim" id="scrim"></div>
<div class="dial" id="dial">
<ul class="acts" id="acts">
<li style="--n:2"><span class="chip">Voice</span><button class="mini" aria-label="Voice">🎙</button></li>
<li style="--n:1"><span class="chip">Photo</span><button class="mini" aria-label="Photo">â–£</button></li>
<li style="--n:0"><span class="chip">Note</span><button class="mini" aria-label="Note">✎</button></li>
</ul>
<button class="fab" id="fab" aria-expanded="false" aria-controls="acts" aria-label="Create"><span>+</span></button>
</div>
</div>
</div>
<script>
const dial=document.getElementById('dial'),fab=document.getElementById('fab'),scrim=document.getElementById('scrim');
function set(o){dial.classList.toggle('open',o);scrim.classList.toggle('on',o);fab.setAttribute('aria-expanded',o);}
fab.addEventListener('click',()=>set(!dial.classList.contains('open')));
scrim.addEventListener('click',()=>set(false));
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&dial.classList.contains('open')){set(false);fab.focus();}});
dial.querySelectorAll('.mini').forEach(b=>b.addEventListener('click',()=>set(false)));
</script>
</body></html>