Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Web applications and APIs

Workbook · Easy · 5 hours

HTML & CSS Basics

Twelve short chapters that build a real webpage, one HTML or CSS idea at a time.

  • html
  • css
  • flexbox
  • forms
  • web-development
On this page 12 sections
  1. ·Chapter 1 — What HTML Does
  2. ·Chapter 2 — The HTML Document Skeleton
  3. ·Chapter 3 — Text and Formatting
  4. ·Chapter 4 — Links and Images
  5. ·Chapter 5 — Lists and Tables
  6. ·Chapter 6 — Forms
  7. ·Chapter 7 — What CSS Does
  8. ·Chapter 8 — CSS Selectors and Specificity
  9. ·Chapter 9 — The Box Model
  10. ·Chapter 10 — Colours, Fonts and Typography
  11. ·Chapter 11 — Flexbox Layout
  12. ·Chapter 12 — Build a Complete Page

The web starts with two languages that work as a pair. HTML names each piece of a page — a heading, a list, a form field — and CSS gives those pieces their look and position. A security engineer who reads these two well can spot the difference between a layout bug and a cross-site scripting hole, follow a phishing kit, and build a clean report page. This workbook walks through twelve short chapters. Each one adds one idea, shows a worked example, and asks a few practice questions. By the end the reader has written a small webpage from scratch and can read the markup behind most sites.

Tools needed: a text editor (VS Code is free) and any web browser. No install, no account and no terminal. Create a file called index.html, open it in a browser, and reload the page after every change.

This workbook has twelve chapters. Each chapter has objectives, an explanation, a worked example and practice questions. Work through them in order. Each chapter uses what the one before it taught.

Chapter 1 — What HTML Does

What You'll Learn

  • Explain what HTML does and what it does not do
  • Name the three layers of a webpage
  • Describe the request and response that load a page

Explanation

HTML (HyperText Markup Language) describes the content and structure of a page: headings, paragraphs, images, links, forms. HTML is a markup language, not a programming language. It has no loops, no variables and no logic.

A webpage has three layers:

LayerJob
HTMLContent and structure
CSSLook: colour, spacing, layout
JavaScriptBehaviour: what happens when the user clicks or types

This workbook covers the first two. A later workbook covers JavaScript.

When you open a URL, the browser sends a request to a server. The server sends back a response: usually an HTML file. The browser reads the HTML and draws the page on screen. This request-response cycle is the same for every page on the web.

Worked example

htmlindex.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <title>My First Page</title>
</head>
<body>
  <h1>Hello, World!</h1>
  <p>This is my first webpage.</p>
</body>
</html>

Save this file as index.html. Open it with your browser (double-click the file, or drag it into a browser window).

What you see in the browser: a large heading that reads "Hello, World!", and a line of text below it. The browser tab title reads "My First Page". No colour, no layout — CSS has not been added yet.

Practice questions

1. Is HTML a programming language? Why or why not?

Answer

No. HTML is a markup language. It describes content and structure. It has no logic, no loops and no variables.

2. Which layer of a webpage controls what happens when the user clicks a button?

Answer

JavaScript. HTML holds the button. CSS styles it. JavaScript runs code when the user clicks it.

3. You open https://example.com/ in a browser. What two messages pass between your browser and the server?

Answer

Your browser sends a request for the page. The server sends back a response, usually an HTML file, which the browser then draws on screen.

Chapter 2 — The HTML Document Skeleton

What You'll Learn

  • Write the five parts every HTML file needs
  • Explain the job of <head> and the job of <body>
  • Explain why the viewport meta tag matters on a phone

Explanation

Every HTML file starts with the same skeleton:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Page Title</title>
</head>
<body>
  <!-- visible content goes here -->
</body>
</html>
PartJob
<!DOCTYPE html>Tells the browser this is HTML5
<html lang="en">The root element. lang names the page's language
<head>Metadata: information about the page, not shown on screen
<meta charset="UTF-8"/>Tells the browser how to read text, including accented letters and symbols
<meta name="viewport" .../>Tells a phone browser to use the screen's real width, not a zoomed-out desktop view
<title>The text shown in the browser tab
<body>Everything the user sees

