In a shopping app, the goods appear as boxes with big photos. Open your order history in the same app, and each order is a single short row. The two shapes are different, and that is on purpose.
A box that gathers everything about one thing (its photo, name, details and action) is a card. A run of rows for many things of the same kind, one row per thing, is a list. Mrs. Sri has both too, on paper. Her brochure shows each room on its own page with a big photo, because guests choose by looking. Her guestbook has one booking per line, because she reads it from top to bottom and compares the dates. Each page of the brochure is a card, and the guestbook is a list.
Card or list
| Card | List | |
|---|---|---|
| Its content | few items, and the picture matters | many uniform items, a picture is optional |
| How it's read | looked at one by one | scanned and compared top to bottom |
| At Mist Homestay | three rooms for a guest to choose from | 16 December bookings on Mrs. Sri's dashboard |
This is the height of each item on a phone 375px wide (measured in Chromium, WebKit and Firefox). The last column shows how many items are visible in a dashboard area 664px tall, the laptop's list area from the Density & Scale lesson. In the phone's 611px area, the result is the same: 1 room, 1 booking, and all 16 bookings.
| Content | One item | Visible in a 664px dashboard area |
|---|---|---|
| A room as a card (4:3 photo, name, price, link) | 443px | 1 room |
| A booking as a card, with the room photo | 379px + a 16px gap | 664 ÷ 395 = 1.68, so 1 of 16 |
| A booking as a list row (37px) | 37px | 664 ÷ 37 = 17.9, so all 16 bookings |
For three rooms, one room per screen is exactly right, because a guest really does decide by looking at the photo. For 16 bookings, cards make Mrs. Sri scroll 16 times. A list shows them all at the same time.
Anatomy of a card
The anatomy of a card means its parts. Their order from top to bottom: image, title, description, action. The image catches the eye. The title gives a name. The description gives a reason to choose it. The action at the bottom (a link or a button) is the next step. Here is the HTML and CSS of the Terrace Room card:
<article class="card">
<img class="card-image" src="terrace-room-800.jpg" width="1200" height="900" alt="">
<div class="card-body">
<h3><a class="card-link" href="/rooms/terrace">Terrace Room</a></h3>
<p>Double bed, a window facing the mountains. 2 guests.</p>
<p class="price">$35 / night</p>
<a class="wa" href="https://wa.me/447700900123">Ask on WhatsApp</a>
</div>
</article>
<style>
.card { position: relative; border-radius: 0.75rem; border: 1px solid #E5E7EB; overflow: hidden; background: #fff; }
.card-image { display: block; width: 100%; height: auto; }
.card-body { padding: 1.25rem; }
</style>
<article>is the HTML element for content that stands on its own: it still makes sense if you move it to another page. The Terrace Room card can show on the home page, in search results or in an email, and stay whole.border-radius: 0.75rem(12px) rounds the card's corners.overflow: hiddenclips everything inside to those corners. Withoutoverflow: hidden, the photo's square corners poke out of the card's rounded ones.padding: 1.25rem(20px) goes on.card-body, not on.card. So the photo still touches the card's edges, and only the text gets space from the edges.alt=""(an empty text alternative) because the photo is decoration: the title under it already names the room.
In the exercise, the values are exactly these: <article class="card"> with border-radius: 0.75rem and overflow: hidden, and .card-body with padding: 1.25rem.
One click area per card
Mrs. Sri wants the whole Terrace Room card to be tappable. So she wraps the card in the link <a href="/rooms/terrace">. Inside the card there's a second link, "Ask on WhatsApp". Guess first: once the browser has read the HTML, where does that WhatsApp link end up?
Wrapping the whole card in an <a> looks handy, but HTML doesn't allow a link inside a link. We tested Mrs. Sri's code in Chromium, WebKit and Firefox. All three arranged the card's elements in the same way. Below is that arrangement. Each line is one element, written as the tag and then the class: a.wa is an <a> link with the class wa. An indented line sits inside the element above it. The wrapping link Mrs. Sri wrote has the class card-cover.
article.card
a.card-cover (the first link, holding only the photo)
img.card-image
div.card-body
a.card-cover (a copy of the first link: title, description, price)
h3, p, p.price
a.wa (the WhatsApp link, on its own)
On meeting the second <a>, the parser (the part of the browser that reads HTML) closes the first link. Then the parser makes a copy of the first link for the title, description and price. So the answer to the guess: the WhatsApp link ends up outside the card link, and the card link is split in two.
There is another problem. A link's name (what a screen reader reads out) is taken from all the text inside it. A link that wraps the card holds all the card's text, so a screen reader reads "Terrace Room Double bed, a window facing the mountains. 2 guests. $35 / night".
The fix: put one short link on the title. Then make an invisible layer that belongs to that link, and stretch it over the whole card:
.card-link::after { content: ""; position: absolute; inset: 0; } /* a click layer as big as the card */
.card:focus-within { outline: 3px solid #1f2937; outline-offset: 2px; } /* the focus ring on the card */
.wa { position: relative; z-index: 1; } /* the second link stays above the layer */
::afteris an extra element that CSS creates inside the title link.position: absolutewithinset: 0stretches it to all four edges of the nearest container (an element around it) that hasposition: relative. In the card code above, that element is.card, so the::afterlayer covers the whole card. In our test, a tap on the photo and on the price both opened/rooms/terrace.:focus-withinapplies when an element inside the card has keyboard focus. So the focus ring shows around the whole card.- A screen reader reads one short link, "Terrace Room", then the WhatsApp link as a second link.
.wagetsposition: relativeandz-index: 1(z-indexsets the stacking order: a bigger number shows on top). So the WhatsApp link sits above the layer and can still be tapped on its own.
A list for Mrs. Sri's dashboard
These are the first two rows of the booking list on Mrs. Sri's dashboard, with their CSS:
<ul class="list">
<li class="list-item"><span><b>Rina</b> · Terrace Room, 1 Dec, 2 nights</span><b>$70</b></li>
<li class="list-item"><span><b>Mr. Hadi</b> · Garden Room, 2 Dec, 1 night</span><b>$42.50</b></li>
</ul>
<style>
.list-item { display: flex; justify-content: space-between; gap: 8px; padding: 8px 12px; font-size: 14px; line-height: 20px; border-bottom: 1px solid #e2e8f0; }
.list-item:last-child { border-bottom: none; }
.list-item > b { flex: none; white-space: nowrap; } /* the total must not break onto two lines */
</style>
<ul>(the tag for a list) and<li>(one list item) are used because the content really is a list: one booking, one<li>.- The height of one row: 8 padding + 20 text + 8 padding + a 1px bottom border = 37px, the same as the dashboard rows in the Borders & Dividers lesson.
- The guest's name is bold, and the total is aligned right. Because every total sits on the same right edge, the eye can run straight down to compare them.
white-space: nowrapon the total stops its text from breaking onto a new line. "$70" has no space, so it can't break anyway. A total with a space in it can: in our Firefox test, "USD 70" broke onto two lines in a narrow row without this rule.
⚠️ Cards that break without you noticing
- A link inside a link. The browser splits the card link in two. Use one title link with an
::afterlayer. - Forgetting
overflow: hidden. The photo's corners poke out of the rounded card. - Cards for data that gets compared. 16 bookings as cards: only 1 shows. Use a list.
- Cards that don't match. If one room card has no photo, or its parts are in a different order, a row of cards becomes hard to compare. Use the same order everywhere: image, title, description, action.
- A card that only looks clickable on hover. Phones have no hover. A title that is a link and an action written out show that the card can be tapped.
TL;DR: A card gathers everything about one thing, in the order image, title, description, action. Cards suit a few items chosen by looking, like three rooms. A list shows many uniform items that get scanned and compared, like 16 bookings: 37px rows fit them all in a dashboard area 664px tall, while cards fit only 1. Wrap a card in <article>, the element for content that stands on its own. In the exercise: .card with border-radius: 0.75rem and overflow: hidden (it clips the photo to the rounded corners), then .card-body with padding: 1.25rem. Never put a link inside a link, because the browser splits it. To make the whole card tappable, give the card position: relative and put one link on the title. Then stretch an ::after { position: absolute; inset: 0 } layer (an extra element that CSS creates) from that link over the whole card. Lift a second link above that layer with position: relative; z-index: 1.
⚡ Card or list
Meaning: A card gathers everything about one thing, and suits a few items chosen by looking, like three rooms. A list shows many uniform items that get scanned and compared, like 16 bookings on Mrs. Sri's dashboard: 37px rows fit them all in an area 664px tall, cards only one.
⚡ Image, title, description, action
Write:
<article class="card">
<img class="card-image" src="terrace-room-800.jpg" width="1200" height="900" alt="">
<div class="card-body">
<h3><a class="card-link" href="/rooms/terrace">Terrace Room</a></h3>
<p>Double bed, a window facing the mountains. 2 guests.</p>
</div>
</article>
<style>
.card { position: relative; border-radius: 0.75rem; border: 1px solid #E5E7EB; overflow: hidden; background: #fff; }
.card-body { padding: 1.25rem; }
</style>
Meaning: <article> is the element for content that stays whole when moved to another page, like this room card. border-radius rounds the card's corners, and overflow: hidden clips the photo to those corners. The padding goes on .card-body, not on .card, so the photo still touches the card's edges.
⚡ No link inside a link
Guess first: the whole card is wrapped in <a href="/rooms/terrace">, with an "Ask on WhatsApp" link inside. Where does the WhatsApp link end up?
Outside the card link, and the card link is split in two. HTML doesn't allow a link inside a link, so the browser closes the first link as soon as it meets the second <a>.
Meaning: Put one short link on the title. Then stretch an ::after layer (an extra element that CSS creates) over the whole card with position: absolute; inset: 0. The card needs position: relative, so the layer is measured from the card. Lift the second link above the layer with position: relative; z-index: 1.