HTML & CSS Toolkit (Cheatsheet)

Context: Full-Stack Roadmap Chapters 1โ€“5 in one place โ€” HTML structure (Ch 1โ€“3) โ†’ CSS styling (Ch 4) โ†’ layout & responsive (Ch 5) ยท covers everything needed to build a styled, responsive, multi-page static site before JavaScript begins. Read protocol: scan tables โ†’ attempt the practice blank โ†’ re-read only the section where you failed.

Quick Revision

  • ๐ŸŽฏ Objective: build a complete, responsive, multi-page website from scratch โž” semantic HTML structure + CSS styling + Flexbox/Grid layout + mobile-first media queries.
  • โšก Key Constraint: the box model is the root of 80% of CSS confusion โ€” width means content width by default (padding + border added on top); always start with * { box-sizing: border-box; }. Second biggest source of bugs: specificity โ€” an ID beats a class beats an element selector, and the browser silently picks the winner with no error.

๐ŸŒ How the web works (mental model)

ConceptOne-linerGotcha
Client / Serverbrowser asks (request) โ†’ server answers (response)your HTML/CSS/JS runs on the client (browser)
HTTPthe protocol for requests; methods: GET / POST / PUT / DELETEstatus codes: 200 OK ยท 404 Not Found ยท 401 โ‰  403
DNSturns example.com โ†’ IP address 93.184.216.34the internetโ€™s phone book
Frontend / Backendcode in the browser / code on the serverfull-stack = both
Renderingbrowser reads HTML topโ†’down, builds a tree (DOM), paints pixelsCSS loads in <head> so styles apply before the page paints

๐Ÿ—๏ธ HTML page skeleton (every page starts here)

<!DOCTYPE html>                        <!-- 1. declaration: "this is modern HTML" -->
<html>
  <head>                               <!-- 2. metadata โ€” NOT visible on page -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tab Title</title>           <!-- browser tab + search results -->
    <link rel="stylesheet" href="styles.css">   <!-- external CSS -->
  </head>
  <body>                               <!-- 3. everything the user SEES -->
    ...
  </body>
</html>
  • <!DOCTYPE html> โ€” always first line; no closing tag.
  • <meta viewport> โ€” required for mobile; without it, phones render at 980px and zoom out.
  • <link> loads an external stylesheet โ€” the standard for all real projects.

๐Ÿ“ HTML elements โ€” content structure