Without the viewport meta tag, a phone browser shrinks the whole page to fit a desktop-sized view. Text becomes too small to read without zooming in.

Worked example

htmlindex.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>About Me</title>
</head>
<body>
  <h1>About Me</h1>
</body>
</html>

What you see in the browser: the tab reads "About Me". The page shows one heading. Open this file on a phone (or resize a desktop browser window to a narrow width) and the heading stays readable, because of the viewport meta tag.

Practice questions

1. A file has no <!DOCTYPE html> line. What goes wrong?

Answer

Without a doctype, old browsers can render the page in "quirks mode": an old, inconsistent rendering mode built for pages from the 1990s. Always start a file with <!DOCTYPE html>.

2. What goes inside <head>, and what goes inside <body>?

Answer

<head> holds metadata: the title, the character encoding, the viewport setting and links to CSS. None of it appears on the page. <body> holds everything the user sees: headings, text, images, links and forms.

3. A page has no viewport meta tag. What does a visitor on a phone see?

Answer

The phone browser shows a zoomed-out, desktop-sized view of the page. The visitor must pinch and zoom to read the text.

Chapter 3 — Text and Formatting

What You'll Learn

  • Use headings <h1> to <h6> in the correct order
  • Choose between a paragraph and a line break
  • Name four inline tags that format text and what each means

Explanation

Headings rank content. <h1> is the most important heading on the page — use it once, for the page's main title. <h2> marks the next level down, <h3> the level below that, and so on to <h6>. Do not skip a level only to make text look smaller: use CSS for that instead (Chapter 10).

A <p> holds one paragraph. The browser puts space above and below it automatically. A <br/> forces a line break inside the same paragraph, with no extra space. Use <br/> rarely. Write most line breaks as new paragraphs.

Four common inline tags change the meaning or look of a word or phrase:

TagMeaning
<strong>Marks text as important. Browsers show it bold
<em>Marks text as emphasised. Browsers show it italic
<code>Marks text as code, such as a filename or a command
<mark>Highlights text, as if marked with a highlighter pen

<strong> and <em> describe meaning, not only looks. A screen reader changes its voice for <strong> text. If you only want bold or italic text with no added meaning, CSS can do that (Chapter 10).

Worked example

htmlindex.html
<h1>Weather Report</h1>
<h2>Today</h2>
<p>It is sunny with a light wind. The forecast calls for <strong>heavy rain</strong> tomorrow.</p>
<p>Carry an umbrella.<br/>Check the forecast again in the evening.</p>
<p>Run the command <code>forecast --tomorrow</code> for a full report.</p>

What you see in the browser: a large heading "Weather Report", a smaller heading "Today", then a paragraph where "heavy rain" stands out in bold. The next paragraph shows two lines with no blank space between them, because <br/> only breaks the line — it does not start a new paragraph.

Practice questions

1. What is the difference between <br/> and a new <p>?

Answer

<br/> breaks the line inside the same paragraph, with no extra space. A new <p> starts a new paragraph, with space above and below it.

2. A page uses <h1>, then jumps straight to <h4> only to make the next line of text look smaller. Why is this wrong?

Answer

Heading tags rank content for every visitor, including screen reader users who navigate by heading level. Skipping levels only to change the size breaks that structure. Use CSS to change text size instead.

3. Which tag is correct for the text "Warning: do not disconnect the power"?

Answer

<strong>. The text is important, not merely emphasised, so <strong> is the correct tag.

What You'll Learn

  • Write a link to another page, to a section on the same page, and to an email address
  • Explain the alt attribute on an image and why it matters
  • Open a link in a new tab safely

Explanation

A link uses the <a> tag with an href attribute, which names the destination:

<a href="https://example.com">Visit example.com</a>
<a href="about.html">About page</a>
<a href="#section1">Jump to a section on this page</a>
<a href="mailto:hello@example.com">Email us</a>

A link to another site uses a full URL. A link to another file on the same site uses a relative path, such as about.html. A link that starts with # jumps to the element on the same page whose id matches that name.

target="_blank" opens a link in a new tab. When you use it, also add rel="noopener noreferrer". Without it, the new page can, in some older browsers, access and change the tab it was opened from. This costs nothing to add and removes the risk:

