Visual Hierarchy & Layout Principles
By the end of this lesson you will be able to decide what a reader should notice first on a page, and then make that happen with CSS. You will use size, weight, colour, contrast and spacing on purpose, line things up on a grid, keep a steady vertical rhythm, and turn a cluttered page into a calm one.
You should know Typography Fundamentals, CSS Grid Introduction, CSS Custom Properties and Responsive CSS Grid. The design theory behind these ideas is in the UI/UX course lessons Visual Design Principles and Spacing, Grids & the 8-point System. Here we focus on the CSS that puts them into practice.
What visual hierarchy means
Visual hierarchy is the order in which the eye notices things on a page. A good page answers the reader's questions in the right order, without the reader having to work for it.
Think of a cricket scoreboard at a stadium. The score, such as 187/4, is huge. The overs are smaller, and the run rate is smaller still. From the back row you know the match situation in one glance.
Most screens need only about three levels:
- Primary: the one thing people must see first, such as the page title or the price.
- Secondary: supporting facts, such as dates, sub-headings and key details.
- Tertiary: everything else, such as small print, captions and metadata.
Five or six competing levels usually means the page needs less content, not more styling.
The levers and the CSS behind them
Each way of showing importance maps to a few CSS properties:
| Lever | CSS | Watch out for |
|---|---|---|
| Size | font-size from a type scale | Steps too small to notice, like 16px next to 17px |
| Weight | font-weight: 400, 600, 700 | Making everything bold |
| Colour | color, background-color | More than one accent colour |
| Contrast | darker text for primary, lighter for tertiary | Muted text that fails 4.5:1 |
| Spacing | margin, padding, gap | Equal gaps everywhere, which hide the groups |
| Position | source order, grid-area, order | Visual order that differs from reading order |
A type scale is a fixed set of font sizes where each step is the previous one times a ratio, such as 1.25. Using only the steps on your scale keeps the differences clear. You will build full scales in Professional Typography Systems.
Step 1: one lever at a time
Run this example. Every card has the same three lines. The goal is that the dish name is seen first, the price second and the description last. Each card uses a different lever.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hierarchy levers</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; padding: 16px; background: #f8fafc; color: #1e293b; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.card { background: #fff; border: 1px solid #e2e8f0; border-radius: 10px; padding: 14px; }
.card p { margin: 0; font-size: 15px; }
.tag { font-size: 12px !important; color: #64748b; margin-bottom: 8px !important; }
.size .name { font-size: 24px; }
.weight .name { font-weight: 700; }
.colour .name { color: #c2410c; }
.space .name { margin-bottom: 14px; }
.all .name { font-size: 22px; font-weight: 700; }
.all .price { font-weight: 600; color: #c2410c; margin: 2px 0 10px; }
.all .desc { font-size: 14px; color: #475569; }
</style>
</head>
<body>
<div class="grid">
<div class="card"><p class="tag">No lever</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
<div class="card size"><p class="tag">Size</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
<div class="card weight"><p class="tag">Weight</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
<div class="card colour"><p class="tag">Colour</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
<div class="card space"><p class="tag">Space</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
<div class="card all"><p class="tag">Three levels</p><p class="name">Masala chai</p><p class="price">₹20</p><p class="desc">Fresh ginger and cardamom</p></div>
</div>
</body>
</html>The first card is flat: all three lines look equally important. Each single lever lifts the name a little. The last card combines size and weight for the name, colour for the price, and a smaller, lighter description: three clear levels from a handful of declarations.
Proximity, alignment, repetition and whitespace
Proximity means related things sit close together. The rule of thumb: space inside a group is smaller than space between groups. Give headings more space above than below, so each heading clearly belongs to the text after it.
h2 { margin-block: 2.5rem 0.5rem; } /* far from the previous section, close to its own text */
.field { display: grid; gap: 0.25rem; } /* label and input: tight */
.form { display: grid; gap: 1.5rem; } /* between fields: looser */Alignment means items share edges. Left-align body text in left-to-right languages, because a straight left edge gives the eye a place to return to. Centre only short items such as a hero title. CSS Grid makes shared edges automatic. Grid columns line up across rows, and Subgrid (Baseline 2023) lets the titles, prices and buttons inside a row of cards line up with each other.
Repetition means the same kind of thing always looks the same. In CSS that is a component class (.card, .btn) and shared tokens, never one-off values typed into each rule.
Whitespace is empty space used on purpose. It separates groups and gives the primary item room to stand out. For reading comfort, limit line length to roughly 45-75 characters with max-inline-size: 65ch.
How people scan: F and Z patterns
Eye-tracking studies, such as those by the Nielsen Norman Group, observed two common scanning shapes. Treat them as observations, not laws: clear headings change how people read.
- F-pattern: on text-heavy pages, many people read across the top, then across a little lower, then down the left edge. They read the first words of lines and miss the rest.
- Z-pattern: on simple pages with few elements, such as a hero section, the eye often goes top left, top right, diagonally down, then across the bottom.
What this means for CSS and content:
- Put the important words first in headings and list items, and make key terms bold.
- Break long text with sub-headings so the left edge carries useful signposts.
- In a hero, place the logo top left and the main action where the Z ends.
- For right-to-left languages such as Urdu, the patterns mirror. Use Logical Properties like
margin-inline-startso the layout mirrors too.
Grids and rhythm
A grid is a set of invisible columns that every element lines up with. Many sites use a 12-column grid, because 12 divides evenly into halves, thirds and quarters:
.page {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
.main { grid-column: span 8; }
.aside { grid-column: span 4; }On a phone, switch to a single column with a media query or let content decide with repeat(auto-fit, minmax(16rem, 1fr)), as in Responsive Grid Systems.
Vertical rhythm is the same idea from top to bottom: spacing that comes from one base unit, so the page feels steady, like the beat in a song. A simple approach uses a spacing scale built on one step, such as 0.5rem, and only those values. The lh unit (Baseline 2023) equals the element's computed line height, so margin-block-end: 1lh makes the gap after a paragraph exactly one line of text.
:root {
--space-1: 0.5rem;
--space-2: 1rem;
--space-3: 1.5rem;
--space-4: 2rem;
--space-6: 3rem;
}
p { margin-block: 0 1lh; }
section + section { margin-block-start: var(--space-6); }Before and after: redesigning a cluttered page
Step 2: the cluttered page
Here is a course page for a coaching centre in Kota. The information is useful, but the page fights the reader. Run it and try to find the start date and the fee.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cluttered page</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; padding: 10px; text-align: center; background: #fffbe6; }
h1 { color: #d00; text-transform: uppercase; font-size: 26px; border: 2px dashed #00f; }
h2 { color: #090; font-size: 20px; text-transform: uppercase; margin: 4px; }
p { font-weight: bold; margin: 3px; color: #333; }
.box { border: 2px solid #f0f; margin: 6px; padding: 4px; background: #e6f7ff; }
.fee { color: #d00; font-size: 20px; }
.new { background: #ff0; color: #d00; }
a { display: inline-block; margin: 6px; padding: 8px; font-weight: bold; color: #fff; }
.a1 { background: #f60; } .a2 { background: #09c; } .a3 { background: #6c3; }
</style>
</head>
<body>
<h1>Bright Path Classes Kota !!!</h1>
<p class="new">NEW!!! ADMISSIONS OPEN!!! LIMITED SEATS!!!</p>
<div class="box">
<h2>JEE Main Foundation Batch</h2>
<p>For students who have finished Class 10 and want to prepare for JEE Main with weekly tests, doubt sessions and printed study material for Physics, Chemistry and Mathematics.</p>
<p>Batch starts: 5 January</p>
<p class="fee">Fee: ₹48,000 per year</p>
<p>Timing: 4 pm to 7 pm, Monday to Saturday</p>
</div>
<div class="box"><h2>Call us</h2><p>98XXXXXX10</p></div>
<a class="a1" href="#">ENROL NOW</a><a class="a2" href="#">DOWNLOAD BROCHURE</a><a class="a3" href="#">BOOK FREE DEMO</a>
</body>
</html>Problems, named by principle:
- Hierarchy: everything is bold, and three loud colours compete, so nothing stands out.
- Alignment: the long paragraph is centred, so every line starts at a different place.
- Proximity: the gaps are tiny and equal, so groups blur together.
- Contrast: the yellow banner and three bright buttons all shout at the same volume.
- Repetition: borders and colours change from box to box.
Step 3: the redesign
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redesigned page</title>
<style>
:root {
--text: #0f172a; --text-muted: #475569; --surface: #ffffff; --page: #f8fafc;
--accent: #c2410c; --accent-hover: #9a3412; --border: #e2e8f0;
--space-1: 0.5rem; --space-2: 1rem; --space-3: 1.5rem; --space-4: 2rem;
}
body { font-family: system-ui, sans-serif; margin: 0; padding: var(--space-3); background: var(--page); color: var(--text); line-height: 1.5; }
main { max-inline-size: 40rem; margin-inline: auto; }
body:has(#squint:checked) main { filter: blur(4px); }
label { display: block; margin-bottom: var(--space-3); font-size: 0.875rem; }
.eyebrow { margin: 0; font-size: 0.875rem; font-weight: 600; color: var(--accent); }
h1 { margin: var(--space-1) 0 var(--space-2); font-size: 2rem; line-height: 1.2; text-wrap: balance; }
.lead { margin: 0 0 var(--space-4); max-inline-size: 60ch; color: var(--text-muted); }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: var(--space-2); margin: 0 0 var(--space-4); padding: var(--space-3); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.facts div { display: grid; gap: 0.125rem; align-content: start; }
.facts dt { font-size: 0.875rem; color: var(--text-muted); }
.facts dd { margin: 0; font-size: 1.25rem; font-weight: 700; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
.btn { display: inline-block; padding: 0.75rem 1.5rem; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 600; text-decoration: none; }
.btn:hover { background: var(--accent-hover); }
.link { color: var(--accent); font-weight: 600; }
.small { margin: var(--space-3) 0 0; font-size: 0.875rem; color: var(--text-muted); }
.small a { color: inherit; }
</style>
</head>
<body>
<label><input type="checkbox" id="squint"> Squint test (blur the page)</label>
<main>
<p class="eyebrow">Admissions open</p>
<h1>JEE Main Foundation Batch</h1>
<p class="lead">For students who have finished Class 10. Weekly tests, doubt sessions and printed material for Physics, Chemistry and Mathematics.</p>
<dl class="facts">
<div><dt>Batch starts</dt><dd>5 January</dd></div>
<div><dt>Fee</dt><dd>₹48,000 a year</dd></div>
<div><dt>Timing</dt><dd>4 to 7 pm, Mon to Sat</dd></div>
</dl>
<div class="actions">
<a class="btn" href="#">Enrol now</a>
<a class="link" href="#">Book a free demo class</a>
</div>
<p class="small">Questions? Call 98XXXXXX10 or <a href="#">download the brochure (PDF)</a>.</p>
</main>
</body>
</html>You should see a calm page: a small orange label, a large title, a grey introduction, a white panel with three facts, one orange button and a text link. Tick Squint test. Even blurred, the title, the facts and the button still stand out, because hierarchy depends on size, weight and space, not on reading every word.
What changed, principle by principle:
- Hierarchy: three levels. The title is the largest; the facts are big and bold; the rest is regular and muted.
- Alignment: everything shares one left edge, and the text is limited to a readable width.
- Proximity: each label sits tight above its value, while sections are separated by
--space-4. - Contrast: one accent colour, used only for the label and the actions.
- Repetition: every spacing value and colour comes from a token.
- Fewer competitors: three equal buttons became one button and one text link. The brochure and phone number moved into the small print.
Common mistakes
- Emphasising everything. Bold, capitals and colour on every line cancel each other out.
- Tiny size steps. A heading at 17px next to 16px text looks like a mistake, not a level.
- Muted text that fails contrast. Secondary text should be lighter, but still pass 4.5:1.
- Random spacing values. Pick from a scale so the rhythm stays steady.
Interview questions
1. What is visual hierarchy, and how do you create it in CSS?
It is the order in which people notice elements on a page. In CSS you create it with font size from a type scale, font weight, colour and contrast, spacing, and position in the layout. Most screens need about three clear levels, not more.
2. What is the proximity principle, and how does it affect spacing?
Items that are close together look related. So spacing inside a group should be smaller than spacing between groups, for example a small gap between a label and its input and a larger one between fields. Headings get more space above than below so they attach to their own content.
3. Are the F and Z patterns rules you must follow?
No. They are patterns observed in eye-tracking studies: F on text-heavy pages and Z on sparse pages. They suggest putting key words first and placing the main action where the eye ends, but clear headings and good content change how people read.
4. What is vertical rhythm, and how can CSS support it?
It is consistent spacing from top to bottom, built from one base unit. In CSS you keep a spacing scale in custom properties and use only those values. The lh unit can set gaps equal to one line of text.
5. How do you check hierarchy quickly without user testing?
Use the squint test: blur your eyes or apply filter: blur() to the page. If the most important items still stand out in the right order, the hierarchy works. It is a quick check, not a replacement for testing with real users.
Key points
- Visual hierarchy is the order of noticing; aim for about three levels.
- The levers are size, weight, colour, contrast, spacing and position, each with clear CSS properties.
- Space inside groups is smaller than space between groups; headings get more space above than below.
- Align to shared edges with Grid, left-align long text, and limit lines to about
65ch. - F and Z scanning patterns are observations: put key words first and actions where the scan ends.
- Build spacing from one scale for a steady vertical rhythm, and check the result with the squint test.
Exercise
Meera's school in Pune has a notice board page. Every line is bold and centred, the heading is red with a dashed border, and all gaps are 4px. The notice says: "Annual Sports Day", "Saturday 12 December, 8:00 am", "Main ground", "Students must wear house colours. Parents are welcome from 10 am." Rebuild it with three levels of hierarchy, a spacing scale in custom properties, left alignment and one accent colour. Show the date and venue as a small fact panel, and add a squint-test checkbox.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sports Day notice</title>
<style>
:root { --text: #0f172a; --muted: #475569; --accent: #1d4ed8; --space-1: 0.5rem; --space-2: 1rem; --space-3: 1.5rem; }
body { font-family: system-ui, sans-serif; margin: 0; padding: var(--space-3); color: var(--text); line-height: 1.5; }
main { max-inline-size: 36rem; }
body:has(#squint:checked) main { filter: blur(4px); }
.eyebrow { margin: 0; font-size: 0.875rem; font-weight: 600; color: var(--accent); }
h1 { margin: var(--space-1) 0 var(--space-3); font-size: 2rem; line-height: 1.2; }
.facts { display: flex; flex-wrap: wrap; gap: var(--space-3); margin: 0 0 var(--space-3); }
.facts dt { font-size: 0.875rem; color: var(--muted); }
.facts dd { margin: 0; font-weight: 700; font-size: 1.125rem; }
.note { margin: 0; max-inline-size: 60ch; color: var(--muted); }
</style>
</head>
<body>
<label><input type="checkbox" id="squint"> Squint test</label>
<main>
<p class="eyebrow">School notice</p>
<h1>Annual Sports Day</h1>
<dl class="facts">
<div><dt>When</dt><dd>Saturday 12 December, 8:00 am</dd></div>
<div><dt>Where</dt><dd>Main ground</dd></div>
</dl>
<p class="note">Students must wear house colours. Parents are welcome from 10 am.</p>
</main>
</body>
</html>The title is the primary level, the date and venue are the secondary level, and the note is tertiary. Every gap comes from the spacing scale, and blue appears only on the small label.
Quick Quiz
1. Which rule of thumb follows from the proximity principle?
Closeness signals that items belong together, so groups need tighter inner spacing than outer spacing.
2. A page has a heading at 17px and body text at 16px. What is the problem?
Hierarchy needs clear differences; tiny steps look like mistakes rather than levels.
3. What does margin-block-end: 1lh do on a paragraph?
The lh unit equals the element's computed line height, which helps keep a steady rhythm.
4. What is the best way to describe the F-pattern?
It is an observation from eye-tracking research, useful as a guide but not a law.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
