Back to Explore
GuideVibeFix editorialUpdated Nov 25, 2026

Build the Page First: HTML Basics in Practice

64 start tags, 5 headings, 4 links, 1 image; 3 books cost 90030 cents, 20 copies; 3 local checks, 200, 200, 404. Python 3.12.3 standard library only, page file 1834 bytes.

Real photo: a person typing at a laptop whose screen shows HTML code, with index.html and about.html tabs visible

Open a web page and you see text, pictures, buttons and tables. The browser sees something else: a set of instructions written in angle brackets, telling it "this part is a heading, that part is a paragraph, this is a picture, that is a table." Those instructions are HTML, short for HyperText Markup Language. This guide is a hands-on HTML introduction: one HTML file builds a reading-list page for a small fictional bookshop called Morning Light Bookshop, and a script using only the Python standard library checks it from top to bottom — counting tags, doing the math in the table, checking heading order and image text, and finally serving it locally and confirming it really opens with real requests. The shop and the numbers are made up to make the ideas clear; every step was really run, with the demo script demo252_g2.py, on Python 3.12.3 locally, zero third-party packages. The Chinese and English runs use the same logic and print identical numbers, and both transcripts are saved in this directory.

Here is the short version first: the page has 64 start tags, including 5 headings (1 h1, 3 h2, 1 h3), 6 paragraphs, 4 links, 1 image and 3 form labels; the 3 books in the table cost 90030 cents in total, which is 900.30 yuan, with 20 copies in stock; the file itself is only 1834 bytes, and its picture bookshelf.jpg is 435903 bytes; served locally and checked 3 times, the home page and the picture both answer 200, and a page that does not exist answers 404. Keep those numbers in mind — everything below is them, unpacked.

Three words to tell apart first: element, tag, attribute

The first thing to get straight in HTML is three words. An element is a chunk of content on the page — a paragraph, a heading, a picture. A tag is the name written in angle brackets that tells the browser "a paragraph starts here" or "a heading ends there". An attribute is extra information stuck onto a tag, like where a link goes, the address of a picture, or how to describe that picture to someone who cannot see it. The official docs put it plainly: HTML is made of a series of elements, which you use to enclose, wrap, or mark up different parts of content to make it appear or act in a certain way; tag names are not case-sensitive, but everyone writes them in lowercase by convention.

A complete element at its simplest looks like this:

<p>My cat is very grumpy</p>

First comes the opening tag <p>, then the content "My cat is very grumpy", then the closing tag </p>, which has an extra slash. Forgetting the closing tag is the most common beginner mistake: the browser guesses where you meant to end, and what it guesses is often not what you meant. Elements can sit inside other elements, which is called nesting:

<p>My cat is <strong>very</strong> grumpy.</p>

Nesting has one iron rule: first opened, last closed. Here <p> opens first and <strong> second, so </strong> must close first and </p> second. Write it the other way around — <p>My cat is <strong>very grumpy.</p></strong> — and the tags cross over, the browser has to guess again, and the page may look fine in one browser and broken in another. My rule of thumb: when you write HTML, do not count on the browser to tidy up after you. Every time you open a tag, keep a mental note, and close it as soon as the content is done.

Not every element has content. Some consist of a single tag, used to insert something into the document; these are called void elements, for example <br> for a line break and <img> for a picture. You will sometimes see a slash at the end, as in <br /> — a different style of markup, not wrong, but the slash is not needed. An attribute looks like this:

<img src="bookshelf.jpg" alt="Wooden bookshop shelves full of books" width="360" height="640">

The attribute name is followed by an equals sign, and the value sits inside quotes. Can you skip the quotes? It works when the value has no spaces, but the moment a space appears everything breaks — the browser treats the word after the space as a new attribute. If the demo page wrote a link title without quotes, the browser would take the first word as the title and the rest as other attributes, and the meaning would be lost. So the rule is simple: always quote attribute values. Single or double quotes both work; just do not mix them.

The skeleton of a document: the invisible parts matter too

A simplest complete web page is only a dozen lines, and the demo page starts exactly like this:

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <title>Morning Light Bookshop · Reading List</title>
  </head>
  <body>
    <p>This is my page</p>
  </body>
</html>