<a href="https://example.com" target="_blank" rel="noopener noreferrer">Open in a new tab</a>

An image uses <img> with two required attributes: src (the file to show) and alt (text shown if the image fails to load, and read aloud by a screen reader):

<img src="photo.jpg" alt="A sunset over the sea"/>

Never leave alt empty on an image that carries meaning. A missing alt leaves a screen reader user with no idea what the image shows.

Worked example

htmlindex.html
<nav>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
  <a href="https://example.com/docs" target="_blank" rel="noopener noreferrer">Docs (new tab)</a>
</nav>

<h2 id="about">About</h2>
<img src="team-photo.jpg" alt="Four people standing in front of an office building"/>

<h2 id="contact">Contact</h2>
<p>Email: <a href="mailto:hello@example.com">hello@example.com</a></p>

What you see in the browser: three links in a row. Clicking "About" or "Contact" jumps down the same page, because their href values start with # and match the id of a heading below. Clicking "Docs" opens a second browser tab. If team-photo.jpg is missing, the browser shows the alt text in its place.

Practice questions

1. Why does target="_blank" need rel="noopener noreferrer"?

Answer

Without it, in some older browsers the newly opened page can reach back into the tab that opened it. rel="noopener noreferrer" removes that link between the two tabs.

2. An <img> tag has no alt attribute. Who is affected, and how?

Answer

A screen reader user hears nothing useful for that image. A sighted user whose image fails to load sees a broken icon with no description. Always set alt to a short description of the image.

3. Write a link that jumps to a heading <h2 id="pricing"> further down the same page.

Answer

<a href="#pricing">Pricing</a>. The href value is # followed by the target element's id.

Chapter 5 — Lists and Tables

What You'll Learn

  • Choose between an ordered list and an unordered list
  • Nest one list inside another
  • Build a table with a header row

Explanation

An unordered list (<ul>) shows bullet points, for items with no required order. An ordered list (<ol>) shows numbers, for items with a sequence, such as steps. Each item in either list is an <li>.

<ul>
  <li>HTML</li>
  <li>CSS</li>
</ul>

<ol>
  <li>Open the file</li>
  <li>Edit the file</li>
  <li>Save the file</li>
</ol>

A list can nest inside a list item, for a sub-category:

<ul>
  <li>Fruit
    <ul>
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </li>
</ul>

A table (<table>) holds rows (<tr>) of cells. A header cell is <th>, inside <thead>. A data cell is <td>, inside <tbody>. Use <th>, not <td>, for a header: it tells a screen reader, and a search engine, which cell is a column title.

<table>
  <thead>
    <tr><th>Name</th><th>City</th></tr>
  </thead>
  <tbody>
    <tr><td>Alice</td><td>London</td></tr>
    <tr><td>Bob</td><td>Paris</td></tr>
  </tbody>
</table>

Worked example

htmlindex.html
<h2>Learning Plan</h2>
<ol>
  <li>Finish Chapter 5</li>
  <li>Build the page in Chapter 12</li>
</ol>

<h2>Tools</h2>
<ul>
  <li>Editor
    <ul>
      <li>VS Code</li>
      <li>Any plain text editor</li>
    </ul>
  </li>
  <li>Browser</li>
</ul>

<table>
  <thead>
    <tr><th>Tag</th><th>Use</th></tr>
  </thead>
  <tbody>
    <tr><td>ul</td><td>Unordered list</td></tr>
    <tr><td>ol</td><td>Ordered list</td></tr>
  </tbody>
</table>

What you see in the browser: a numbered list with two steps, a bulleted list where "Editor" has two sub-bullets indented beneath it, and a two-column table with a bold header row.

Practice questions

1. You list the steps to install a program. Which do you use, <ul> or <ol>? Why?

Answer

<ol>. The steps have a required order, so a numbered list is correct.

2. Why use <th> instead of <td> for a table's header row?

Answer

<th> tells the browser, a screen reader and a search engine that the cell is a column or row title, not data. Browsers also show <th> text as bold and centred by default.

3. Write the HTML for a list item "Vegetables" with a nested list of "Carrot" and "Pea" beneath it.

Answer
<li>Vegetables
  <ul>
    <li>Carrot</li>
    <li>Pea</li>
  </ul>
</li>

