Skip to content
UX PrinciplesChapter 43 / 57Intermediate12 min read

Visual Hierarchy & Spacing

Visual hierarchy and whitespace rules for UX. One primary button, a first viewport budget, a spacing scale, a type scale and the squint test, with measurable numbers.

On this page

1. What is this practice?

Visual hierarchy means guiding the user's eye in a set order: the most important thing first, then the next most important. The tools for this are size, weight, color, position and space.

It starts with one sentence: "This screen lets <user> do or learn <one thing>." If you cannot write that sentence, the screen has two jobs. Fix that problem first, because no layout fix will repair it.

The rules of this chapter at a glance:

RuleMeaningNumber
One primary actionOnly one primary-styled button in a viewportMax 1, and 0 is also wrong
First viewport budgetThe answer to the screen is visible without scrolling, and banners and onboarding take little spaceTransient blocks max 25 percent
Whitespace creates groupsThe gap between groups is at least double the gap inside a groupRatio 2x or more
Spacing scaleEvery margin, padding and gap comes from a fixed step4, 8, 12, 16, 24, 32, 48, 64
DensityFixed sizes for tables and stat cardsRows 40, 48 or 56 px
Lighten first, then removeShrink, collapse or move before you removeAn order of 4 steps
Disclosure depthA thing is visible, or one or two clicks awayMax 2 levels
Type scaleFew text sizes, few big things3 sizes + title, max 2 large
When to use a borderSpace first, then a tint, a border lastSurfaces max 2 deep
Squint testIn a blurred screenshot, the primary thing shows first5 to 20 px blur
Front-loadThe first word of a heading carries informationHeading max 6 words

Two reference sizes are used for the viewport: desktop 1440 by 900 and phone 390 by 844. The "first viewport" is the part you see on page load without scrolling.

2. Why does it matter?

UX research (especially the eye-tracking studies by Nielsen Norman Group) keeps showing the same thing: users spend most of their time above the fold. In one study about 57 percent of viewing time was on the first screen and about 74 percent on the first two screens. Whatever you push down loses a big share of attention.

The second reason is decision time. Hick's law says the more equal choices there are, the slower the decision. Three equal primary buttons mean the user has to work out what the next step is. Government design systems also keep one main call to action per page for this reason.

Proximity also comes from research: things that are close together are seen by the brain as one group. So spacing is not just "looks nice", it tells you which thing belongs with which.

Without the rulesWith the rules
Three primary buttons, the user is confusedOne primary, the rest secondary or link
Banners and a checklist ate the first screenStats and main content are in the first screen
A label floats between two inputsThe label sticks to its own field
p-[18px], mt-5, mt-6 random valuesThe scale of 4, 8, 12, 16, 24, 32, 48, 64
7 font sizes in one card3 sizes and one page title
A card inside a card inside a cardPage, then card, that is all
Blur it and a grey slab shows firstBlur it and the main number and button show first

3. When to follow it

LevelHow importantWhy
Learning projectOne primary button, the spacing scale and 3 text sizes from nowIf these three become habits, the rest of the design starts looking better on its own, and the cost is zero
Production codeAll the rules, on every screen, at both desktop and phone sizeReal users do not scroll, and they get confused and leave, which directly hits conversion
Senior levelMeasure the rules in review and give feedback with numbersFeedback like "it feels a bit off" fixes nothing, a measured height and the name of the rule does

When to learn it: as soon as you build your first real UI component with Tailwind. Learning design tokens and the spacing scale together is the easiest way.

4. Bad vs good code

One primary action

Bad: every button is primary.

tsx
export function ProjectsHeader() {
  return (
    <div className="flex gap-3">
      <button className="bg-primary text-primary-foreground px-4 py-2">New project</button>
      <button className="bg-primary text-primary-foreground px-4 py-2">Import</button>
      <button className="bg-primary text-primary-foreground px-4 py-2">Export</button>
    </div>
  );
}

Good: one primary, the rest secondary and ghost.

tsx
export function ProjectsHeader() {
  return (
    <div className="flex gap-2">
      <button className="rounded-md bg-primary px-4 py-2 text-primary-foreground">Create project</button>
      <button className="rounded-md border px-4 py-2">Import projects</button>
      <button className="rounded-md px-4 py-2 text-muted-foreground">Export</button>
    </div>
  );
}

Zero primary is also wrong. On a first-run screen, if no button is primary, the user cannot tell where the next step is.

Spacing that makes groups

Bad: the same gap everywhere, and the label hangs between the input above and the one below.

tsx
<form className="space-y-4">
  <label htmlFor="name">Project name</label>
  <input id="name" className="p-[18px]" />
  <label htmlFor="slug">URL slug</label>
  <input id="slug" className="mt-5" />
</form>

Good: 8 px from the label to its field, 24 px from one field group to the next.

tsx
<form className="flex flex-col gap-6">
  <div className="flex flex-col gap-2">
    <label htmlFor="name">Project name</label>
    <input id="name" className="rounded-md border px-3 py-2" />
  </div>
  <div className="flex flex-col gap-2">
    <label htmlFor="slug">URL slug</label>
    <input id="slug" className="rounded-md border px-3 py-2" />
  </div>
</form>

How the spacing scale relates to Tailwind classes:

StepTailwindWhere to use it
4 px1An icon and its label, inside a badge
8 px2Inside one control group: label to field, button to button
12 px3Rows of a compact list, between chips
16 px4Card padding on phone, related blocks inside a card, page margin on phone
24 px6Card padding on desktop, sections inside a card, page margin on desktop
32 px8Between page sections on phone
48 px12Between page sections on desktop
64 px16Only for hero or marketing blocks, never inside an admin screen