The first line, <!doctype html>, is the document type declaration: "this is a modern HTML document, please render it in standards mode." It is a historical leftover, and today this is the shortest valid form — but without it, the browser may fall back to an old compatibility mode and the layout can go strange. So every document needs it on the first line, and the first thing the demo script checks is that it is there. Next, <html> is the root element, wrapping everything on the page; lang="zh-CN" tells the browser and screen readers that the page is in Chinese, so a screen reader knows to read it with Chinese pronunciation instead of spelling it out letter by letter as if it were English.

The <head> holds information about the page, not the page content: <meta charset="utf-8"> declares the character encoding as UTF-8, without which Chinese can turn into garbled text in some browsers; <title> is the line shown on the browser tab, and also the title shown in search results. The demo page has only these two in its head, but real projects add a page description, stylesheet links and icon links there too — none of them show on the page directly, yet they decide what the page looks like in search results and what picture it gets when shared in another app. My rule of thumb: write the skeleton right first, and never skip any of these four — doctype, lang, charset, title. Together they are fewer than ten lines, and they are what keeps the page free of garbled text, readable by screen readers, and findable by search.

Once the skeleton is set, the <body> holds what users actually see. Nearly all of the demo page's 64 start tags live there: apart from the 1 meta and 1 title in the head, the other 62 are all inside the body. That is how the counting script works: it reads the whole file and counts start tags one by one, finally printing 64. The next sections go through those 64 tags one group at a time.

Semantic tags: write what it is, not what it looks like

HTML has a set of tags whose job is to say clearly what a chunk of content is. They are called semantic tags, and the demo page's skeleton is built from them:

<header>  <h1>Morning Light Bookshop</h1>  <nav>…</nav> </header>
<main>
  <article>…the book list…</article>
  <aside>…visiting info…</aside>
  <section>…the note form…</section>
</main>
<footer>…</footer>

header is the page header, nav is navigation, main is the main content, article is a self-contained piece of content, aside is a sidebar related to the main content but readable on its own, section is a themed group of content, and footer is the page footer. On screen they look exactly like a plain div by default — no difference in styling. The difference is in meaning: a screen reader can recognise nav as navigation and jump past it, a search engine can recognise article as the main content and weigh it more, and another programmer reading your code can tell at a glance which part is which.

The official docs give a very plain example: a button to play a video could be written as <div>Play video</div>. It would show the words "Play video" on screen, and with styling and scripting it could even play the video when clicked. But written as <button>Play video</button>, you get three things for free: the browser's own button look, reaching it with the Tab key, and pressing it with Space or Enter. With a div pretending to be a button, all of that has to be built back by hand, which takes a fair amount of code and is easy to get wrong. The docs put it this way: semantic HTML takes no longer to write than non-semantic markup if you do it consistently from the start — and beyond accessibility it has other benefits: it is easier to develop with, lighter on mobile, and better for search engines.

Headings are part of the semantics too. The demo page has 5 headings: 1 h1, 3 h2, 1 h3, in the order h1 > h2 > h3 > h2 > h2. The h1 is the title of the whole page, and there is only one — "Morning Light Bookshop". The three h2 headings are the titles of the three big blocks: "This week's book list", "Visiting info" and "Leave a note". The h3 is a smaller title inside the book list: "How to pick a book". Heading levels are the page's table of contents: screen readers can list all the headings and let users jump to the section they want, the way you would use a contents page. My rule of thumb: never use a heading as a way to make text bigger — use styling for that. Headings only say "this section is about this, and it sits one level below the last one." The document tree on the left of the diagram shows this structure growing down from html.

Paragraphs and lists: getting the words settled

Most of the words on a web page live in paragraphs, <p>. The demo page has 6 paragraphs: one introducing the book list, one doing the math under the table, one on how to pick a book, one with the visiting info, one under the picture carrying a link, and one in the footer. The browser leaves a gap between paragraphs on its own, so you do not need blank lines to separate them the way you would in plain text — in HTML, starting a new paragraph means starting a new <p>.

There are two kinds of lists: unordered lists <ul> and ordered lists <ol>. The demo page uses 2 <ul> elements and 6 <li> items: the 3 navigation links each take one li, and the 3 books in the list each take one li. A list item li must live inside a ul or an ol, never directly under body; and a ul may only directly contain li elements, not a loose chunk of text. When do you use ordered, and when unordered? Use ol when the order itself means something, like "step 1, step 2, step 3"; use ul for items that are simply side by side, like a book list or three navigation links. The demo book list has no before-and-after, so it uses ul. Another common beginner mistake is "drawing" a list with <br> and numbers, like "1. Three-Body<br>2. To Live". It looks like a list, but a screen reader has no idea it is one, and the user cannot jump to the third item — with a real ul or ol available, do not fake one with line breaks.