Chapter 6 — Forms

What You'll Learn

  • Build a form with labelled text, email and choice inputs
  • Link a <label> to its input with for and id
  • Explain why client-side checks like required are a convenience, not a security control

Explanation

A form collects input and sends it to a server when the user selects a submit button:

<form action="/submit" method="POST">
  ...
  <button type="submit">Submit</button>
</form>

action names the URL the form sends data to. method is usually POST for a form that changes data, and GET for one that only searches or filters.

Every input needs a <label>. Link a label to its input with for, matching the input's id. This lets a user select the label text to focus the input, and it tells a screen reader what the input is for:

<label for="email">Email:</label>
<input type="email" id="email" name="email" required/>

Common input types:

TypeUse
textA short line of free text
emailAn email address. The browser checks the format before submitting
passwordHides the characters the user types
numberA numeric value, with optional min and max
checkboxAn on/off choice
radioOne choice from a small set, when inputs share the same name

A dropdown uses <select> with <option> children. A multi-line field uses <textarea>.

The required attribute, and the browser's check on type="email", help the user catch a mistake before they submit. They run in the user's browser, so a user can turn off JavaScript or send a request without a browser at all and skip them completely. A server must check every value it receives again, on its own side. This workbook does not cover server code, but keep the rule: never trust a check that only runs in the browser.

Worked example

htmlindex.html
<form action="/submit" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" placeholder="Jordan Lee"/>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required/>

  <label for="country">Country:</label>
  <select id="country" name="country">
    <option value="pk">Pakistan</option>
    <option value="us">United States</option>
  </select>

  <label for="updates">
    <input type="checkbox" id="updates" name="updates" value="yes"/>
    Send me updates
  </label>

  <button type="submit">Submit</button>
</form>

What you see in the browser: a text field, an email field, a dropdown and a checkbox, each with a visible label. Select a label text and its input gains focus. Select "Submit" with the email field empty, and the browser blocks the submit and highlights the email field, because of required.

Practice questions

1. Why link a <label> to its input with for and id, instead of only using a placeholder?

Answer

A placeholder disappears once the user starts typing, and a screen reader does not reliably announce it as the input's name. A linked <label> stays visible and is read aloud as the input's label, and selecting the label text focuses the input.

2. A form checks required on the email field in the browser only. A user sends a request to the same URL without using the form at all. Does the empty-email check still run?

Answer

No. required is a browser-side check. A request sent without the browser skips it completely. The server must check the value again on its own side.

3. Which input type fits a field for a user's age, limited to 1 through 120?

Answer

<input type="number" name="age" min="1" max="120"/>.

Chapter 7 — What CSS Does

What You'll Learn

  • Explain what CSS controls, and what it does not control
  • Name the three ways to add CSS to a page
  • Write a basic CSS rule: selector, property and value

Explanation

CSS (Cascading Style Sheets) controls how HTML looks: colour, font, size, spacing and layout. HTML without CSS still shows all its content, with no style applied.

There are three ways to add CSS to a page:

  1. External stylesheet (the usual choice): a separate .css file, linked from <head>.
  2. Internal: a <style> block inside <head>.
  3. Inline: a style attribute on one element. Avoid this — it mixes content and style, and is hard to reuse.
<!-- External -->
<link rel="stylesheet" href="style.css"/>

<!-- Internal -->
<style>
  h1 { color: red; }
</style>

<!-- Inline (avoid) -->
<h1 style="color: red;">Hello</h1>

A CSS rule has a selector (what to style) and a block of property: value; pairs:

h1 {
  color: red;
  font-size: 32px;
}

This rule selects every <h1> on the page and sets its text colour and size.

Worked example

htmlindex.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <title>CSS Test</title>
  <style>
    h1 { color: darkblue; }
    p  { color: gray; font-size: 18px; }
  </style>
</head>
<body>
  <h1>Styled Heading</h1>
  <p>Styled paragraph text.</p>
</body>
</html>

What you see in the browser: the heading shows in dark blue. The paragraph shows in gray, slightly larger than the browser's default text size. Delete the <style> block and reload the page — the text goes back to plain black, proving the colour came from CSS, not HTML.

Practice questions

1. Which of the three ways to add CSS best separates content from style, and why?