ElementMicro-syntaxJob / gotcha
Headings<h1> โ€“ <h6>hierarchy, not size; never skip levels (h1โ†’h3 is wrong); one <h1> per page
Paragraph<p>text</p>block-level; browser adds margin above/below automatically
Link<a href="url">text</a>href can be absolute (https://โ€ฆ) or relative (about.html)
Image<img src="path" alt="desc" width="400">no closing tag; alt is required (accessibility + SEO); src is relative or URL
Line break<br>no closing tag; use for addresses/poems โ€” separate ideas = separate <p>
Comment<!-- text -->browser ignores it; notes to yourself
Entity&copy; โ†’ ยฉ ยท &amp; โ†’ & ยท &lt; โ†’ <escape special characters

๐Ÿ“‹ Lists

TypeMicro-syntaxWhen
Unordered<ul><li>Item</li></ul>bullets โ€” order doesnโ€™t matter (ingredients)
Ordered<ol><li>Step</li></ol>numbered โ€” sequence matters (cooking steps); browser auto-numbers
Nestedinner <ul>/<ol> goes inside a <li>, before its </li>sub-items; browser changes bullet style per depth

โš ๏ธ Only <li> belongs directly inside <ul>/<ol> โ€” raw text or <p> inside the list container is invalid.

โœ๏ธ Inline formatting

ElementRenders asSemantic meaning
<strong>boldimportant โ€” screen readers stress it
<em>italicemphasis โ€” like vocal stress
<code>monospacecode, filenames, commands
<sub> / <sup>Hโ‚‚O / xยฒsubscript / superscript
<b> / <i>bold / italicno semantic meaning โ€” use <strong>/<em> by default

๐Ÿ“ฎ Forms (gateway to interactivity)

Element / AttributeMicro-syntaxJob / gotcha
Container<form>groups inputs; later add action="url" method="POST" for backend
Text<input type="text" id="x" name="x">single-line; name = key sent to server
Email / Number / Date / Password / URL<input type="email"> etc.browser validates format + shows correct mobile keyboard โ€” free
Radio<input type="radio" name="group" value="v">pick one; same name = same group
Checkbox<input type="checkbox" name="x" value="v">pick many; can share a name
Dropdown<select name="x"><option value="v">Label</option></select>always set value explicitly (display text โ‰  sent value)
Textarea<textarea name="x" rows="5" cols="40"></textarea>multi-line; has a closing tag (unlike <input>)
Label<label for="id">Text</label>for links to inputโ€™s id; always include โ€” accessibility + click-to-focus
Submit<button type="submit">Go</button>triggers form submission
Validationrequired ยท min="1" ยท max="100" ยท placeholder="hint"client-side; browser blocks submit if invalid

๐Ÿ“Š Tables

<table>
  <thead><tr><th>Name</th><th>Score</th></tr></thead>   <!-- header row -->
  <tbody><tr><td>Aisyah</td><td>85</td></tr></tbody>    <!-- data rows -->
  <tfoot><tr><td>Average</td><td>79</td></tr></tfoot>   <!-- summary row -->
</table>
  • <th> = header cell (bold, centered) ยท <td> = data cell.
  • <thead>/<tbody>/<tfoot> โ€” accessibility, CSS targeting, sticky headers, print repetition.
  • โš ๏ธ Never use tables for page layout โ€” use CSS Grid/Flexbox.

๐Ÿ›๏ธ Semantic structure

ElementMeaningRule
<header>site/page intrologo, title, tagline
<nav>navigation linksmenus, breadcrumbs
<main>primary contentone per page
<article>self-contained contentblog post, product card โ€” โ€œmakes sense in an email?โ€
<section>thematic groupingchapter within an article; doesnโ€™t stand alone
<aside>tangential contentsidebar, related links
<footer>closing contentcopyright, contact, sitemap
<div>generic box โ€” no meaninguse only when no semantic element fits; for CSS grouping
โ”Œโ”€โ”€โ”€ <header> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”œโ”€โ”€โ”€ <nav> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”œโ”€โ”€โ”€ <main> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€ <aside> โ”€โ”€โ”€โ”€โ”ค
โ”‚  <section>              โ”‚  sidebar      โ”‚
โ”‚    <article> โ€ฆ </article>โ”‚              โ”‚
โ”‚  </section>             โ”‚              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ””โ”€โ”€โ”€ <footer> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽจ CSS โ€” how to add styles

MethodSyntaxWhen
External (โœ… use this)<link rel="stylesheet" href="styles.css"> in <head>every real project โ€” one file styles the whole site
Internal<style>โ€ฆ</style> in <head>single-page experiments
Inline (โŒ avoid)<p style="color:red;">one-off test only; canโ€™t reuse

๐ŸŽฏ Selectors

SelectorSyntaxTargets
Elementpall <p>
Class.cardall class="card" โ€” primary styling tool
ID#logoone id="logo" โ€” prefer classes for styling
Descendantnav a<a> inside <nav> only
Grouph1, h2, h3all three โ€” same rule
Element+Classp.introonly <p class="intro">
Attributeinput[type="email"]inputs of that type
Pseudo-classa:hover ยท input:focus ยท li:first-child ยท li:last-child ยท li:nth-child(2)element state or position

Specificity (who wins when rules conflict): inline style > ID > class/pseudo-class/attribute > element. Equal specificity โ†’ last rule wins. DevTools Styles panel shows crossed-out lines for overridden rules.

๐ŸŽจ Colors & backgrounds

FormatExampleNotes
Namedtomato, navy, teal~140 names; prototyping
Hex#2c3e50, #fffmost common; 6-digit (RRGGBB) or 3-digit shorthand
RGBrgb(44, 62, 80)decimal equivalent of hex
RGBArgba(0, 0, 0, 0.5)adds transparency (0 = invisible, 1 = solid)
background-color#f5f5f5fills the padding area too
background-imageurl('img.jpg')background-size: cover fills the box (hero images)

๐Ÿ”ค Typography

PropertyJobCommon values
font-familytypeface + fallback chain'Segoe UI', Tahoma, sans-serif โ€” always end with generic
font-sizetext size16px, 1.2rem, 2em
font-weightthicknessnormal (400), bold (700)
line-heightvertical space between lines1.5โ€“1.8 for body text
colortext color#333 (dark grey > pure black for readability)
text-alignhorizontal alignmentleft, center, right
text-decorationunderline etc.none (remove link underline), underline
letter-spacingspace between characters-0.5px for tight headings

๐Ÿ“ฆ The Box Model (the single most important CSS concept)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ MARGIN โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   transparent โ€” pushes others away
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ BORDER โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚   visible edge
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ PADDING โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚  โ”‚   inside the box โ€” bg color fills it
โ”‚  โ”‚  โ”‚       CONTENT             โ”‚  โ”‚  โ”‚   text / image
โ”‚  โ”‚  โ”‚   width ร— height          โ”‚  โ”‚  โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
PropertyMicro-syntaxGotcha
width / height300px, 90%, autodefault = content width only; border-box makes it total width
padding20px (all) ยท 10px 20px (TB, LR) ยท 10px 20px 30px 40px (T R B L โ†ป)inside the box; bg color fills it
border1px solid #ddd ยท border-radius: 8pxvisible edge; radius = rounded corners
marginsame shorthand as padding ยท 0 auto centers a block with max-widthoutside the box; transparent
box-sizing* { box-sizing: border-box; }always add โ€” makes width include padding + border

Shorthand order (clockwise from top): Top โ†’ Right โ†’ Bottom โ†’ Left. Same for padding and margin.

Centering a block: max-width: 800px; margin: 0 auto; โ€” must have a width or max-width set, or auto does nothing.

๐Ÿ“ Units

UnitMeaningUse for
pxfixed pixelsborders, small spacing, exact sizes
%percentage of parentfluid widths (width: 90%)
emrelative to elementโ€™s font-sizespacing that scales with text
remrelative to root (<html>) font-sizefont sizes โ€” most predictable
vh / vw% of viewport height / widthfull-screen sections (height: 100vh)
frfractional unit (Grid only)divide available space (1fr 1fr 1fr = 3 equal cols)

Practical rule: rem for font sizes, px for borders and fine details, % + max-width for containers, fr for Grid columns.

๐Ÿ’ช Flexbox (1D layout โ€” row OR column)

.container {
    display: flex;              /* activates Flexbox */
    flex-direction: row;        /* default: horizontal; column = vertical */
    justify-content: center;    /* distribute along MAIN axis */
    align-items: center;        /* align along CROSS axis */
    flex-wrap: wrap;            /* allow wrapping to next line */
    gap: 20px;                  /* space between items */
}
justify-contentVisual (main axis โ†’)
flex-start[A][B][C]ยทยทยทยทยทยทยทยทยท
flex-endยทยทยทยทยทยทยทยทยท[A][B][C]
centerยทยทยทยท[A][B][C]ยทยทยทยท
space-between[A]ยทยทยทยท[B]ยทยทยทยท[C]
space-evenlyยทยทยท[A]ยทยทยท[B]ยทยทยท[C]ยทยทยท
align-itemsEffect (cross axis)
stretchitems fill container height (default)
centervertically centered
flex-start / flex-endtop / bottom
Item propertyMicro-syntaxJob
flex-growflex-grow: 1absorb leftover space (default 0 = donโ€™t grow)
flex-shrinkflex-shrink: 0prevent shrinking
align-selfalign-self: flex-endoverride align-items for one item

Center anything (holy grail):

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

Navbar pattern:

nav { display: flex; justify-content: space-between; align-items: center; }
/* logo pushed left, links pushed right */

๐Ÿ”ฒ CSS Grid (2D layout โ€” rows AND columns)

.grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;     /* 3 equal columns */
    grid-template-rows: auto 1fr auto;       /* header / stretch / footer */
    gap: 20px;                               /* row + column gap */
}
PatternMicro-syntaxResult
Equal columnsgrid-template-columns: 1fr 1fr 1fr3 equal-width columns
Fixed + flexgrid-template-columns: 200px 1frsidebar 200px + fluid content
Repeatrepeat(3, 1fr)shorthand for 1fr 1fr 1fr
Auto-responsiverepeat(auto-fit, minmax(250px, 1fr))as many cols as fit, min 250px โ€” responsive without media queries