Links and images: the web is connected

The "hyper" in hypertext means links. A link is written with the anchor element <a>, and the href attribute holds the destination:

<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML">MDN's HTML introduction</a>

The demo page has 4 links: 3 in the navigation and 1 in the sidebar. The first two in the navigation are links inside the page, href="#books" and href="#visit". Clicking them does not leave the page; it jumps to the headings carrying id="books" and id="visit". That is why headings carry an id: an id is a unique name on the page, and links, form labels and styles can all use it to find the matching element. The demo page has 5 id values in total — books, visit, name, email and book — and the script checked that none of them repeats. The other two links go off-site, to MDN's HTML documentation pages; clicking them leaves the site.

There is one rule for link text: never write "click here" or "more". Screen reader users often listen to all the links on a page first, then decide which one to follow; four links all saying "click here" tell them nothing. The demo page's link texts are "Book list", "Visit", "Learn HTML" and "MDN's HTML introduction" — each one says where it goes. The official accessibility docs say the same: link text should be meaningful, so users do not have to read the surrounding text to know where a link leads.

Pictures use <img>, a void element with no closing tag, and the address goes in src. The demo page has just 1 image:

<img src="bookshelf.jpg" alt="Wooden bookshop shelves full of books" width="360" height="640">

alt is the text version of the picture: when the image fails to load, or the user cannot see it, the browser and screen readers read this sentence instead. The demo picture is a real photo — a row of wooden bookshop shelves full of books, with a small white stool in the middle — a 435903-byte JPEG, 720 pixels wide and 1280 tall, which is why the page says width="360" height="640", showing it at half size in the same proportions. Writing width and height is not only about size: the browser knows in advance how much room the picture will take, so the page does not jump around and push the text aside while the picture is still downloading.

What alt should say depends on what the picture does on the page. This one sits beside the book list to give a feel of "this is a bookshop", so its alt says "Wooden bookshop shelves full of books" — it describes what is in the picture. If a picture is purely decorative and removing it would change nothing, write an empty alt="" and screen readers will skip it. The worst option is leaving alt out altogether — then the screen reader reads the file name, and "bookshelf.jpg" spoken aloud means nothing. My rule of thumb: every meaningful picture gets an alt. Imagine describing the picture to someone over the phone, in one sentence. That is enough. Do not write "image" or "photo", and do not copy the text next to the picture all over again.

Tables: rows are rows, columns are columns — not for layout

Tables are the squarest set of tags in HTML: <table> is the whole table, <tr> is a row, <th> is a header cell, <td> is an ordinary cell. The demo book list table looks like this:

<table>
  <tr><th>Book</th><th>Unit price (cents)</th><th>In stock (copies)</th></tr>
  <tr><td>Three-Body</td><td>5990</td><td>7</td></tr>
  <tr><td>To Live</td><td>2900</td><td>5</td></tr>
  <tr><td>West with the Night</td><td>4200</td><td>8</td></tr>
</table>

The table has 4 rows in total: 1 header row plus 3 data rows; 3 header cells and 9 data cells. The first row uses th instead of td because "Book, Unit price, In stock" are the names of the columns, not data — browsers render th bold and centred by default, and a screen reader reading a cell will also announce which column it belongs to, for example "5990, unit price (cents)". If everything used td, a screen reader user would hear a string of numbers with no column names, and have no idea whether 5990 is a price or a stock count.

Here is the math in the table: Three-Body at 5990 cents times 7 copies is 41930 cents, To Live at 2900 cents times 5 copies is 14500 cents, West with the Night at 4200 cents times 8 copies is 33600 cents; the three lines add up to 90030 cents, which is 900.30 yuan; the stock total is 7 plus 5 plus 8, which is 20 copies. The demo script does not have these numbers hard-coded. It really reads the 6 number cells in the table — 5990, 7, 2900, 5, 4200, 8 — multiplies them line by line and adds them up, and the total it prints matches the numbers in this article exactly. Why cents instead of yuan is something the earlier Node.js guide covered: decimal numbers like 59.9 cannot be stored exactly in binary floating point, so money is kept as whole numbers in the smallest unit and divided by 100 only when shown to people.

