Bitpath

HTML & CSS Layout

Learn the two languages every page is made of: HTML that says what each piece is, and the CSS rules that decide which style wins and where every box lands. You work out sizes, winners and positions from the rules, the way the browser does.

  • Beginner
  • 4 units, 12 lessons, 156 exercises
  • HTML, CSS
  • No experience needed

Unit 01 · 3 lessons

Structure

Write HTML that says what each piece of a page is

  1. > start01Elements
  2. 02Meaning
  3. BOSSA form
Unit prize
  1. 01
    Elements

    Tags, attributes and the tree they build

  2. 02
    Meaning

    Elements chosen for what content is, not how it looks

  3. BOSS
    A form

    One sign-up form, from label to what is sent

Read the unit guide

HTML describes what things are: this is a heading, this is a link, this is a form field. An element is an opening tag, its content and a closing tag, and attributes in the opening tag configure it (href, src, alt, name). Elements nest, so every page is a tree with parents, children and siblings. A few elements, such as <img>, <input>, <br> and <meta>, are void: they hold no content and take no closing tag.

Every page has the same skeleton: <!doctype html>, then <html> holding a <head> (information about the page, such as its <title>) and a <body> (everything the user sees). Browsers never refuse broken HTML. They repair it by fixed rules, which is why a missing closing tag gives a strange tree instead of an error.

Without CSS, a block element (<p>, <h1>, <div>, <ul>) starts on a new line and takes the full width, and an inline element (<a>, <span>, <em>, <img>) flows inside a line of text.

Pick elements for their meaning, never for their default look. Headings (<h1> to <h6>) form an outline with no skipped levels, landmarks (<header>, <nav>, <main>, <footer>) name the regions of a page, a link goes somewhere and a <button> does something. Screen readers, search engines and keyboards rely on all of it. A <div> with a click handler gets none of it for free.

Every meaningful image needs alt text that says what the image tells a sighted reader. A purely decorative image gets alt="" so it is skipped.

A form sends name=value pairs to the URL in its action. A control without a name is not sent. Every control needs a <label>, tied with for and id or by wrapping the control. Input types and the required and pattern attributes let the browser check values before anything is sent, and a <button> inside a form submits it unless it says type="button".

Unit 02 · 3 lessons

Rules

Predict which CSS rule wins and how big a box ends up

  1. 01Selectors
  2. 02The cascade
  3. BOSSThe box model
Unit prize
  1. 01
    Selectors

    Pick exactly the elements a rule should style

  2. 02
    The cascade

    When rules disagree, one wins by fixed steps

  3. BOSS
    The box model

    Work out the real size of a box from its CSS

Read the unit guide

A CSS rule is a selector and a block of declarations. The selector picks elements: a bare name matches every element of that type, .note matches a class, #total matches one id. A space means "anywhere inside" (nav a), > means "directly inside" (nav > a), a comma lists alternatives, and pseudo-classes such as :hover, :first-child and :nth-child(odd) match by state or position.

When several rules set the same property on one element, the cascade picks a winner. !important beats everything normal, an inline style beats any selector, and then specificity decides: count the ids, then the classes (pseudo-classes and attribute selectors count as classes), then the element types, and compare left to right. One id beats any number of classes. Only on a full tie does the later rule win.

Inheritance is separate and weaker. Text properties such as color and font-size pass from parent to child, but any rule that targets the child directly beats an inherited value, however weak its selector. Box properties such as border, padding and width do not inherit.

Custom properties (--brand: #0a7, read with var(--brand)) are values with a name. They inherit, so a value set on :root is available everywhere and can be replaced for one part of the page.

Every element is a box: content, padding, border, margin. By default width sets the content only, so padding and border make the box bigger. box-sizing: border-box makes width mean the whole box out to the border, which is why most stylesheets start by setting it on everything.

Vertical margins between blocks collapse: the gap is the larger of the two, not the sum. Horizontal margins never collapse. For sizes, px is fixed, % is a share of the parent, rem is a multiple of the root font size (16px unless changed), em a multiple of the element's own font size, and 1vw is 1% of the viewport width.

Unit 03 · 3 lessons

Layout

Place boxes with flexbox, grid and positioning, and predict their sizes

  1. 01Flexbox
  2. 02Grid
  3. BOSSPositioning
Unit prize
  1. 01
    Flexbox

    One line of items, sized by sharing out space

  2. 02
    Grid

    Rows and columns, tracks, and placing items by line

  3. BOSS
    Positioning

    Lift boxes out of the flow and stack them in order

Read the unit guide

Three tools cover nearly every page. Flexbox lays items out along one line. Grid lays them out in rows and columns at once. Positioning takes single boxes out of the normal flow: a badge on a corner, a header that stays put, a dialog over everything.

A flex container (display: flex) has a main axis, the direction its items run in, and a cross axis at right angles to it. justify-content spreads items along the main axis and align-items places them on the cross axis. With flex-direction: column the axes swap and so do the jobs of those two properties.

Flex sizing is arithmetic. Each item starts at its flex-basis (or width). Left-over space is shared out in proportion to flex-grow. If the items are too big, each gives space back in proportion to flex-shrink, which is 1 by default. flex: 1 means "ignore my content size and take an equal share". Remember to take the gaps out before sharing.

A grid (display: grid) is defined by its tracks: grid-template-columns: 200px 1fr 2fr. Fixed tracks and gaps are subtracted first and the fr units share the rest. Items fill the cells row by row. To place one yourself, name the lines it runs between: lines are numbered from 1, 1 / 3 covers two columns, span 2 covers two from wherever the item starts, and 1 / -1 is the full width. repeat(auto-fit, minmax(200px, 1fr)) makes as many columns as fit, with no media query.

position: relative nudges a box and keeps its place reserved. absolute removes it from the flow and places it against the nearest ancestor that is itself positioned, which is why the parent of a badge gets position: relative. fixed places it against the viewport, and sticky scrolls normally until it reaches its offset and then stays.

z-index orders overlapping boxes, but only positioned ones (and flex or grid items). A positioned box with a z-index starts a stacking context: its children are layered inside it, so a child with z-index: 999 still sits below anything that beats its parent.

Unit 04 · 3 lessons

Every screen

Make one page work at any width, for any reader

  1. 01Responsive rules
  2. 02Type and colour
  3. BOSSA component
Unit prize
  1. 01
    Responsive rules

    The viewport tag, media queries, and sizes that flex

  2. 02
    Type and colour

    Readable text, enough contrast, and a dark theme

  3. BOSS
    A component

    Build a card and a nav bar, then debug a broken layout

Read the unit guide

One page has to work on a 360px phone and on a 2000px monitor, for someone with sharp eyes and for someone using a screen reader. You do not write several pages. You write one, with rules that adapt.

It starts with one line of HTML. Without <meta name="viewport" content="width=device-width">, a phone pretends to be a desktop about 980px wide and shrinks the result, and no media query for small screens ever matches.

A media query, @media (min-width: 600px) { ... }, switches a block of rules on when its condition holds. It adds no specificity, so the cascade works as usual and later rules win. Hence the mobile-first habit: plain rules for the narrow layout, then min-width queries from the smallest up, each changing only what a wider screen needs.

Many things need no query at all. clamp(16px, 4vw, 32px) gives a size that grows with the window between two limits. max-width: 100% with height: auto keeps an image inside its column and in proportion. A grid with repeat(auto-fit, minmax(200px, 1fr)) chooses its own number of columns.

Text has to be readable: a font stack that ends in a generic family, a unitless line-height near 1.5, lines capped at about 65 characters, sizes in rem so the reader's own setting counts, and a contrast of at least 4.5:1 between body text and its background.

Colours belong in custom properties on :root. A prefers-color-scheme: dark query can then redefine those few values, and the whole page follows. The last lesson builds a card and a navigation bar this way, then debugs a layout by doing what the browser does: find the rule that won, and add up the boxes.

Learn HTML & CSS Layout on your phone.

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy