Skip to content

02. Your First HTML File

Creating your first HTML file is a foundational step in web development. This guide walks you through the process of creating a basic HTML document, understanding its structure, and viewing it in a browser. By the end, you’ll have a working HTML file that demonstrates core concepts.

Before creating your first HTML file, ensure you have:

  • A text editor installed (VS Code, Sublime Text, Notepad++, or any plain text editor)
  • A modern web browser (Chrome, Firefox, Safari, or Edge)
  • Basic file system knowledge (creating and saving files)

Launch your preferred text editor. For beginners, VS Code is recommended as it provides syntax highlighting and helpful extensions for web development.

Create a new file in your text editor. You can do this by:

  • Clicking “File” → “New File”
  • Using the keyboard shortcut (Ctrl+N on Windows/Linux, Cmd+N on macOS)

Type the following HTML code into your new file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First HTML Page</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is my first HTML file.</p>
</body>
</html>

Save your file with a .html extension:

  1. Click “File” → “Save As”
  2. Choose a location (create a folder for your HTML projects)
  3. Name the file index.html or my-first-page.html
  4. Make sure the file extension is .html (not .txt)
  5. Click “Save”

Important: The .html extension is crucial. Without it, browsers won’t recognize the file as an HTML document.

Let’s break down what each part of your HTML file does:

<!DOCTYPE html>

This tells the browser you’re using HTML5, the current standard. It must be the first line of your document.

<html lang="en">

The root element that wraps all content. The lang attribute specifies the language (English in this case), which helps screen readers and search engines.

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First HTML Page</title>
</head>

Contains metadata about the document:

  • charset: Specifies character encoding (UTF-8 supports all characters)
  • viewport: Ensures proper mobile rendering
  • title: Sets the browser tab title
<body>
<h1>Hello, World!</h1>
<p>This is my first HTML file.</p>
</body>

Contains the visible content:

  • h1: Main heading (largest heading)
  • p: Paragraph text
  1. Navigate to the folder where you saved your HTML file
  2. Double-click the file
  3. It will open in your default web browser
  1. Open your web browser
  2. Press Ctrl+O (Windows/Linux) or Cmd+O (macOS)
  3. Navigate to your HTML file
  4. Click “Open”
  1. Open your web browser
  2. Drag the HTML file from your file explorer into the browser window

When you open your HTML file, you should see:

  • “Hello, World!” displayed as a large heading
  • “This is my first HTML file.” displayed as paragraph text
  • “My First HTML Page” in the browser tab title

If you see this, congratulations! You’ve successfully created your first HTML file.

One of the great things about HTML is that you can see changes immediately:

  1. Edit your HTML file in the text editor
  2. Save the file (Ctrl+S or Cmd+S)
  3. Refresh the browser (F5 or Ctrl+R / Cmd+R)
  4. Your changes will appear instantly

Try adding more content:

<body>
<h1>Hello, World!</h1>
<p>This is my first HTML file.</p>
<h2>About Me</h2>
<p>I'm learning HTML and web development.</p>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</body>

Problem: The file opens in a text editor instead of a browser.

Solution: Right-click the file → “Open With” → Choose your browser, or change the file association.

Problem: You edited the file but don’t see changes in the browser.

Solution: Make sure you saved the file, then refresh the browser (F5 or Ctrl+R / Cmd+R).

Special Characters Don’t Display Correctly

Section titled “Special Characters Don’t Display Correctly”

Problem: Characters like é, ñ, or emojis appear as boxes or question marks.

Solution: Ensure your file has <meta charset="UTF-8"> in the head section and save the file with UTF-8 encoding.

Now that you’ve created your first HTML file, you can:

  • Learn about HTML elements and tags
  • Add more content and structure
  • Style your page with CSS
  • Add interactivity with JavaScript
  • Explore semantic HTML elements

When creating HTML files:

  • Use meaningful file names: about.html is better than page1.html
  • Organize files: Create folders for different projects
  • Use index.html: This is the default file browsers look for
  • Validate your HTML: Use online validators to check for errors
  • Keep code organized: Use proper indentation and spacing