html_cheatsheet.txt

Cheat Sheet

The tags and properties we use most in class. For anything else, check MDN.

Page structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Page Title</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- Visible content goes here -->
</body>
</html>

Common tags

TagWhat it does
<h1> to <h6>Headings. Use one <h1> per page, then go down in order.
<p>A paragraph of text
<a href="page.html">A link. Add target="_blank" to open in a new tab.
<img src="pic.png" alt="...">An image. No closing tag. Always add alt.
<ul> / <ol> + <li>Bulleted / numbered list, with a list item inside
<strong> / <em>Important (bold) / emphasised (italic) text
<div>A generic box for grouping things to style
<span>A generic inline wrapper for styling part of a line
<br>A line break inside text (not for spacing!)
<!-- comment -->A note in your code that doesn't show on the page

Semantic layout tags

TagUse it for
<header>The top of the page: logo, site name, nav
<nav>Your main menu links
<main>The main content. Only one per page.
<section>A themed group of content with its own heading
<article>A self-contained item, like a blog post or project
<aside>Side content, like a sidebar
<footer>The bottom of the page: copyright, contact links

Links to other files

<a href="about.html">          <!-- same folder -->
<a href="projects/game.html">  <!-- inside a folder -->
<a href="../index.html">       <!-- up one folder -->
<a href="https://example.com"> <!-- another website -->
css_cheatsheet.txt

How a CSS rule works

selector {
    property: value;
}

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

Selectors

SelectorSelectsHTML it matches
pEvery <p><p>
.cardEverything with that class<div class="card">
#logoThe one element with that ID<img id="logo">
nav aLinks inside a <nav><nav><a>
h1, h2Both <h1> and <h2>
a:hoverA link while the mouse is over it

Common properties

PropertyExampleWhat it does
colorcolor: #860c5d;Text colour
background-colorbackground-color: pink;Background colour
font-familyfont-family: Verdana, sans-serif;Font, with backups
font-sizefont-size: 1.2rem;Text size
text-aligntext-align: center;Left / centre / right align text
marginmargin: 20px;Space outside the border
paddingpadding: 10px;Space inside the border
borderborder: 2px solid black;Width, style, colour
border-radiusborder-radius: 8px;Rounded corners
width / max-widthmax-width: 900px;How wide something can be
displaydisplay: flex;How an element is laid out

The box model

┌─────────── margin ───────────┐
│  ┌──────── border ────────┐  │
│  │  ┌───── padding ────┐  │  │
│  │  │     content      │  │  │
│  │  └──────────────────┘  │  │
│  └────────────────────────┘  │
└──────────────────────────────┘

Flexbox: items in a row

.container {
    display: flex;
    gap: 16px;                /* space between items */
    justify-content: center;  /* along the row */
    align-items: center;      /* up and down */
    flex-wrap: wrap;          /* wrap onto new lines */
}

Media query: styles for phones

@media (max-width: 600px) {
    .container {
        flex-direction: column;  /* stack instead of row */
    }
}

Colour formats

color: red;                  /* name */
color: #ff0000;              /* hex */
color: rgb(255, 0, 0);       /* red, green, blue */
color: rgba(255, 0, 0, 0.5); /* with transparency */