Flexbox vs. Grid โ€” decision rule

ScenarioUse
Row of items (navbar, toolbar, centering)Flexbox
Card grid / image gallery / dashboardGrid
Full page layout (header / sidebar / footer)Grid
Items that wrap into rowseither โ€” Flex wrap or Grid auto-fit
One dimension (row or column)Flexbox
Two dimensions (rows AND columns)Grid

Most pages use both: Grid for the page skeleton, Flexbox for component internals.

๐Ÿ“ฑ Responsive Design

ToolMicro-syntaxJob
Viewport tag<meta name="viewport" content="width=device-width, initial-scale=1.0">required โ€” without it, mobile renders at 980px and zooms out
Fluid containerwidth: 90%; max-width: 1000px; margin: 0 auto;adapts to screen, caps at max, centered
Fluid imagesimg { max-width: 100%; height: auto; }prevents overflow
Auto-fit gridrepeat(auto-fit, minmax(280px, 1fr))responsive grid, zero media queries
Media query@media (min-width: 768px) { โ€ฆ }apply CSS only above a width

Mobile-first (standard practice): write base styles for the smallest screen โ†’ add complexity with min-width queries:

/* Mobile (default) */
.card-grid { grid-template-columns: 1fr; }
nav { flex-direction: column; }
 