One old rule is worth repeating: tables hold data that belongs in rows and columns. They are not for laying out a page. Long ago people used tables to arrange pages — navigation in the left column, content in the right — but that is wrong today: table layouts are hard to change on a phone, screen readers read them as data tables, and changing one thing means touching the whole table. Layout belongs to styling, which is CSS's job; tables only carry data.

Forms: letting readers send something back

Pages are not only for reading; they also have to take things in — notes, sign-ups, purchases all run through forms. At the bottom of the demo page there is a note form:

<form action="/leave-a-note" method="post">
  <label for="name">Your name</label>
  <input id="name" name="name" type="text" required>
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>
  <label for="book">Which book are you looking for</label>
  <select id="book" name="book">
    <option>Three-Body</option>
    <option>To Live</option>
    <option>West with the Night</option>
  </select>
  <button type="submit">Send the note</button>
</form>

The most important pair in a form is a label's for and a control's id: a label with for="name" belongs to the input with id="name". Click the words "Your name" and the cursor jumps into the input; a screen reader reading the input will also read "Your name", so the user knows what the box is for. The demo page has 3 label elements, matched to 3 controls — 2 input elements and 1 select — and the script checked them one by one: every for value matches an id exactly, none missing. Writing bare text next to a box without a label is the most common form mistake: it looks no different, but to a keyboard user or a screen reader user that box has no name at all.

The type of an input is also worth a word. The name field is type="text"; the email field is type="email". Changing that one word makes the browser do two extra things: on a phone, tapping the email box brings up a keyboard with an @ key; and on submit, the browser first checks whether the email contains an @, and blocks the form if it does not — without a single line of script. required is just as plain: this field must be filled in, and an empty form will not submit. The dropdown select holds 3 option elements, exactly the 3 books on the list; the final button type="submit" sends the form to the address in the form tag, action="/leave-a-note", using method="post". Note that none of this needs JavaScript — submitting a form is built into HTML itself. What the server does with the data afterwards is the back end's job.

Accessibility basics: good HTML is good accessibility

Accessibility sounds like a specialist subject, but for HTML most of it is simply "do the things in the sections above properly." The MDN accessibility guide says it up front: semantic HTML is the basis of accessibility, and using the right element for the job is doing accessibility. The demo page's checklist has only 5 items, and each one stands for a real person:

First, do not skip heading levels. The demo page's order is h1 > h2 > h3 > h2 > h2: from h1 to h2, then to h3, never jumping straight from h1 to h3, and never two h1 headings. Screen reader users navigate by headings the way you would use a contents page, and a skipped level is a missing page in that contents list. When writing, ask yourself: if this section sits two levels below the last one, where did the middle level go? Either you forgot a heading, or this section should not sit that low.

Second, every meaningful picture needs an alt. The demo page has just 1 image, and its alt — "Wooden bookshop shelves full of books" — is non-empty; the script checked. Pictures are the easiest thing to forget, because the person writing the page can see them and feels "the picture is right there". But for someone who cannot see it, a picture without alt is a blank space.

Third, every form control needs a label. The demo page's 3 labels are all in place, checked one by one in the section above. One more point: placeholder — the pale grey hint text inside an input — cannot replace a label. It disappears the moment the user starts typing, and many screen readers do not read it at all.

Fourth, the order of the source code should make sense. The demo page goes header, then book list, then visiting info, then the note form, and a screen reader reads it in exactly that order, top to bottom. Styling can move the sidebar to the left or push the form to the top on screen, but the order in the source should always be "what you meet first is read first." The official docs warn: get the order wrong and screen reader users hear a mess.

Fifth, using the right elements gives you keyboard support for free. Links can be reached with Tab and opened with Enter; buttons can be reached with Tab and pressed with Space; dropdowns change options with the up and down arrow keys — all of it comes with the browser. Use a div as a button or a span as a heading instead, and all of that has to be built back, with effort and easy to miss. My rule of thumb: accessibility is not an extra layer added after the page is written. It is HTML itself — get the headings, the labels and the descriptive text right, and most of the accessibility work is already done.

Running it locally: one file can be served too

