HTML & CSS Basics
HTML gives structure to web documents while CSS controls presentation. Learn the DOM tree, semantic elements, the cascade and the box model — the foundation every frontend framework builds on.
What is this?
HTML is the markup language that structures a page into a document tree (the DOM); CSS is a style language that binds presentation rules to nodes in that tree. Browsers parse HTML into the DOM, match CSS selectors against it, resolve conflicting declarations with the cascade, and paint the result on screen. Together they define content and presentation, and both are required for every web page.
Why do you need it?
- HTML is the only universally guaranteed contract: every browser, screen reader and search crawler interprets the same DOM.
- Separating content (HTML) from presentation (CSS) lets one stylesheet restyle an entire site and keeps markup readable.
- Semantic elements such as main, nav and article give accessibility and SEO a head start with zero extra code.
- Every modern framework — React, Vue, Svelte — ultimately compiles down to HTML and CSS in the browser.
Mental model
A skeleton and skin
HTML and CSS
The skeleton defines the shape and where every part attaches; the skin determines color, texture and presentation. You can swap the skin without breaking the skeleton — exactly like replacing a stylesheet for a whole new design.
How it works
- The browser fetches HTML and parses it into a tree of nodes called the DOM, preserving nesting.
- CSS rules are loaded and matched against the DOM with selectors; the cascade decides which declaration wins when rules conflict.
- The box model computes the rendered size of every element from content, padding, border and margin.
- Layout engines (Blink, Gecko, WebKit) place boxes on the page, then painting and compositing produce the pixels you see.
- Scripts can read and mutate the DOM, which triggers style and layout recalculations.
From a document to a rendered page.
Key commands
mkdir my-site && cd my-siteCreate a folder for the project.
npx serve .Serve the current folder over HTTP so the browser loads it as a real page.
Serving! Local: http://localhost:3000
curl http://localhost:3000Fetch the served HTML to confirm it is reachable.
<!DOCTYPE html><html lang=en>...
npx html-validate index.htmlValidate the document against the HTML spec and flag common issues.
npx stylelint '**/*.css'Lint the CSS for invalid properties, ordering and browser pitfalls.
npx prettier --write '**/*.{html,css}'Format all HTML and CSS consistently.
Examples
A semantic card with a stylesheet
<!-- index.html --><!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Article Card</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <main> <article class="card"> <h2>A semantic article</h2> <p>Structure comes from HTML, styling from CSS.</p> <a href="/read-more">Read more</a> </article> </main> </body></html> /* styles.css */body { font-family: system-ui, sans-serif; margin: 0; }main { padding: 2rem; }.card { max-width: 320px; padding: 1.5rem; border: 1px solid #ddd; border-radius: 12px;}.card h2 { margin-top: 0; }.card a { color: #0a66c2; font-weight: 600; }The HTML defines semantic landmarks (main, article) and links a separate stylesheet. The CSS targets .card and its descendants; max-width, padding, border and border-radius are all box-model properties applied to the article element.
In production
- Static marketing sites, documentation and email templates still ship raw HTML and CSS because a framework would add cost without benefit.
- Design systems define CSS custom properties (variables) once and let themes, dark mode and white-labeling reuse the same tokens.
- Accessibility and SEO audits — Lighthouse, axe, Core Web Vitals — are largely scored on how well your HTML and CSS are structured.
- CSS is the delivery layer for every framework: even heavy React apps render a DOM and apply a cascade.
Best practices
- Use semantic landmarks (header, nav, main, footer) instead of generic divs so the document has a meaningful outline.
- Declare a UTF-8 charset and a viewport meta tag in every page head.
- Provide alt text for informative images and an empty alt attribute for decorative ones.
- Prefer rem-based font sizes so text scales with the user's browser setting.
- Set an explicit lang attribute on the html element so screen readers and translation tools pick the right language.
Common mistakes
- Reaching for divs and spans for everything, which erases the document outline and hurts accessibility.
- Piling everything into inline style attributes, making styles unreusable and hard to override.
- Forgetting the viewport meta tag, so mobile browsers render the page at desktop width.
- Using tables or float hacks for layout instead of Flexbox and Grid.
Common issues
My styles are not applying at allSelector specificity, an earlier rule, or a cached stylesheet.
Open DevTools, select the element and read the Computed panel to find the winning rule; hard-reload to bypass the cache.
The page looks different on mobileMissing viewport meta tag or fixed pixel widths.
Add the viewport meta tag and use relative units and responsive layouts.
Text is tiny on iOS SafariiOS auto-inflates text when a page is treated as desktop width.
Ensure the viewport meta tag is present and the page is genuinely responsive.
Debugging tips
- Inspect any element with DevTools and read the Computed panel to see exactly which CSS rule won.
- Temporarily set outline: 1px solid red on an element to visualize its box boundaries.
- Validate HTML at validator.w3.org to catch unclosed tags and invalid nesting.
- Hard-reload (Ctrl+Shift+R) to rule out a stale cached stylesheet when changes do not appear.
Interview checkpoint
Hands-on lab
- Create a valid HTML5 document with the correct head metadata
- Structure a page with semantic landmarks
- Style the page with an external stylesheet
- Serve and validate the result
Build a Semantic Landing Page
25 min · beginner
Quick quiz
Which element is the correct semantic landmark for the primary content of a page?
The viewport meta tag is required for pages to scale correctly on mobile devices.
Which property changes the text color of an element?
Cheat sheet
<!DOCTYPE html>Declares an HTML5 document<html lang='en'>Root element with language hint<head>Metadata and linked assets<meta charset='utf-8'>Character encoding<meta name='viewport' ...>Responsive viewport controlelementMatches by tag name.classMatches by class#idMatches a unique ida:hoverPseudo-class for a state