Answer

An external stylesheet. It keeps all style rules in one file, reusable across many pages, with none of it mixed into the HTML.

2. Write a CSS rule that makes every paragraph's text gray.

Answer
p { color: gray; }

3. A page has no CSS at all. Does the content still display?

Answer

Yes. HTML always displays its content. Without CSS, the browser uses its own default look: black text, a plain font and no custom layout.

Chapter 8 — CSS Selectors and Specificity

What You'll Learn

  • Select elements by tag, class and ID
  • Combine selectors for a descendant or a group
  • State the order of specificity when two rules conflict

Explanation

SelectorTargetsExample
ElementEvery element of that tagp { }
ClassEvery element with that class.card { }
IDThe one element with that ID#header { }
DescendantAn element inside another.container p { }
GroupSeveral selectors at onceh1, h2, h3 { }

A class is written class="card" in HTML and selected with .card in CSS. An ID is written id="header" and selected with #header. An ID must be unique on the page; a class can be used on many elements.

Pseudo-classes select an element in a certain state:

a:hover { color: red; }        /* mouse is over the link */
input:focus { border-color: blue; }  /* input is selected */

Specificity decides which rule wins when two rules target the same element. From weakest to strongest:

Element  <  Class  <  ID  <  Inline style

A rule with #header beats a rule with .card, even if the class rule comes later in the file.

Worked example

cssstyle.css
p { color: black; }
.highlight { color: orange; }
#warning { color: red; }
<p>Plain text.</p>
<p class="highlight">Highlighted text.</p>
<p id="warning" class="highlight">Both a class and an ID.</p>

What you see in the browser: the first line shows black. The second shows orange, because .highlight beats the plain p rule. The third shows red, not orange, because #warning is an ID selector and beats the class selector .highlight, even though both rules match that element.

Practice questions

1. A <button> has class="btn" and id="submit-btn". One rule is .btn { background: gray; }, another is #submit-btn { background: green; }. Which colour wins?

Answer

Green. An ID selector is more specific than a class selector.

2. Write a selector that targets only <a> elements while the mouse is over them.

Answer
a:hover { }

3. Write a selector that targets every <p> inside an element with class="container".

Answer
.container p { }

Chapter 9 — The Box Model

What You'll Learn

  • Name the four layers of the CSS box model
  • Use shorthand to set padding or margin on all four sides
  • Explain what box-sizing: border-box changes

Explanation

Every HTML element is a box with four layers, from the inside out: content, padding, border, margin.

div {
  width: 300px;
  height: 200px;
  padding: 20px;        /* space inside the border */
  border: 2px solid black;
  margin: 30px;          /* space outside the border */
}

Shorthand sets all four sides at once, in the order top, right, bottom, left (clockwise):

padding: 10px 20px 10px 20px; /* top right bottom left */
padding: 10px 20px;           /* top/bottom  left/right */
padding: 10px;                /* all four sides */

By default, width and height set the size of the content only. Padding and border add to that size. On the div above, the rendered width is 300 (content) + 20 + 20 (padding, both sides) + 2 + 2 (border, both sides) = 344 pixels.

box-sizing: border-box changes this: width and height then include the padding and the border. The rendered width stays exactly 300 pixels. Most developers set this on every element:

* {
  box-sizing: border-box;
}

Worked example

cssstyle.css
.card {
  width: 300px;
  padding: 20px;
  border: 2px solid black;
  margin: 10px;
}
<div class="card">A card with content, padding, border and margin.</div>

What you see in the browser: a box of text with visible space between the text and its black border (the padding), and visible space between the border and whatever is next to it (the margin). Without box-sizing: border-box, the box's full rendered width is wider than 300 pixels, because the browser adds the padding and border on top of it.

Practice questions

1. A div has width: 300px, padding: 20px and no box-sizing rule. What is its rendered width?

Answer

340 pixels: 300 of content plus 20 pixels of padding on the left and 20 on the right. Without a border, there is nothing else to add.

2. The same div gets box-sizing: border-box. What is its rendered width now?

Answer

300 pixels. border-box makes width include the padding and the border, so the content area shrinks to fit inside the stated 300 pixels.

3. Write shorthand for 10px of padding on top and bottom, and 20px on left and right.