A finished HTML file opens in the browser with a double-click, but that is the file:// way of opening it: relative paths sometimes behave differently, and you cannot test what happens when the server cannot find a page. The demo script does it differently: it copies index.html and bookshelf.jpg into a temporary folder, then starts a server on the local machine with Python's standard library http.server, listening only on 127.0.0.1 and letting the system pick any free port — so the demo never fails because "the port is taken" on whatever machine it runs on. Using only the standard library has another benefit: anyone reading this guide needs to install nothing, because Python 3.12.3 ships with these modules.

Once the server is up, the script checks 3 requests with the standard library's urllib: GET / returns 200, type text/html, 1834 bytes — exactly matching index.html on disk; GET /bookshelf.jpg returns 200, type image/jpeg, 435903 bytes — exactly matching the picture on disk; GET /missing.html returns 404, type text/html, 335 bytes — the server's own "page not found" message. 200 means "it worked", 404 means "what you asked for does not exist", and those are the only two status codes in this guide. The demo deliberately asks for a page that does not exist, because "does not exist" is also something a server has to be able to say: a service that can only say 200 leaves the caller unable to tell "found it" from "no such page".

If you do not want to write a script, one line from Python does the same thing: go into the folder holding index.html, run python3 -m http.server 8000, then open http://127.0.0.1:8000/ in the browser — the same page appears. If port 8000 is taken, use another one, like 8001. Once the server is running, change a file, refresh the browser, and the change shows at once. That is the shortest feedback loop in writing HTML: change one line, refresh, look.

The 6 mistakes beginners make most

First, forgetting to close a tag. Write <p> without </p> and everything after it gets swallowed into that paragraph. The page may still look fine, but the structure is already broken. The fix: write the closing tag the moment you write the opening one, then fill in the content between them.

Second, crossing nested tags. First opened, last closed; crossed tags make the browser guess, and a wrong guess breaks the page. The fix: write with indentation, so the levels are visible at a glance — a crossed tag shows up in the indentation first.

Third, leaving quotes off attribute values. One space in the value and the browser treats the rest as new attributes. The fix: always quote, and never give the browser a chance to guess.

Fourth, pictures without alt. It looks harmless, but to a screen reader user it is a blank space. The fix: treat alt as a required part of img, and write it the moment you write the picture.

Fifth, skipping heading levels. An h3 straight after an h1, or two h1 headings on one page, breaks the table of contents screen reader users rely on. The fix: headings only express levels — never use one as a way to make text bigger.

Sixth, getting a relative path wrong. The picture says src="bookshelf.jpg", meaning "in the same folder as this HTML file". Move the file, or mistype one letter in the case, and the picture 404s. The demo script checking /bookshelf.jpg and getting 200 is confirming that this path is right. The fix: once the files are placed, do not move them around, and when you change a path, open it through the local server and see it work — do not trust your eyes alone.

What you can do after this guide — and where it stops

After this guide you should be able to do six things: explain what an element, a tag and an attribute each mean; write a document with a correct skeleton, with doctype, lang, charset and title all present; build the meaning of a page with header, nav, main, article, aside, section and footer; write links, pictures, tables and forms, knowing who alt, th and label are for; write headings in h1 > h2 > h3 order without skipping a level; and serve a finished page locally and confirm it really opens, with 200 and 404.

Document tree and the bill for 3 books: 64 start tags, 5 headings; Three-Body 41930 cents, To Live 14500 cents, West with the Night 33600 cents, total 90030 cents, 20 copies; 3 local checks, 200, 200, 404

Where this guide stops also needs saying. The site's DOM querying and traversal guide covers "the tree is already built — how does JavaScript find the nodes on it?" That guide operates on the tree; this one builds the tree, so do not mix the two ways of writing: this guide has no JavaScript at all, and form submission, image text and heading levels are all HTML's own business. The site's Node.js basics guide covers JavaScript leaving the browser to read files and start servers; this guide only borrowed Python's standard library to verify the page, and teaches no back end. The next CSS guide will pick up the same bookshop: the same 64 tags and the same reading-list page, but dressed up — so the headings, the table and the form actually look good on screen.

Three things to try yourself next, none of them needing any package: add a book of your own to the list, so the table gains a row, then run demo252_g2.py and see whether the total follows; delete the picture's alt and run it again, and see which step stops you; change one letter in a label's for and run it again, and see what error it reports. Once you feel "I can write a page clearly without a framework", then look at the tools that generate HTML for you — that is when you will see that what they save you is exactly the tags you just wrote by hand.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles