Step 3 / 1127%

Step 3

Styling the Display and Keys

20 min

Style the display and give each group of keys its own color.

Definition

A utility class is a CSS class that applies a single, precise property: a background color, a text size, a rounded corner... By combining several of them on the same element, you get its complete appearance, without writing a single line of CSS.

An interactive state describes how an element looks depending on the user's action: when the mouse hovers over it (hover:) or while it is being pressed (active:). With Tailwind, you just prefix a class so that it only applies in that state.

Time for color. We style the display first, then the keys, with a simple rule: color indicates the key's role.

This rule isn't just an aesthetic detail. In an interface, color guides the eye: the user spots the digits, the operators and the result key at a glance, without having to read each symbol. You will apply this principle with just a few classes.

1. The display

calculator.html (replace the Display block)
<!-- Display -->
<div class="mb-4 rounded-2xl bg-slate-950 px-4 py-5 text-right" aria-live="polite">
  <p id="previous" class="h-6 truncate text-sm text-slate-400"></p>
  <p id="current" class="truncate text-4xl font-semibold text-white">0</p>
</div>
  • bg-slate-950: a background darker than the card gives the effect of a recessed screen.
  • text-right: numbers align to the right, like on a real calculator.
  • h-6 on the top line: it keeps its height even when empty, so the display doesn't jump when it fills up.
  • truncate: if a text is too long, it is cut off instead of overflowing the display.

Note the size difference between the two lines: text-sm and text-slate-400 make the previous calculation discreet, while text-4xl font-semibold text-white highlights the number currently being entered. Visual hierarchy is used to show what matters most.

2. The keys

All the keys share the same base. Only the background color changes depending on the family:

Recipe (to remember)
Base (all keys)
rounded-2xl py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95

Background color
digits and decimal point  →  bg-slate-700
C, ⌫, %, ±                →  bg-slate-600
operators                 →  bg-indigo-600
equals                    →  bg-emerald-600
  • hover:brightness-110 lightens the key on hover, whatever its color: a single class instead of one per color.
  • active:scale-95 slightly shrinks the key while it is pressed, to give the impression of pushing it.
  • transition makes these changes gradual rather than abrupt.

Apply this recipe to each of the 20 buttons. To save you time, here is the complete grid:

calculator.html (replace the Keys block)
<!-- Keys -->
<div id="keys" class="grid grid-cols-4 gap-3">
  <button data-action="clear" aria-label="Clear all" class="rounded-2xl bg-slate-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">C</button>
  <button data-action="delete" aria-label="Delete last digit" class="rounded-2xl bg-slate-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">⌫</button>
  <button data-action="percent" aria-label="Percent" class="rounded-2xl bg-slate-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">%</button>
  <button data-operator="/" aria-label="Divide" class="rounded-2xl bg-indigo-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">÷</button>

  <button data-digit="7" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">7</button>
  <button data-digit="8" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">8</button>
  <button data-digit="9" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">9</button>
  <button data-operator="*" aria-label="Multiply" class="rounded-2xl bg-indigo-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">×</button>

  <button data-digit="4" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">4</button>
  <button data-digit="5" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">5</button>
  <button data-digit="6" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">6</button>
  <button data-operator="-" aria-label="Subtract" class="rounded-2xl bg-indigo-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">−</button>

  <button data-digit="1" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">1</button>
  <button data-digit="2" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">2</button>
  <button data-digit="3" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">3</button>
  <button data-operator="+" aria-label="Add" class="rounded-2xl bg-indigo-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">+</button>

  <button data-action="sign" aria-label="Change sign" class="rounded-2xl bg-slate-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">±</button>
  <button data-digit="0" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">0</button>
  <button data-action="decimal" aria-label="Decimal point" class="rounded-2xl bg-slate-700 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">.</button>
  <button data-action="equals" aria-label="Equals" class="rounded-2xl bg-emerald-600 py-4 text-xl font-medium text-white transition hover:brightness-110 active:scale-95">=</button>
</div>

You will notice that the base classes are repeated on every button. This is normal with Tailwind, and it is the price to pay for never writing separate CSS. On a large project, these repetitions are grouped into a component or a loop, but for twenty buttons, copying the recipe remains the simplest option.

Finished calculator

It already looks like a real calculator. It just needs a brain.

Common mistakes to avoid

  • Forgetting to replace the whole block: if the old code stays in the page, you will see two displays or forty buttons.
  • Getting a color value wrong: a typo in the value, for example bg-indigo-600 written differently, produces no error but the key keeps a transparent background.
  • Deleting the data attributes: when replacing the block, be sure to keep data-digit, data-operator and data-action: the JavaScript will need them.

Key takeaways

  • Color indicates the role of each key.
  • The hover: and active: prefixes apply a class only on hover or on press.
  • The same set of base classes, with a single color changing, is enough to style a whole family of elements.

Checkpoint

Hover over the keys: they get lighter. Click on them: they shrink for a moment. Nothing shows on the display yet; the JavaScript comes in the next step.

If the keys don't react on hover, check that hover:brightness-110 and transition are present on every button.