Primary & Secondary Button
A button hierarchy with clear hover, focus, pressed, loading and disabled states.
AI prompt
Create a button set with a primary (filled accent), secondary (1px border, transparent) and a disabled primary. All have 44px height, 14px horizontal padding of 20px, 12px radius, 600 weight. Primary hover lightens the fill; pressed darkens it and scales to 0.98 for 90ms. Focus-visible shows a 2px accent ring with 2px offset. Clicking the primary enters a loading state for 1.4 seconds: replace the label with a 16px CSS spinner (700ms linear), keep the button's width fixed by setting min-width from its measured width, set aria-busy="true" and ignore further clicks, then restore and briefly show a checkmark label "Saved". Disabled buttons use the disabled attribute at 40% opacity. Under 480px the buttons stack and fill the width. Under prefers-reduced-motion drop the scale and show "Saving…" text instead of a rotating spinner.About this pattern
Definition
Two levels of button used together: a filled primary button for the single most important action in a view, and an outlined secondary button for alternatives. Both share size, radius and states so they read as one family.
Purpose & UX principles
- One primary action per view keeps the decision obvious.
- The secondary button offers a lower-commitment path such as Cancel or Learn more.
- Use verbs in labels ("Save changes", not "OK").
- A loading state prevents double submits and tells the user the click was received.
Interaction states
- Default, hover (brighter), pressed (darker, scale 0.98), focus-visible (2px ring with offset).
- Loading: label replaced by a spinner, width locked to avoid layout shift,
aria-busy="true". - Disabled: 40% opacity and
disabledattribute, no hover effect.
Motion specification
- Colour and shadow: 160ms ease.
- Press: transform scale(0.98), 90ms.
- Spinner: 700ms linear rotation.
- Reduced motion: colour changes stay, scale and spin are replaced by a static ellipsis.
Responsive behaviour
- Minimum height 44px for touch, 36px acceptable only on dense desktop layouts.
- On phones, primary and secondary stack vertically and fill the width, with the primary on top.
Code
<div class="row">
<button class="btn primary" id="save">Save changes</button>
<button class="btn secondary">Cancel</button>
<button class="btn primary" disabled>Disabled</button>
</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}
.row{display:flex;gap:12px;flex-wrap:wrap;justify-content:center}
.btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;height:44px;padding:0 20px;border-radius:12px;font:inherit;font-size:14px;font-weight:600;cursor:pointer;border:1px solid transparent;transition:background .16s,box-shadow .16s,transform .09s}
.btn:active:not(:disabled){transform:scale(.98)}
.btn:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.btn:disabled{opacity:.4;cursor:not-allowed}
.primary{background:var(--accent);color:var(--accent-ink)}
.primary:hover:not(:disabled){background:#3a3a3a;box-shadow:0 6px 18px rgba(0,0,0,.18)}
.primary:active:not(:disabled){background:#000}
.secondary{background:transparent;color:var(--text);border-color:var(--line)}
.secondary:hover{background:var(--surface-2)}
.spin{width:16px;height:16px;border-radius:50%;border:2px solid rgba(255,255,255,.3);border-top-color:var(--accent-ink);animation:r .7s linear infinite}
@keyframes r{to{transform:rotate(360deg)}}
@media (max-width:480px){.row{flex-direction:column}.btn{width:100%}}
@media (prefers-reduced-motion:reduce){.btn{transition:none}.btn:active:not(:disabled){transform:none}.spin{animation:none;border-top-color:rgba(255,255,255,.3)}}const save=document.getElementById('save');
save.addEventListener('click',()=>{
if(save.getAttribute('aria-busy')==='true')return;
const label=save.textContent,w=save.offsetWidth;save.style.minWidth=w+'px';save.setAttribute('aria-busy','true');
save.innerHTML='<span class="spin" aria-hidden="true"></span><span class="sr" style="position:absolute;left:-9999px">Saving</span>';
setTimeout(()=>{save.removeAttribute('aria-busy');save.textContent='✓ Saved';setTimeout(()=>{save.textContent=label;save.style.minWidth='';},1100);},1400);
});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>Primary & Secondary 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}}
.row{display:flex;gap:12px;flex-wrap:wrap;justify-content:center}
.btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;height:44px;padding:0 20px;border-radius:12px;font:inherit;font-size:14px;font-weight:600;cursor:pointer;border:1px solid transparent;transition:background .16s,box-shadow .16s,transform .09s}
.btn:active:not(:disabled){transform:scale(.98)}
.btn:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.btn:disabled{opacity:.4;cursor:not-allowed}
.primary{background:var(--accent);color:var(--accent-ink)}
.primary:hover:not(:disabled){background:#3a3a3a;box-shadow:0 6px 18px rgba(0,0,0,.18)}
.primary:active:not(:disabled){background:#000}
.secondary{background:transparent;color:var(--text);border-color:var(--line)}
.secondary:hover{background:var(--surface-2)}
.spin{width:16px;height:16px;border-radius:50%;border:2px solid rgba(255,255,255,.3);border-top-color:var(--accent-ink);animation:r .7s linear infinite}
@keyframes r{to{transform:rotate(360deg)}}
@media (max-width:480px){.row{flex-direction:column}.btn{width:100%}}
@media (prefers-reduced-motion:reduce){.btn{transition:none}.btn:active:not(:disabled){transform:none}.spin{animation:none;border-top-color:rgba(255,255,255,.3)}}
</style></head>
<body><div class="stage">
<div class="row">
<button class="btn primary" id="save">Save changes</button>
<button class="btn secondary">Cancel</button>
<button class="btn primary" disabled>Disabled</button>
</div>
</div>
<script>
const save=document.getElementById('save');
save.addEventListener('click',()=>{
if(save.getAttribute('aria-busy')==='true')return;
const label=save.textContent,w=save.offsetWidth;save.style.minWidth=w+'px';save.setAttribute('aria-busy','true');
save.innerHTML='<span class="spin" aria-hidden="true"></span><span class="sr" style="position:absolute;left:-9999px">Saving</span>';
setTimeout(()=>{save.removeAttribute('aria-busy');save.textContent='✓ Saved';setTimeout(()=>{save.textContent=label;save.style.minWidth='';},1100);},1400);
});
</script>
</body></html>