A starter page to copy
An HTML file is a plain text file whose name ends in .html. You don’t need special software to make one.
Any editor that saves plain text will do, and every computer comes with one. Here’s a small, complete page to start
from. It’s live: change the words in the code and the preview updates.
-
<!DOCTYPE html>tells the browser this is a modern HTML page. Leave it as the first line. -
The
<head>holds things you don’t see on the page: the title in the browser tab,charset="utf-8"so letters like é and emoji show up right, and the viewport line so phones don’t shrink the page. - Everything inside
<body>is what shows up on the page.
If the tags themselves are new to you, What is HTML? explains them from the start.
Create an HTML file on Windows (Notepad)
Notepad is already on every Windows PC, and it saves plain text, which is exactly what you want. Word doesn’t: it wraps your code in its own formatting.
- Open the Start menu, type Notepad and open it.
- Paste the starter page into it (Ctrl+V).
- Choose File > Save as (Ctrl+Shift+S). Pick a folder you’ll find again. A new folder called
my-siteon the Desktop works well. - Set Save as type to All files (*.*). This is the step people skip.
- Type the file name
index.html. - Check that Encoding says UTF-8. Recent versions of Notepad pick it already. If it says ANSI, change it.
- Click Save.
The index.html.txt trap. If Save as type was left on “Text documents”, Notepad may add
.txt and you end up with index.html.txt. Windows hides the last extension by default, so it
still looks like “index.html” in File Explorer, but it opens in Notepad instead of your browser. Typing the name in
quotes, "index.html", also stops Notepad from adding anything.
To see the real file names, turn on file extensions in File Explorer. On Windows 11, click View > Show
> File name extensions. On Windows 10, open the View tab and tick
File name extensions. If you spot an index.html.txt, rename it to
index.html and say yes when Windows warns you about changing the extension.
Create an HTML file on a Mac (TextEdit)
TextEdit comes with every Mac, but it starts in rich text mode, like a small word processor. Saved that way, your browser shows your tags as text instead of a page. Switch it to plain text first:
- Open TextEdit (press Cmd+Space, type TextEdit) and click New Document.
- Choose Format > Make Plain Text (Shift+Cmd+T). The font and formatting buttons disappear from the top of the window. That’s how you know it worked.
- Paste the starter page (Cmd+V).
- Choose File > Save (Cmd+S), type
index.htmlas the name and pick a folder. - Leave Plain Text Encoding on Unicode (UTF-8) and click Save.
- TextEdit asks if you’d rather have the standard
.txtextension. Click Use .html.
Change one more setting now. By default, TextEdit opens an HTML file as a formatted page, not as code, and if you save it like that your code is gone. Open TextEdit > Settings (called Preferences on older macOS). Under Open and Save, tick Display HTML files as HTML code instead of formatted text. Then, on the New Document tab, set Format to Plain text so you don’t have to switch modes every time.
Use a code editor instead
Notepad and TextEdit are fine for a first page. Once you write more than a few lines, a code editor is much nicer. It colors the tags so mistakes stand out, closes tags for you and shows all the files of your site side by side. VS Code is free and runs on Windows, Mac and Linux. Notepad++ is a free option on Windows.
In VS Code, open your folder with File > Open Folder, create a new file and save it as
index.html. Then type ! and press Tab or Enter: it writes a starter page like the one
above for you. (It only works once the file has a .html name, because that’s how the editor knows it’s
HTML.) Many people also add an extension such as Live Server, which opens the page in your browser and reloads it
every time you save.
Open an HTML file in Chrome or another browser
The simplest way is to double-click the file. It opens in your default browser, which on a new Windows PC is usually Edge and on a Mac is Safari. To pick a different one, or if double-clicking opens an editor, use one of these:
| Way | Windows | Mac |
|---|---|---|
| Right-click the file | Open with, then Google Chrome (or Edge, Firefox) | Open With, then Google Chrome (or Safari, Firefox) |
| Drag and drop | Drag the file onto an open browser window or tab | Same, or drop it on the browser’s icon in the Dock |
| From inside the browser | Press Ctrl+O in Chrome, Edge or Firefox and pick the file | Press Cmd+O in Chrome, Safari or Firefox and pick the file |
| Type the address | file:///C:/Users/you/Desktop/my-site/index.html | file:///Users/you/Desktop/my-site/index.html |
Put your own user name in place of you. On many Windows 11 PCs the Desktop lives inside OneDrive, so
the real path has OneDrive in it too. Easier: drag the file into the browser and copy the address it
shows.
Look at the address bar once the page is open. It starts with file:///, which means the browser is
reading a file from your own disk. It isn’t on the internet, and nobody else can open that address. To show the
page to a friend, it has to be put online with a hosting service, or you can paste the code into our
HTML Viewer and use Share to get a link.
You can also look at the code behind the page you just opened: press Ctrl+U on Windows, or Cmd+Option+U in Chrome on a Mac. The view HTML source tutorial covers that and the developer tools.
Edit the file and see your changes
Editing an HTML file means opening it in your text editor, not in the browser. Right-click the file and choose Open with (Windows) or Open With (Mac), then your editor. Keep the editor and the browser open next to each other and go round this loop:
- Change something in the editor.
- Save: Ctrl+S on Windows, Cmd+S on a Mac.
- Switch to the browser and refresh: F5 or Ctrl+R on Windows, Cmd+R on a Mac.
The browser never notices changes on its own, so if the page looks the same, you probably forgot to save or to
refresh. This example skips both steps, so you can play first. Try a color like tomato or
#2563eb for the heading:
In a real file, the <style> block goes inside <head> and the rest goes inside
<body>. When your CSS grows, move it into its own file: the
link CSS to HTML tutorial shows how.
Open an HTML file on iPhone or Android
Phones don’t treat HTML files the way computers do, and neither comes with a plain text editor for writing them.
iPhone and iPad
An HTML file you get from an email, AirDrop or a download ends up in the Files app. Tap it and you usually get a quick preview of the page. It’s a preview, not a full browser: links to other pages, images stored next to the file and scripts often don’t work, and you can’t edit anything there. To see the whole page, open our HTML Viewer in Safari, tap Open a file and pick the file from Files. Or copy the code and paste it in. To write HTML on an iPhone, you’ll need a code editor app from the App Store, or you can type straight into the HTML Editor.
Android
It depends on your phone and its files app. Tapping an .html file usually asks which app to use. Chrome
is often on the list, while some phones open the file as text and show the code. If it won’t show as a page, the
same trick works: open the HTML Viewer in Chrome, tap Open a file and choose the file.
How to run HTML code
There’s nothing to install, compile or press play on. To run HTML, you open it in a browser. The browser reads the tags from top to bottom and draws the page, together with any CSS and JavaScript the page includes. That’s why a double-click is enough.
Opening a file straight from your disk is fine for HTML, CSS, images and most JavaScript. A few things only work
when the page comes from a web server, even one running on your own computer: JavaScript that loads another file
with fetch(), JavaScript modules, and some newer browser features. This line, for example, works on
a website but not from a file:/// address:
<script type="module" src="app.js"></script>
The browser refuses to load app.js, and Chrome shows a CORS error in the Console. When you hit that,
start a small local server. The Live Server extension in VS Code does it in one click. If you have Python, open a
terminal in your site’s folder and run this (type python3 instead of python on a Mac):
python -m http.server
Then go to http://localhost:8000 in your browser. Your page is served from your own computer, and
the module loads. Press Ctrl+C in the terminal to stop the server.
Try HTML without making a file
If you only want to test a snippet, or you’re on a computer where you can’t save files, skip the file altogether.
Paste your code into the HTML Viewer and the page shows up as you type. You can also drop an
.html file on it. The HTML Editor has separate HTML, CSS and JavaScript tabs
and a Run button (Ctrl+Enter), and it remembers your code in this browser for next time. When you’re happy, copy the
code into a file as above.
For a fuller starting point than the one on this page, the HTML Starter Template builds the whole top of a page for you, with the title, description, social preview tags and links to your CSS and JavaScript files.
When it doesn’t work
- The browser shows your code instead of the page. The file isn’t really HTML. On Windows it was
probably saved as
index.html.txt(turn on file extensions to check). On a Mac, TextEdit was in rich text mode, so it saved your tags as text. Make it plain text and save again. - Strange characters like é or � where é should be. The file wasn’t saved as UTF-8, or the
<meta charset="utf-8">line is missing. Check both. - An image doesn’t show. Nearly always the path in
src. It’s worked out from the folder the HTML file is in, soimages/cat.jpgmeans “the images folder next to this file”. Use forward slashes, and match the name exactly.Cat.JPGandcat.jpgmay both work on your laptop, but most web servers treat them as different files. The images tutorial has a full checklist. - Your changes don’t appear. Save, then refresh. If that doesn’t help, check that the file you’re
editing is the same one the browser has open. Two copies of
index.htmlin different folders catch everyone at least once. Ctrl+Shift+R on Windows, Cmd+Shift+R in Chrome or Firefox on a Mac, or Option+Cmd+R in Safari, reloads without the cache. - Something is off but you can’t see what. Paste the code into the
HTML Validator. It points at the line with a missing
</div>or an unclosed quote, the kind of slip you can read past ten times.
Naming your files and folders
Call your main page index.html. When someone visits a folder on a website, like
example.com/, the server looks for a file with that name and shows it. Give other pages short names
that say what they are, such as about.html or contact.html. Use lowercase letters, and
hyphens instead of spaces (opening-hours.html, not Opening Hours.html), because spaces turn
into %20 in web addresses. .htm works too, but .html is what almost everyone
uses now.
Keep everything for one site in one folder, with images in a folder of their own:
my-site/
├── index.html
├── about.html
├── styles.css
└── images/
└── cat.jpg Then, inside index.html, every path starts from that folder:
<link rel="stylesheet" href="styles.css">
<a href="about.html">About me</a>
<img src="images/cat.jpg" alt="My cat asleep on a pile of laundry" width="400" height="300">
Avoid full paths like C:\Users\you\Desktop\my-site\images\cat.jpg. They work on your own computer and
break everywhere else, including as soon as you move the folder.
Questions people ask
How do I create an HTML file?
Open a plain text editor, such as Notepad on Windows or TextEdit in plain text mode on a Mac. Paste or type your HTML, then save the file with a name that ends in .html, for example index.html. That file is a web page: double-click it and it opens in your browser.
How do I save a file as HTML in Notepad?
Choose File > Save as, set Save as type to All files (*.*), type a name like index.html, leave Encoding on UTF-8 and click Save. If Save as type stays on Text documents, Notepad may add .txt, and you end up with index.html.txt. Turn on file name extensions in File Explorer to check.
How do I open an HTML file in Chrome?
Right-click the file and choose Open with, then Google Chrome. You can also drag the file onto a Chrome window, or press Ctrl+O (Cmd+O on a Mac) inside Chrome and pick the file. Double-clicking opens it in your default browser, which may be Edge or Safari.
Why does my HTML file show the code instead of the page?
The file was saved as text, not as HTML. On Windows the name is probably index.html.txt, with the .txt hidden by File Explorer. On a Mac, TextEdit was in rich text mode. Save it again as plain text with a name that really ends in .html.
How do I edit an HTML file?
Open it in a text editor, not the browser: right-click the file, choose Open with (Open With on a Mac) and pick Notepad, TextEdit or a code editor like VS Code. Make your change, save it, then refresh the browser tab with F5, Ctrl+R or Cmd+R to see it.
How do I open an HTML file on an iPhone?
Tap the file in the Files app and you usually get a quick preview, but links, images next to the file and scripts often don’t work there. For the full page, open the HTML Viewer on htmlviewer.tech in Safari, tap Open a file and pick it, or paste the code in.
How do I run HTML code?
Open it in a web browser. HTML doesn’t need to be compiled or installed: the browser reads the tags and draws the page. Save the code as an .html file and double-click it, or paste it into an online HTML viewer or editor to see it without making a file.
Do I need a web server to view an HTML file?
No. A browser opens .html files straight from your disk, and HTML, CSS, images and most JavaScript work that way. A few things need a server, even a local one: fetch() calls to other files and JavaScript modules. For those, use a local server such as the Live Server extension in VS Code, or python -m http.server in the folder.
Should I use .html or .htm?
Either works. Browsers and servers treat them the same. .htm dates from old systems that only allowed three-letter extensions, and .html is what almost everyone uses now. Just pick one and stick with it, so your links match.
Can I write an HTML file in Word or Google Docs?
Not for writing code by hand. Word and Docs save their own formatting, so your tags end up as text on the page. Use a plain text editor instead. If you already have a document and want it as a web page, a Word to HTML converter turns its formatting into clean tags.
Keep going
Tools that help
- HTML Viewer Paste code or open an .html file and see the page right away.
- HTML Editor HTML, CSS and JavaScript tabs, with a Run button.
- HTML Starter Template Build the top of a new page: title, meta tags, CSS and JS links.
- HTML Validator Finds missing closing tags and other small mistakes.
More tutorials
- How to Center Text, a Div or an Image in HTML & CSS
- How to Add a Line Break in HTML
- How to Add Spaces in HTML
- How to Change Text Color in HTML
- How to Change Background Color in HTML
- How to Add an Image in HTML
- What Is a URL? Meaning, Parts and Examples
- How to Redirect a Page in HTML
- How to Get an Image URL
- Canonical URL
- What Is HTML? What It Stands For and How It Works
- How to Link CSS to HTML
- How to See the HTML Code of a Website
- How to Link JavaScript to HTML with the script Tag
- HTML hr Tag
- HTML Comments
- How to Make Text Bold in HTML
- HTML Font and Font Size
- All tutorials