The steps of the scale differ from each other by at least about 25 percent, so the choice is clear. If mt-5 and mt-6 sit side by side, the eye cannot tell the difference, it just makes the code messy.

Type scale and borders

Bad: many sizes, everything bold, a card inside a card.

tsx
<div className="rounded-xl border p-6">
  <div className="rounded-lg border p-4">
    <h3 className="text-xl font-bold">Revenue</h3>
    <p className="text-[13px] font-semibold">This month</p>
    <div className="rounded border p-3 text-3xl font-bold text-primary">$4,200</div>
    <p className="text-[11px]">Updated 5 min ago</p>
  </div>
</div>

Good: 3 sizes, size, weight and color on only one thing, and only one level of surface.

tsx
<section className="flex flex-col gap-1 rounded-lg border p-6">
  <h3 className="text-sm text-muted-foreground">Revenue this month</h3>
  <p className="text-2xl font-semibold tabular-nums">$4,200</p>
  <p className="text-xs text-muted-foreground">Updated 5 min ago</p>
</section>

What is the difference?

BadGoodRule
Three primary buttonsOne primary, one bordered, one ghostOne primary action
Equal space-y-4gap-2 inside, gap-6 outsideDouble gap between groups
p-[18px], mt-5Only steps from the scaleSpacing scale
5 font sizes, 4 bold12, 14, 24 px and one bold valueMax 3 sizes, max 2 large
Three nested bordersOne cardSurfaces max 2 deep
"Revenue" + "This month" separate"Revenue this month"Front-load, fewer lines

5. How to apply it

How to review a screen
Write the screen's one job in one sentence
Open the screen at 1440 by 900 and at 390 by 844
Measure the primary buttons, text sizes and block heights
Squint test: blur a screenshot by 5 to 20 px
For every mistake, take the smallest fix: lighten, collapse, move, then remove
Measure again, and write the before and after numbers in the PR

Measure with the browser, not by guessing. In the DevTools console:

js
const setup = document.querySelector("[data-block='setup']");
const { top, height } = setup.getBoundingClientRect();
const budget = window.innerHeight * 0.25;

const textSizes = new Set(
  [...document.querySelectorAll("main *")]
    .filter((el) => el.childElementCount === 0 && el.textContent.trim())
    .map((el) => getComputedStyle(el).fontSize)
);

console.log({ top, height, budget, textSizes: [...textSizes] });

The checklist for the remaining rules:

CheckWhen it passes
First viewportThe answer to the screen starts without scrolling on both desktop and phone
Transient blocksBanners, promos and onboarding together at most 225 px at 900, at most 211 px at 844
Lighten firstOrder: less size or contrast, then collapse to one line, then a drawer or separate page, and remove last
DisclosureEverything is visible, or one click away, or two clicks away, never three
BorderSpacing first, if that does not work a light background tint, and a border only when the block should read as one unit with unrelated content
HeadingsThe first word carries information, max 6 words, the description max one line (90 characters)
Basic UIBody text not under 16 px, line length 60 to 75 characters, text contrast 4.5:1

An example of front-loading: "Products: 0 added" is a good heading. "In order to get started you will first need to add some products" will be missed by a scanning user, because people read in an F-shape and only see the first words.

Interview tip

If you are asked "why does this screen feel cluttered?", answer with the name of the rule and a number: "It has two primary buttons, 6 text sizes and cards nested three levels deep." That is a much stronger answer than "I just did not like it."

6. Exceptions and common mistakes

SituationWhat to do
Marketing or hero page64 px spacing and large type are allowed, not on admin screens
A brand new account with no dataOnboarding can take more of the first viewport, because there is nothing else to show
A pricing page with three plansNot an exception. The recommended plan is primary, the rest secondary
A destructive confirm dialogThe dialog has one primary (like "Delete project"), and "Cancel" is secondary

Mistake: Deleting a busy panel outright

Why? The help link or setting that users or support depended on disappears.

Better: First reduce its size or contrast, then collapse it, then move it into a drawer. Removing is the very last step.

Mistake: Arbitrary spacing values like p-[18px] or mt-5 next to mt-6

Why? The eye cannot tell 20 and 24 px apart, the rhythm just breaks and every developer adds their own value.

Better: Use only steps from the scale and keep each step for its set job.

Mistake: The same gap everywhere

Why? A label is as close to its own field as to the previous one, so groups are not visible.

Better: 8 px inside a group, 24 px between groups. The outer gap is at least double the inner one.

Mistake: A banner, an announcement and a checklist stacked one above the other

Why? The first screen goes to messages and the real work is pushed below the fold.

Better: All transient blocks share one budget, max 25 percent. Show what is closest to value and let the rest wait.

7. What to learn before and after

This chapter is the foundation of the UX section. The other four chapters apply these same rules to forms, onboarding, dashboards and motion.

Final quick revision

CheckWhat to remember
One jobThe screen's job in one sentence
Primary buttonExactly one in every viewport
First viewportThe answer without scrolling, transient blocks max 25 percent
Spacing4, 8, 12, 16, 24, 32, 48, 64 and a double gap between groups
TypeMax 3 sizes + title, max 2 large things
SurfacesSpace first, then tint, then border, max 2 deep
DisclosureMax two clicks away
Squint testThe primary thing shows first in the blur
Fix orderLighten, collapse, move, remove