Build a reusable card
Show two different cards using one small HTML component, with a default link label and optional content.
Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder; do not create another starter.
Read
Start at L03. Your three pages share the layout, navigation and footer. Keep the dev server running. This lesson adds one component and changes Home and the stylesheet.
A component is a reusable HTML fragment. Unlike a routed page, it does not get its own URL. Unlike the site layout, it can appear several times inside one page. Saving components/content-card.html makes it available as <what-content-card>; there is no import statement or registration command.
Props are values supplied on that tag, like HTML attributes. A slot is where content placed between its opening and closing tags appears. The card below has a heading, summary and one ordinary link; the whole card is not a second, nested link.
Change
1. Create the component. Add components/content-card.html with this complete source:
<what>
props = "heading, href, summary, label"
defaults.label = "Read more"
</what>
<article class="content-card">
<h2>#heading#</h2>
<p>#summary#</p>
<slot/>
<a href="#href#">#label#<span class="visually-hidden">: #heading#</span></a>
</article>
props lists the names that the card accepts. defaults.label supplies “Read more” when the caller omits label. A supplied value wins over that default, including an explicitly empty string. Other declared props have no default, so always supply the heading, summary and destination for this card.
The card's heading is separate from the page's title. Each card receives its own values. #heading# and the other substitutions output text with HTML escaping; they are not a way to pass raw markup. Use the slot for HTML you author. Our href values are fixed local paths; text escaping alone is not a policy for arbitrary visitor-supplied URLs.
2. Use it twice. Replace site/index.html. The first card omits a label and includes a short note in its slot. The second overrides the label and has no slot content, so it can use a self-closing tag.
<what>
description = "Welcome to my small place for ideas, projects and useful things."
</what>
<h1>My website</h1>
<p>I am building my own website.</p>
<p>A small place for ideas, projects and useful things.</p>
<figure>
<img src="/static/images/site-image.svg" width="640" height="360"
alt="An open notebook and a green plant on a sunlit desk"
class="site-image">
<figcaption>A little space to make something of your own.</figcaption>
</figure>
<div class="card-grid">
<what-content-card heading="About this website" href="/about"
summary="Learn what I am making and why I started.">
<p class="card-note">Built one page at a time.</p>
</what-content-card>
<what-content-card heading="Keep in touch" href="/contact"
summary="Find the best way to start a conversation."
label="Visit Contact"/>
</div>
3. Style the cards. Replace static/styles.css. The existing site rules remain, followed by a small responsive grid and card styles. The visually hidden text gives each link a distinct accessible name, such as “Read more: About this website.”
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
Check
- Open Home. The first card should say “About this website,” contain “Built one page at a time.” and link with “Read more.” The second should say “Keep in touch,” have no note, and link with “Visit Contact.”
- Press Tab to reach each card's link, then Enter. They should open About and Contact. There should be one link per card and no duplicate HTML IDs.
- Narrow the browser. The two cards should become one column without sideways scrolling.
- Temporarily change one invocation's heading to
Ideas & projects. The browser should show “Ideas & projects” once, without visible entity text or markup. Restore the heading afterward.
Do not continue until the two instances keep their own data, the default label works, and both links reach the right pages.
Recover
- The card is missing: check that the file is named
components/content-card.htmland that the tag is<what-content-card>. New component files and saved edits are picked up by live reload; a restart should not be needed. This is an anticipated filename or tag typo. - A value is empty or the default is absent: compare the prop names in the component and caller. The component's configuration uses
=, includingdefaults.label = "Read more". A colon copied from an older example is not component assignment syntax. - An ampersand appears as visible
&text: check thatrun-what --versionprints 2.0.0 or later. Releases before 2.0.0 escaped component values twice.
If you need a clean comparison, download the L03 source checkpoint for this lesson. It is a copy of the project at this point, not a separately installed template. It contains source files and a checksum manifest; it excludes sessions and runtime databases. Keep your project and extract into a new folder. For example, after placing that archive in Downloads, stop your dev server and run these commands from the folder that contains my-site:
mkdir my-site-recovery-L03 && unzip -n "$HOME/Downloads/L03.zip" -d my-site-recovery-L03
If the folder already exists, the first command fails and extraction does not run. Pick another new name; do not empty an existing folder. After successful extraction:
cd my-site-recovery-L03
run-what dev
Reapply this lesson there. Optional exercises are not part of the required checkpoint; keep your own variations in your original project.
Try
Optional: give the component a variant prop with an empty default. Add that value alongside content-card in the article's class, pass variant="compact" to only one instance, and write a .content-card.compact rule with smaller padding. Both instances should still use the same component file. You do not need this variation for lesson 5.
Continue
You can now reuse a fragment with different data, defaults and optional child content. Next, build and serve the complete static website without running What.