Ever pressed a lift button that didn't light up? You press it again, and again, even though the lift was called a long time ago. Look at that button panel again. The floor buttons are arranged neatly, and the emergency button is set apart. Every button is big enough for a finger, and its label says what happens. A pressed button lights up right away and stays lit until the lift arrives. If it doesn't light up, people press it again and again.
A button on a web page makes the same promise: "press me, and this happens". A CTA (call to action) is the button for the page's main action. On Mrs. Sri's page, the CTA is "Book the room, pay on arrival".
Hierarchy: one primary per screen
From the Color Priority lesson: every screen has one main action, and the other actions differ in visual weight (how strongly a button pulls the eye).
| Level | Look | On Mrs. Sri's booking card |
|---|---|---|
| Primary (main) | solid green #15803d, white text (5.02:1) |
"Book the room, pay on arrival" |
| Secondary (second) | green border and text, white background | "Ask Mrs. Sri on WhatsApp" |
| Tertiary (ghost) | text only, no background or border | "See the full cancellation policy" |
The tertiary level is often called ghost because it has no body, only text. So the three levels are: primary, secondary, ghost (or tertiary).
In the exercise, the same three levels use blue:
.btn-primarywithbackground-color: #2563EB,color: white,font-weight: 600(white on blue 5.17:1)..btn-secondarywith#2563EBtext on a transparent background and a1px solid #2563EBborder..btn-ghostwith grey#6B7280text (4.83:1), a transparent background, and no border.
Six states
The same button looks different in each state. The code below gives each state its own look. The comment at the end of each line names the state:
.btn { padding: 11px 16px; font: inherit; font-weight: 600; line-height: 24px; border: 1px solid transparent; border-radius: 8px; cursor: pointer; }
.btn-primary { color: #fff; background-color: #15803d; } /* default */
.btn-primary:hover { background-color: #166534; } /* mouse over it */
.btn-primary:active { background-color: #14532d; } /* being pressed */
.btn:focus-visible { outline: 3px solid #1f2937; outline-offset: 2px; } /* keyboard focus */
.btn-primary:disabled { color: #475569; background-color: #e2e8f0; cursor: not-allowed; } /* disabled */
.btn-primary[aria-busy="true"] { background-color: #14532d; cursor: progress; } /* loading */
- Default: the resting state. Hover: darker while the mouse is over it (phones have no hover). Active: darkest while pressed.
- Focused: a 3px ring when the button is reached with the Tab key. Never remove it with
outline: none. - Disabled: grey, not pale green, because it can't be pressed yet (
#475569on#e2e8f0, 6.15:1). Its cursor becomes a "not allowed" sign (cursor: not-allowed). Adisabledbutton can't be reached with Tab either, so write the reason next to it: "Choose the number of nights first". - Loading: after the press, until the server answers. The button is marked with
aria-busy="true", which means "busy", and its cursor becomes a loading sign (cursor: progress).
Why does loading matter? Mr. Hadi has a weak signal. He presses "Book the room", and for 1.5 seconds the button looks exactly the same, so he presses it once more. How many bookings reach Mrs. Sri? Guess first; the answer comes after the code below.
The first five states (default, hover, active, focused, disabled) are required for every button. Loading is required for a button that sends something. This JavaScript runs when the book button (.book) is clicked:
const book = document.querySelector(".book");
book.addEventListener("click", async () => {
if (book.getAttribute("aria-busy") === "true") return; // a second tap is ignored
book.setAttribute("aria-busy", "true");
book.textContent = "Booking...";
await sendBooking(); // send to the server
book.removeAttribute("aria-busy");
book.textContent = "Book the room, pay on arrival";
});
- The button changes to "Booking..." right away, so Mr. Hadi knows his tap was received.
await sendBooking()waits until the server answers. During that time the button carriesaria-busy="true", and theifline at the start makes the code ignore any further taps. In the Lab, two quick taps still make one booking.- The button is as wide as the card, so its width doesn't change when its text gets shorter. A button that shrinks while loading moves everything around it.
Without a loading state, the button looks the same. Mr. Hadi presses again, and every tap sends one booking. So the answer to the guess: two bookings, for the same nights.
The code above still has one gap. If sendBooking() fails, for example because Mr. Hadi loses his signal, the two lines after await never run. The button stays at "Booking..." with aria-busy="true", and every later tap is ignored. The fix: wrap the sending in try, catch and finally.
book.addEventListener("click", async () => {
if (book.getAttribute("aria-busy") === "true") return;
book.setAttribute("aria-busy", "true");
book.textContent = "Booking...";
try {
await sendBooking();
} catch {
document.querySelector("#error").textContent = "Booking not sent yet. Try again.";
} finally {
book.removeAttribute("aria-busy");
book.textContent = "Book the room, pay on arrival";
}
});
try wraps the line that can fail. catch runs if the sending fails, and writes an error message in #error, the paragraph below the button. finally runs whether the sending worked or failed. So the button always returns to normal and can be pressed again. The Feedback & States lesson later uses the same pattern.
Size and touch targets
Button height = top padding + line height + bottom padding + top and bottom border. These are measured with line-height: 24px (the height of each line) and a 1px border. The result is the same in Chromium, WebKit and Firefox:
| Size | Top and bottom padding | Height | Suits |
|---|---|---|---|
| Small | 3px | 32px | small actions in a table row, on a laptop |
| Medium | 7px | 40px | forms on a laptop |
| Large | 11px | 48px | CTAs, and every button on a phone |
The touch target limits from the Density & Scale lesson: WCAG 2.5.8 at least 24 × 24px (level AA), Apple 44 × 44pt, Material 48 × 48dp. On a phone, use 48px. Mrs. Sri's book button is 48px tall and as wide as the card (311px on a 375px phone), so it's easy to press with a thumb.
Labels: a verb first
A button's label (the text on it) says what happens when it's pressed, starting with a verb:
| Unclear | Clearer | Why |
|---|---|---|
| Submit, OK | Book the room | the guest knows what happens: the room gets booked |
| Click here | Ask Mrs. Sri on WhatsApp | it names where it goes |
| Yes / No | Cancel booking / Go back | each label makes sense without the question |
Short is good (1 to 3 words), but clear matters more. "Book the room, pay on arrival" is longer than "Book", but this label also answers the guest's worry: no need to pay now.
Choose the element from what happens. Use <button> for an action on this page (booking). Use <a href> (a link) for going somewhere else (opening WhatsApp, opening the cancellation policy page), even when it looks like a button.
⚠️ Buttons that leave guests unsure
- Three solid buttons. None is seen first. One primary, the rest secondary and ghost.
- No loading state. The guest presses twice, and the booking is sent twice.
- No
finally. If the sending fails, the button stays at "Booking..." and cannot be pressed again. - A pale green disabled button with no reason. It still looks pressable, and the guest doesn't know why it isn't. Grey, plus the reason in text.
- A 32px button on a phone. It meets WCAG's 24px, but it's easy to miss. Use 48px.
- A "Submit" or "OK" label. Say the action: "Book the room".
TL;DR: The button hierarchy has three levels: primary (solid, one per screen), secondary (with a border), and ghost or tertiary (text only). In the exercise: .btn-primary background-color: #2563EB, color: white, font-weight: 600; .btn-secondary color: #2563EB; .btn-ghost color: #6B7280. Every button has five required states: default (at rest), hover (the mouse is over it), active (being pressed), focused and disabled. Focused means picked with Tab (:focus-visible), and its focus ring is never removed with outline: none. Disabled means it can't be pressed yet: grey, with the reason in writing. A button that sends something also needs a loading state (aria-busy="true", the text "Booking...", a second tap ignored) so a booking isn't sent twice. Button height = padding + line-height (the height of each line) + border: 32, 40 or 48px. On a phone, use 48px (Material 48dp, Apple 44pt, WCAG 2.5.8 at least 24px). A label starts with a verb and says what happens: "Book the room", not "Submit".
⚡ One primary per screen
Meaning: Buttons have three levels: primary (solid, only one per screen), secondary (with a border), and ghost or tertiary (text only). On Mrs. Sri's booking card, the primary is "Book the room, pay on arrival".
⚡ Five required states
Write:
.btn-primary { color: #fff; background-color: #15803d; } /* default */
.btn-primary:hover { background-color: #166534; } /* mouse over it */
.btn-primary:active { background-color: #14532d; } /* being pressed */
.btn:focus-visible { outline: 3px solid #1f2937; outline-offset: 2px; } /* keyboard focus */
.btn-primary:disabled { color: #475569; background-color: #e2e8f0; cursor: not-allowed; } /* disabled */
Meaning: Default (at rest), hover (the mouse is over it), active (being pressed), focused (picked with Tab) and disabled (can't be pressed yet) are required on every button. Never remove the focus ring with outline: none. Disabled is grey, not pale green, with the reason written next to it.
⚡ Loading prevents a double booking
Write:
book.addEventListener("click", async () => {
if (book.getAttribute("aria-busy") === "true") return; // a second tap is ignored
book.setAttribute("aria-busy", "true");
book.textContent = "Booking...";
await sendBooking(); // send to the server
});
Guess first: Mr. Hadi has a weak signal. After he presses "Book the room", the button looks the same for 1.5 seconds, so he presses it once more. How many bookings reach Mrs. Sri?
Two, for the same nights. Without a loading state, every tap sends one booking.
Meaning: book is the book button. When it's clicked, it changes to "Booking..." right away, so the guest knows the tap was received. Until the server answers, the button carries aria-busy="true" (busy), and the first if line makes the code ignore further taps.
⚡ A verb first, 48px on a phone
Meaning: A button's label says what happens, starting with a verb: "Book the room", not "Submit" or "OK". On a phone, the button is 48px tall so it's easy to press with a thumb.