Answer
padding: 10px 20px;

Chapter 10 — Colours, Fonts and Typography

What You'll Learn

  • Write a colour as a named colour, a hex code, and rgba()
  • Set a font with a fallback chain
  • Explain why rem is usually better than px for font size

Explanation

CSS accepts colour in several forms:

color: red;                    /* named colour */
color: #dc2626;                /* hex code */
color: rgb(220, 38, 38);       /* red, green, blue, 0–255 each */
color: rgba(220, 38, 38, 0.5); /* same, with 50% opacity */

A font-family lists fonts in order of preference. The browser uses the first one it has, falling back to the next:

font-family: 'Inter', Arial, sans-serif;

font-size can use px (an exact pixel size) or rem (relative to the page's root font size, usually 16px by default). 1rem equals 16px unless the user or the page changes the root size. Using rem lets a user who sets a larger default text size in their browser see every rem-sized element grow with it. A px size stays fixed regardless of the user's settings.

body {
  font-family: 'Inter', Arial, sans-serif;
  font-size: 1rem;      /* scales with the user's settings */
  line-height: 1.6;     /* space between lines, as a multiple of the font size */
}

Worked example

cssstyle.css
body {
  font-family: Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
  color: #1a1a1a;
}
h1 {
  color: #dc2626;
  font-weight: bold;
}

What you see in the browser: body text in a dark, near-black colour, with visible space between lines, in Arial. The <h1> text shows in a red tone. Increase your browser's default font size in its settings and reload — both the body text and the heading grow, because both use relative sizing (rem for the body, and the heading inherits the same scaling).

Practice questions

1. Why might a developer choose rem over px for font size?

Answer

rem scales with the page's root font size, which the user or the browser can change. A user who sets a larger default text size sees rem-sized text grow with it. A px size stays fixed.

2. What does the fourth value in rgba(220, 38, 38, 0.5) control?

Answer

Opacity. 0.5 makes the colour 50% transparent. 1 is fully opaque, 0 is fully transparent.

3. Write a font-family value that tries "Inter" first, then falls back to any sans-serif font.

Answer
font-family: 'Inter', sans-serif;

Chapter 11 — Flexbox Layout

What You'll Learn

  • Turn a container into a flex container
  • Use justify-content and align-items to position children
  • Make a row of items wrap onto a new line on a narrow screen

Explanation

Flexbox lays out the children of one container, in a row or a column. Set display: flex on the container:

.container {
  display: flex;
  flex-direction: row;   /* row (default) or column */
  justify-content: center;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}

justify-content positions children along the main axis (left to right, in a row):

ValueEffect
flex-startItems sit at the start (default)
centerItems sit in the middle
space-betweenItems spread out, with space only between them

align-items positions children across the cross axis (top to bottom, in a row):

ValueEffect
flex-startItems sit at the top
centerItems sit in the middle
stretchItems fill the full height (default)

flex-wrap: wrap lets items that do not fit on one line move to the next line, instead of shrinking or overflowing. gap sets the space between items without needing margin on each one.

Worked example

cssstyle.css
.cards {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}
.card {
  flex: 1;
  min-width: 200px;
  padding: 16px;
  border: 1px solid #ccc;
}
<div class="cards">
  <div class="card">Card one</div>
  <div class="card">Card two</div>
  <div class="card">Card three</div>
</div>

What you see in the browser: three equal-width boxes in a row, with 20 pixels of space between them. Shrink the browser window below about 600 pixels wide, and the cards wrap: one or more move down to a second line. This happens because flex-wrap: wrap is set and each card has a min-width.

Practice questions

1. Which property controls whether flex children sit at the top, middle or bottom of the container?

Answer

align-items. justify-content controls the main-axis position (left, centre, right in a row); align-items controls the cross-axis position (top, centre, bottom in a row).

2. Write CSS that centres a single div both horizontally and vertically inside the full browser window.

Answer
.centred {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

100vh sets the container's height to the full viewport height, so there is room to centre inside.

3. Three cards sit in a flex row. On a narrow phone screen, they squeeze into unreadable slivers instead of stacking. What CSS property is most likely missing?

Answer

flex-wrap: wrap. Without it, flex items shrink to fit one line instead of moving to a new line.

Chapter 12 — Build a Complete Page

What You'll Learn

  • Combine HTML structure with CSS style and layout in one file
  • Apply the box model, colour, fonts and Flexbox from earlier chapters
  • Read and change an existing page's CSS with confidence

Explanation

A real page combines every idea from this workbook: a document skeleton (Chapter 2), text and links (Chapters 3 and 4), a box model for spacing (Chapter 9), colour and fonts (Chapter 10), and Flexbox for layout (Chapter 11). Nothing in this chapter is new — it is the same tags and properties, used together.

Worked example

htmlindex.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>My Portfolio</title>
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: Arial, sans-serif; background: #f5f5f5; color: #333; }

    nav {
      background: #1a1a1a;
      padding: 16px 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    nav a { color: white; text-decoration: none; margin-left: 24px; }
    nav a:hover { color: #dc2626; }

    .hero {
      text-align: center;
      padding: 80px 20px;
      background: #1a1a1a;
      color: white;
    }
    .hero h1 { font-size: 3rem; margin-bottom: 16px; }
    .hero p  { font-size: 1.2rem; color: #aaa; }

    .btn {
      display: inline-block;
      margin-top: 24px;
      padding: 12px 28px;
      background: #dc2626;
      color: white;
      border-radius: 6px;
      text-decoration: none;
      font-weight: bold;
    }
    .btn:hover { background: #b91c1c; }

    .cards {
      display: flex;
      gap: 24px;
      padding: 40px 32px;
      flex-wrap: wrap;
    }
    .card {
      flex: 1;
      min-width: 220px;
      background: white;
      padding: 24px;
      border-radius: 8px;
    }
    .card h3 { margin-bottom: 10px; color: #dc2626; }

    footer {
      text-align: center;
      padding: 24px;
      background: #1a1a1a;
      color: #777;
    }
  </style>
</head>
<body>
  <nav>
    <strong style="color:white">Jordan Lee</strong>
    <div>
      <a href="#about">About</a>
      <a href="#projects">Projects</a>
      <a href="#contact">Contact</a>
    </div>
  </nav>

  <div class="hero">
    <h1>Hi, I'm Jordan</h1>
    <p>Web developer and student</p>
    <a href="#projects" class="btn">See My Work</a>
  </div>

  <div class="cards" id="projects">
    <div class="card">
      <h3>Project 1</h3>
      <p>A webpage built with HTML and CSS.</p>
    </div>
    <div class="card">
      <h3>Project 2</h3>
      <p>A script that automates a daily task.</p>
    </div>
    <div class="card">
      <h3>Project 3</h3>
      <p>A table that tracks weekly progress.</p>
    </div>
  </div>

  <footer id="contact">Contact: hello@example.com</footer>
</body>
</html>

Save the page above as index.html and open it in a browser.

What you see in the browser: a dark navigation bar with three links. Below it is a dark hero section with a heading and a red "See My Work" button. Three white cards sit in a row under that, above a dark footer. Shrink the window and the three cards wrap onto more than one line, because of flex-wrap: wrap in .cards.

Practice questions

1. Which CSS rule in the page above makes the three cards sit in a row instead of stacking one under another?

Answer

.cards { display: flex; }. Flexbox lays the .card children out in a row by default.

2. You want the "See My Work" button to turn blue instead of dark red when the mouse is over it. Which selector do you change?

Answer

.btn:hover. It is the pseudo-class rule that sets the button's colour while the mouse is over it.

3. The nav bar text and the hero heading both use color: white directly. Name one way to avoid repeating that value in every rule.

Answer

Set the shared colour once higher up, such as on body or on a shared class, and let the rules below inherit it, instead of repeating color: white in each rule. A later workbook on advanced CSS covers CSS custom properties (variables), which also solve this.

Workbook complete. You can read and write an HTML skeleton, format text, build links, lists, tables and forms, and style a page with CSS selectors, the box model, colour, fonts and Flexbox. Practise the same skills against a real target in HTML & CSS Lab, then move on to Introduction to Web Applications to see how a server and a browser work together.

Sign in to track progress and send feedback.

Related

This workbook is extra practice for the course Web applications and APIs. Do the lessons first.Open the course