0% completed
Creating and Opening Your First HTML File
On This Page
Choosing a Text Editor
Making a Folder for the Site
Saving index.html
The Hidden .txt Trap on Windows
Opening the File in a Browser
Edit, Save, Reload
Looking at the Source
Checking the Markup
Key Takeaways
Two programs are enough to build a web page: a text editor, where you write the HTML, and a web browser, where you see the result. You already have a browser, and every computer comes with a simple text editor. Nothing has to be bought or installed, and you do not need an internet connection to see your page. The bakery page from the previous lesson will be your first file, and by the end of the lesson it will open in your browser.
Choosing a Text Editor
An HTML file must contain only the characters you type, with nothing hidden between them. A program that saves exactly that is a plain-text editor. Notepad on Windows is one, and so is TextEdit on a Mac after you choose Format, then Make Plain Text. TextEdit also shows a saved HTML file as a formatted page when you open it again, unless you turn on "Display HTML files as HTML code instead of formatted text" under Open and Save in its settings.
A word processor such as Microsoft Word or Google Docs does not work. It saves fonts, margins and other formatting information along with your text, so the file is no longer plain text. It also changes straight quotes into curly ones as you type. HTML accepts only straight quotes around a value, so if the quotes in <html lang="en"> turn curly, the browser treats them as part of the value, and the language of the page is no longer en.
Most people who write HTML use a code editor: a plain-text editor with extra help for programmers, such as colors that separate tags from text. Visual Studio Code, from Microsoft, is one free example that runs on Windows, macOS and Linux. Any plain-text editor works for this course, however, so use whichever one you have.
Making a Folder for the Site
A website is a set of files: pages, images and, later, other files that the pages use. Keep them together in one folder from the start, because the links and images in later chapters find each other by their places in that folder.
Create a folder named bakery-site, for example inside your Documents folder. Write the names of folders and files in lowercase letters, with hyphens instead of spaces. Many web servers treat Index.html and index.html as two different files, and a space in a file name becomes %20 in a web address, so this habit prevents problems when the site goes online.
Saving index.html
Open your editor, create a new file, and type the bakery page:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Maya's Bakery</title> </head> <body> <h1>Maya's Bakery</h1> <p>Fresh bread every morning, from 7 a.m.</p> </body> </html>
Most of these lines are new. The next lesson explains each of them, so for now copy them exactly as they are.
Choose Save, open the bakery-site folder, and name the file index.html. If TextEdit asks which extension to use, choose "Use .html". The part after the last dot, .html, is the file extension. It tells the computer that the file holds HTML, so the computer opens it in a browser when you double-click it. The name index has a meaning too: when an address names only a folder, most web servers send the file called index.html from that folder. For this reason the home page of a site is usually index.html.
Save the file as UTF-8, a way of storing text that can hold the letters of every language. Visual Studio Code and current versions of Notepad use UTF-8 by default. If your editor offers an Encoding choice in the Save dialog, choose UTF-8.
The Hidden .txt Trap on Windows
Windows hides the extension of many file types by default. So a file that is really called index.html.txt appears in File Explorer as index.html, and you cannot tell the difference. This happens when you create the file with New, then Text Document, and rename it, or when the editor's "Save as type" box is set to text documents and the editor adds .txt itself.
The sign of the trap is that a double-click opens the file in Notepad instead of a browser. To see the real name, open File Explorer and choose View, then Show, then File name extensions. If the name ends in .txt, rename the file to index.html, and in future choose "All files" as the type when you save.
Opening the File in a Browser
Double-click index.html in the folder. Your default browser opens it and shows the page. You can also drag the file from the folder onto an open browser window. Either way, the browser shows this:
Browser tab: Maya's Bakery Maya's Bakery (large, bold heading) Fresh bread every morning, from 7 a.m.
Look at the address bar. Instead of an address that starts with https://, it shows one that starts with file:///, followed by the place of the file on your computer. On Windows, for a user named student, the address is file:///C:/Users/student/Documents/bakery-site/index.html. On a Mac it is file:///Users/student/Documents/bakery-site/index.html. Some browsers hide the file:/// part until you click the address bar.
Compare the path of the file on the left of the picture with the address on the right. The browser builds the address from the same folders, in the same order, with forward slashes. The page has not been sent over the internet, so only you can see it. Putting a site online, so that anyone can open it with an https:// address, means copying these files to a web server, which is outside the scope of this course.
Edit, Save, Reload
The browser shows the file as it was when the browser loaded it. It does not notice later changes by itself. So every change follows the same three steps.
First, edit the file in the editor. Add a second paragraph below the first one:
<p>Fresh bread every morning, from 7 a.m.</p> <p>Order birthday cakes two days ahead.</p>
Second, save the file with Ctrl+S on Windows or Command+S on a Mac. Third, reload the page in the browser with F5 or Ctrl+R on Windows, or Command+R on a Mac. The browser reads the file again and shows the new paragraph:
Maya's Bakery Fresh bread every morning, from 7 a.m. Order birthday cakes two days ahead.
If the page does not change, the usual cause is a file that was not saved. Visual Studio Code marks an unsaved file with a dot on its tab.
Looking at the Source
The browser can also show the text of the file it loaded. Right-click an empty part of the page and choose "View page source", or press Ctrl+U in Chrome, Edge and Firefox on Windows. A new tab opens with your markup, exactly as the browser received it, with line numbers. In Safari, this command appears only after you turn on the features for web developers in Safari's settings.
View Source works on any page on the web, not only your own. Reading the source of pages you visit is a good way to see how other people write HTML.
Checking the Markup
A browser almost never reports a mistake in HTML. It repairs the markup as well as it can and draws the page, so an error can stay hidden. Suppose you type the wrong end tag in the heading:
<h1>Maya's Bakery</h2>
Reload the page, and it looks exactly the same as before. The browser closes the heading at the wrong end tag, so in this case its repair matches what you meant. The markup is still wrong, however, and in a longer page a repair can produce a different result from the one you expected.
A program that checks markup against the rules of HTML is called a validator. To find such mistakes, use the Nu HTML Checker, the W3C's HTML validator, at validator.w3.org/nu. Choose "file upload" and select index.html, or choose "text input" and paste the markup. The "address" choice does not work for this file, because the checker runs on a web server and cannot reach a file:/// address on your computer. For the wrong end tag, the checker reports an error on line 9:
Error: End tag "h2" seen, but there were open elements.
After you correct the end tag to </h1> and check again, it reports "Document checking completed. No errors or warnings to show." Checking a page after every few changes is a good habit while you learn.
Key Takeaways
- Write HTML in a plain-text editor, never in a word processor.
- Keep each site in its own folder, with lowercase file names and hyphens instead of spaces.
- Name the home page
index.html, save it as UTF-8, and on Windows turn on File name extensions so that you can see a.txtadded to the name. - A file opened from your computer has an address that starts with
file:///. - After every change, save the file and reload the page.
- The browser repairs mistakes without a message, so check your markup with the Nu HTML Checker.
Reading Progress
0%
On This Page
Choosing a Text Editor
Making a Folder for the Site
Saving index.html
The Hidden .txt Trap on Windows
Opening the File in a Browser
Edit, Save, Reload
Looking at the Source
Checking the Markup
Key Takeaways