/* Tablet 768px+ */
@media (min-width: 768px) {
    .card-grid { grid-template-columns: 1fr 1fr; }
    nav { flex-direction: row; }
}
 
/* Desktop 1024px+ */
@media (min-width: 1024px) {
    .card-grid { grid-template-columns: 1fr 1fr 1fr; }
}
BreakpointWidthDevices
Mobile< 768pxphones
Tablet768px โ€“ 1023pxtablets, small laptops
Desktopโ‰ฅ 1024pxlaptops, monitors

DevTools responsive testing: F12 โ†’ device toggle icon (Ctrl+Shift+M) โ†’ resize or pick a preset device.

๐Ÿงฐ CSS reset / starter (paste at top of every stylesheet)

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
 
img {
    max-width: 100%;
    height: auto;
}
 
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-size: 16px;
    line-height: 1.6;
    color: #333;
}

โœ๏ธ Integration Practice

โš ๏ธ Common Mistakes (top debugging targets)

  • ๐Ÿ’ก Forgot * { box-sizing: border-box; } โž” width: 300px + padding: 20px + border: 5px = 350px actual. Boxes overflow everywhere.
  • ๐Ÿ’ก Forgot viewport meta tag โž” page looks perfect on laptop, tiny text on phones.
  • ๐Ÿ’ก Styles donโ€™t apply โ€” no error shown โž” check in order: (1) typo in class name, (2) missing <link> to stylesheet, (3) specificity conflict (ID beats class), (4) browser cache (Ctrl+Shift+R to hard refresh).
  • ๐Ÿ’ก margin: 0 auto doesnโ€™t center โž” the element has no width or max-width set โ€” auto needs a constraint.
  • ๐Ÿ’ก Images overflow their container โž” add global rule img { max-width: 100%; height: auto; }.
  • ๐Ÿ’ก Headings used for size, not structure โž” <h3> after <h1> because you want smaller text โ†’ wrong; use CSS font-size to control appearance, headings define document outline.
  • ๐Ÿ’ก justify-content vs align-items confusion โž” justify = main axis, align = cross axis. If flex-direction: column, main axis is vertical (swapped).
  • ๐Ÿ’ก Radio buttons not grouped โž” each radio has a different name โ†’ they behave as independent checkboxes.
  • ๐Ÿ’ก Missing <label for="id"> โž” clicking the text doesnโ€™t focus the input; screen readers canโ€™t announce the field.
  • ๐Ÿ’ก Desktop-first media queries โž” writing max-width queries that undo complexity โ†’ harder to maintain. Write mobile-first with min-width instead.