Writing and Running Your First HTML Page
Set up a folder, save a real .html file, open it in a browser and understand the difference between opening a file and serving it. The workflow every later note assumes.
-
HTML Basics
- What is HTML: The Structure Layer of Every Web Page
- HTML Document Structure: DOCTYPE, html, head and body
- Elements, Tags and Attributes: The Vocabulary of HTML
- HTML Comments: Notes That Ship With Your Code
- Block Level and Inline Elements
- Writing and Running Your First HTML Page
- How a Browser Turns Markup Into a Page
- Text and Formatting
- Links and Navigation
- Images and Media
- Lists
- Tables
-
Forms
- Form Structure: form, action and method
- Input Types: Text, Email, Number, Date and the Rest
- Labels: The Most Important Element in a Form
- Checkboxes, Radio Buttons and Grouping
- select, option, optgroup and datalist
- textarea, File Uploads and Hidden Fields
- Buttons: submit, reset and button
- Built In Form Validation
- GET or POST: What Happens When a Form Is Submitted
- Semantic HTML
- HTML5 Features
- Head and Metadata
- HTML with CSS
- HTML with JavaScript
- Accessibility
-
HTML SEO
- How Google Works: Crawling, Indexing and Ranking
- SEO Friendly HTML Structure
- Titles and Descriptions That Earn Clicks
- Headings and Content Structure for Search
- Internal Linking and Anchor Text
- robots.txt and XML Sitemaps
- Canonical URLs and Duplicate Content
- Structured Data and JSON-LD
- Image SEO
- Core Web Vitals and Mobile Friendliness
- DevTools and Debugging
- Editor Productivity
- HTML Best Practices
- HTML Projects
- Advanced Projects
- Practice and Exams
Concept
HTML needs no compiler, no build tool and no installation. A plain text file with an .html extension and a browser are the whole toolchain. What does need attention is the folder layout and the difference between opening a file from disk and serving it over HTTP, because a few features fail silently in the first case.
Set up the folder
Start with a structure you will not have to rearrange later:
first-site/
index.html
about.html
styles/
site.css
scripts/
site.js
images/
portrait.jpgindex.html is special by convention: web servers serve it automatically when a visitor requests a directory, so example.com/ and example.com/index.html are the same page. Every home page should carry that name.
File naming rules
- Lower case only. Many servers are case sensitive, so
About.htmlandabout.htmlare different files there and identical on your Windows machine. This is a classic first deployment failure. - Hyphens instead of spaces.
contact-us.html, nevercontact us.html, because a space becomes%20in the URL. - No accented characters, and nothing outside letters, digits and hyphens.
- Keep the
.htmlextension. Windows hides known extensions by default, which is how people end up withindex.html.txtand a browser that shows them source code.
Example
Save this as index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first page</title>
</head>
<body>
<h1>Hello from a file on my own machine</h1>
<p>This page took no tools to build. It is text in a file.</p>
<p>Next stop: <a href="about.html">the about page</a>.</p>
</body>
</html>Opening it
Two ways, and they are not equivalent.
Straight from disk
Double click the file, or press Ctrl and O in the browser. The address bar shows a file:// URL. This is enough for markup, CSS, images and links between local pages.
Through a local server
The address bar shows http://localhost and something behaves correctly that did not before. Several things only work over HTTP:
- Root relative paths such as
/styles/site.css. Overfile://the leading slash means the root of your hard disk. - JavaScript modules,
fetchand anything else governed by cross origin rules. - Service workers, the clipboard API, geolocation and any other feature that requires a secure context.
- The directory index, so
/resolves toindex.html.
Any of these will start a server in the current folder:
# Python, already present on most systems
python -m http.server 8000
# Node
npx serve
# PHP
php -S localhost:8000Then open http://localhost:8000. In an editor, a live preview extension does the same thing and reloads on save.
Explanation
The browser reads the file, meets the doctype and switches to standards mode, reads the encoding, builds the DOM from the tags, applies its own default stylesheet, and paints. The title becomes the tab label. The link to about.html is a relative path, so the browser resolves it against the folder the current page came from - which is why the two files must sit side by side.
Important rules
- Save as plain text with UTF-8 encoding. A word processor will insert formatting and produce a file the browser cannot parse.
- Reload after every save. The change is on disk, not in the open tab.
- If a change refuses to appear, force a reload with
CtrlShiftR; the browser is serving a cached copy. - Relative paths are resolved against the current document, so moving a file breaks every link into and out of it.
Common mistakes
- Saving as
.txtand seeing raw markup in the browser instead of a page. - Writing the page in a word processor. Straight quotes become curly quotes, and
<p class="x">stops working. - Using a backslash in a path. URLs use forward slashes on every platform, including Windows.
- Mixing case between the file name and the link. It works locally and fails on the server.
- Testing only from
file://, then finding half the page broken once it is deployed.
Best practices
- Work through a local server from day one. It removes a whole class of problems that only appear at deployment.
- Create the folders before the files. Retrofitting a structure means rewriting every path.
- Put the project under version control immediately, even for a single page.
- Keep a starter file with the head already filled in and copy it for each new page.
- Test in two browsers regularly rather than once at the end.
Practice
- Build the folder above, write
index.htmlandabout.html, and link them in both directions. - Link the stylesheet as
/styles/site.cssand open the page from disk. Does it apply? Now serve it over HTTP and try again. Explain the difference. - Rename
about.htmltoAbout.html, leave the link lower case, and note that it still works locally. Why is that dangerous? - Start a local server, open the folder as
http://localhost:8000/and confirm which file the browser chose without being told.