HTML & CSS exercises with solutions
1. Semantic Business Card Beginner
Build a small page with <header> (name as <h1>), <main> (a paragraph about yourself, a list of 3 hobbies and a link to a website) and <footer> (copyright notice). Use fitting semantic elements.
Note
Use <header>, <main>, <footer>, <h1>, <p>, <ul> with <li> and <a href="…">.
Show sample solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Business Card</title>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}</style>
</head>
<body>
<header><h1>Ada Lovelace</h1></header>
<main>
<p>I am interested in mathematics and programming.</p>
<h2>Hobbies</h2>
<ul><li>Reading</li><li>Chess</li><li>Music</li></ul>
<p><a href="https://example.com">My website</a></p>
</main>
<footer><small>© 2024 Ada Lovelace</small></footer>
</body>
</html>2. Contact Form Beginner
Create a form with the fields name (required), email (required, type email), topic (dropdown with 3 options), message (multi-line) and a submit button. Every field needs a linked label.
Note
Use <label for="…"> + id on the field, or wrap the field in the label. required makes fields mandatory.
Show sample solution
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222} p { margin: 0 0 10px; }</style>
<form onsubmit="event.preventDefault()">
<p><label for="name">Name</label><br><input id="name" name="name" required></p>
<p><label for="mail">Email</label><br><input id="mail" name="mail" type="email" required></p>
<p><label for="thema">Topic</label><br>
<select id="thema" name="thema"><option>Question</option><option>Feedback</option><option>Other</option></select></p>
<p><label for="text">Message</label><br><textarea id="text" name="text" rows="4"></textarea></p>
<button type="submit">Submit</button>
</form>3. Card with the Box Model Beginner
Style a <div class="karte"> with a heading and text: maximum width 300px, centered, 16px padding, gray border (1px), rounded corners (12px), light shadow. Set box-sizing: border-box.
Note
margin: 0 auto centers, max-width limits the width, box-shadow: 0 4px 12px rgb(0 0 0 / 15%) creates the shadow.
Show sample solution
<!DOCTYPE html>
<style>
body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
*, *::before, *::after { box-sizing: border-box; }
.karte { max-width: 300px; margin: 0 auto; padding: 16px; border: 1px solid #ccc;
border-radius: 12px; box-shadow: 0 4px 12px rgb(0 0 0 / 15%); }
.karte h2 { margin-top: 0; }
</style>
<div class="karte">
<h2>Title</h2>
<p>Description text of the card.</p>
</div>4. Navigation Bar with Flexbox Intermediate
Build a horizontal navigation: a logo text on the left, then three links with spacing, and on the far right a "Login" button. Dark background, light text, elements vertically centered.
Note
display: flex; align-items: center; gap: …; on the container and margin-left: auto on the button.
Show sample solution
<!DOCTYPE html>
<style>
body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
body { margin: 0; }
.nav { display: flex; align-items: center; gap: 16px; padding: 12px 20px; background: #1f2937; color: #fff; }
.nav a { color: #d1d5db; text-decoration: none; }
.nav a:hover { color: #fff; }
.logo { font-weight: 800; }
.nav button { margin-left: auto; padding: 8px 14px; border: 0; border-radius: 8px; cursor: pointer; }
</style>
<nav class="nav">
<span class="logo">LOGO</span>
<a href="#">Home</a><a href="#">Courses</a><a href="#">Contact</a>
<button>Login</button>
</nav>5. Responsive Card Grid Intermediate
Show 6 cards in a grid: one column on narrow screens, two from 500px, three columns from 800px – with media queries (mobile first) and 16px spacing.
Note
Base: display: grid; gap: 16px;. Then @media (min-width: 500px) { … grid-template-columns: repeat(2, 1fr) } and likewise for 800px.
Show sample solution
<!DOCTYPE html>
<style>
body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.raster { display: grid; gap: 16px; }
.raster div { background: #dbeafe; padding: 24px; border-radius: 10px; text-align: center; }
@media (min-width: 500px) { .raster { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 800px) { .raster { grid-template-columns: repeat(3, 1fr); } }
</style>
<div class="raster">
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div>
</div>6. Accessible Price Table Intermediate
Create a table with a title, a header row (Product, Price) and three data rows. Use <caption>, <thead>, <tbody>, th scope. Style: collapsed borders, zebra stripes, prices right-aligned.
Note
border-collapse: collapse, tbody tr:nth-child(even) { background: … } and a class .preis { text-align: right }.
Show sample solution
<!DOCTYPE html>
<style>
body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
table { border-collapse: collapse; min-width: 260px; }
caption { text-align: left; font-weight: 700; padding-bottom: 6px; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #ddd; }
thead th { background: #222; color: #fff; }
tbody tr:nth-child(even) { background: #f3f4f6; }
.preis { text-align: right; }
</style>
<table>
<caption>Price List</caption>
<thead><tr><th scope="col">Product</th><th scope="col" class="preis">Price</th></tr></thead>
<tbody>
<tr><th scope="row">Coffee</th><td class="preis">8.50 €</td></tr>
<tr><th scope="row">Tea</th><td class="preis">6.20 €</td></tr>
<tr><th scope="row">Cake</th><td class="preis">3.90 €</td></tr>
</tbody>
</table>7. Theme Switcher with Variables Advanced
Build a page with a light and a dark theme via CSS variables (background, text, accent). A button switches the data-theme attribute on the <html> element with JavaScript. Use a smooth transition for the color changes and respect prefers-reduced-motion.
Note
Define :root { --bg … } and [data-theme="dunkel"] { … }. In the script: document.documentElement.dataset.theme = ….
Show sample solution
<!DOCTYPE html>
<style>
:root { --bg: #fff; --text: #1a1a1a; --akzent: #2b59c3; }
[data-theme="dunkel"] { --bg: #14161c; --text: #ececec; --akzent: #8fb0ff; }
body { font-family: system-ui, sans-serif; margin: 12px; background: var(--bg); color: var(--text);
transition: background .3s, color .3s; }
button { background: var(--akzent); color: var(--bg); border: 0; padding: 8px 14px; border-radius: 8px; cursor: pointer; }
@media (prefers-reduced-motion: reduce) { body { transition: none; } }
</style>
<button id="wechsel">Switch theme</button>
<p>Sample text showing the colors.</p>
<script>
const root = document.documentElement;
document.getElementById('wechsel').addEventListener('click', () => {
root.dataset.theme = root.dataset.theme === 'dunkel' ? 'hell' : 'dunkel';
});
</script>8. Accessible Collapsible Section with Animation Advanced
Build a section with a button and content that is linked via aria-expanded/aria-controls and is expanded and collapsed with JavaScript. An arrow in the button should rotate via a transition; the focus must be visible and animations may be dropped for "reduced motion".
Note
Set hidden on the content and toggle aria-expanded along with it. The arrow is an ::after element with transform: rotate(180deg) in the [aria-expanded="true"] state.
Show sample solution
<!DOCTYPE html>
<style>
body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
button { font: inherit; padding: 8px 14px; border: 2px solid #333; border-radius: 8px; background: #fff; cursor: pointer; }
button::after { content: "▾"; display: inline-block; margin-left: 8px; transition: transform .2s; }
button[aria-expanded="true"]::after { transform: rotate(180deg); }
button:focus-visible { outline: 3px solid #f59e0b; outline-offset: 3px; }
#inhalt { margin-top: 10px; padding: 10px; background: #eef; border-radius: 8px; }
@media (prefers-reduced-motion: reduce) { button::after { transition: none; } }
</style>
<button id="knopf" aria-expanded="false" aria-controls="inhalt">Show more</button>
<div id="inhalt" hidden>Hidden content</div>
<script>
const knopf = document.getElementById('knopf');
const inhalt = document.getElementById('inhalt');
knopf.addEventListener('click', () => {
const offen = knopf.getAttribute('aria-expanded') === 'true';
knopf.setAttribute('aria-expanded', String(!offen));
inhalt.hidden = offen;
});
</script>