Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 122 ยท Getting Started

HTML Editors, Emmet and Comments

Pick a code editor for HTML, type faster with Emmet abbreviations, and learn how HTML comments work, what they are good for and what never to put in them.

You can write HTML in any program that saves plain text, even Notepad. But a good code editor makes the work faster and less error-prone: it colors your code, closes tags for you, warns about mistakes and lets you preview the page. This lesson helps you choose one, shows a typing shortcut language called Emmet, and then covers comments, the notes you leave in your HTML for people (and only for people).

The live editors in this tutorial are enough to follow every lesson. But once you build real pages, you will want a proper editor on your own computer.

Plain text, not word processors

HTML files are plain text. A word processor such as Microsoft Word or Pages stores fonts, colors and layout information in a special format, so it cannot produce a clean .html file. Use a text editor or code editor instead, and save your file with the extension .html, as you did in Your First Web Page.

Choosing an editor

Many editors are free and good enough for professional work. The choice is mostly about taste:

Editor Cost Good to know
Visual Studio Code Free The most popular choice. Huge library of extensions, built-in Emmet, runs on Windows, macOS and Linux.
Sublime Text Paid license, free to evaluate Very fast, even on huge files. Emmet comes as a plugin.
WebStorm and other JetBrains IDEs Paid, with free options for some uses A full development environment with deep code understanding. Emmet is built in.
Zed Free A newer, fast editor with collaboration features.
Notepad++ Free Light, Windows only. A big step up from Notepad.
Vim, Neovim, Emacs Free Keyboard-driven editors with a steep learning curve and a loyal following.

If you have no strong preference, start with Visual Studio Code. Whatever you choose, look for these features:

  • Syntax highlighting. Tags, attributes and text get different colors, so a missing quote stands out immediately.
  • Auto-closing and auto-indenting. Type <p> and the editor adds </p> and keeps your indentation tidy.
  • Live preview or a local server. Many editors can reload the browser whenever you save.
  • Search and replace across files. Essential once a site has more than a handful of pages.
  • Extensions. Formatters, validators and spell checkers save time. See Validating Your HTML.

A few habits that help in any editor: save with UTF-8 encoding (explained in Character Encoding), use two spaces for indentation as the style guide recommends, and turn on word wrap if long lines annoy you.

Emmet: type less, build more

Emmet is a built-in shortcut language for writing HTML and CSS. You type a short abbreviation, press Tab (or Enter, depending on the editor), and it expands into full markup. It is included in Visual Studio Code and many other editors, and it works in .html files out of the box.

The simplest abbreviation is a tag name. Typing p and pressing Tab gives <p></p>, with the cursor inside. From there, a small set of symbols covers most needs:

Symbol Meaning Example abbreviation
. class div.card
# id section#intro
> child ul>li
+ sibling h1+p
* repeat li*3
[ ] attributes a[href=/contact]
{ } text content button{Save}
$ counter li.item$*3
^ climb up one level div>p^footer
( ) group (header>nav)+main

Some abbreviations expand into whole documents. In an empty HTML file, typing ! (or html:5) and pressing Tab produces the full skeleton you built in Your First Web Page: doctype, html, head with charset and viewport, and body.

Here are a few abbreviations and what they become. This block is read-only because Emmet lives in your editor, not on this page:

HTML
<!-- Abbreviation: nav>ul>li*3>a -->
<nav>
  <ul>
    <li><a href=""></a></li>
    <li><a href=""></a></li>
    <li><a href=""></a></li>
  </ul>
</nav>

<!-- Abbreviation: ul>li.item$*3{Task $} -->
<ul>
  <li class="item1">Task 1</li>
  <li class="item2">Task 2</li>
  <li class="item3">Task 3</li>
</ul>

<!-- Abbreviation: a[href=/contact target=_blank]{Contact us} -->
<a href="/contact" target="_blank">Contact us</a>

A few more worth remembering: lorem or lorem20 inserts placeholder text (20 words), img expands to <img src="" alt="">, input:email creates an email field, and link:css creates a stylesheet link. Editors also offer snippets, which are similar but defined by the editor or by you.

Emmet's output is ordinary HTML. The page below is exactly what the abbreviation main>h1{Emmet demo}+p.lead{Typed in one line.}+ul>li*3 would give you, after a little text editing. Run it and change the list.

Markup built from an Emmet abbreviation

HTML comments

A comment is text that the browser reads but never shows. It is a note to yourself and to other developers. A comment starts with <!-- and ends with -->:

HTML
<!-- This is a comment. Visitors will not see it on the page. -->
<p>This paragraph is visible.</p>

Comments can span several lines, and they can appear almost anywhere in a document: in the <head>, in the <body>, between elements, even between table rows.

HTML
<!--
  Navigation
  Links are added by the build script.
-->

What comments are good for

  • Explaining why, not what. The code already shows what it does; a comment can explain a surprising choice, such as why an image has an empty alt.
  • Marking sections in long files, for example <!-- Footer --> before the footer, which makes searching easier.
  • Temporarily disabling code while you test. Wrap an element in <!-- ... --> to hide it without deleting it.
  • Leaving reminders such as <!-- TODO: replace with the final logo -->. Search for "TODO" before you publish.

Most editors toggle comments with a shortcut: Ctrl + / (Cmd + / on a Mac) comments or uncomments the selected lines.

Comments are invisible, not private

A comment does not appear on the page, but it is still part of the file the server sends. Anyone can see it with View Source, as you learned in Viewing Source and Using DevTools. So never put these in a comment:

  • passwords, API keys, tokens or private addresses,
  • notes about security weaknesses,
  • unfinished or embarrassing remarks about customers.

Comments also add bytes to every download. A few notes cost nothing, but thousands of lines of old commented-out markup slow a page down slightly. Build tools often strip comments from the production version for this reason.

Rules and pitfalls

  1. Comments do not nest. The first --> ends the comment. So you cannot comment out a block that already contains a comment: the inner --> closes everything early, and the rest of your "commented" code shows up on the page.
  2. Do not start the text with > or ->, and never write <!--, --> or --!> inside. The HTML standard forbids these because parsers treat them inconsistently. A safe habit: write ordinary sentences.
  3. A comment cannot go inside a tag. Writing <p <!-- note --> class="x"> does not work, because a comment is not allowed in the middle of a start tag.
  4. Comments are not conditional logic. Old Internet Explorer versions understood special "conditional comments". They are obsolete and not supported anywhere today.

The next example shows rule 1 in action. The commented block contains a comment of its own, so the outer one ends early and the text "This line leaks out." appears on the page.

Comments do not nest

Notice that the text after the first --> is rendered. If you need to disable a block that contains comments, remove the inner comments first, or use your editor's shortcut, which handles this for you in most languages.

Comments exist in the DOM

Although comments are invisible, the browser keeps them in the DOM as nodes of a special type, so JavaScript can find them. This is rarely needed in everyday work, but it proves that comments are real parts of the document. Run this example, which lists every comment on the page:

Finding comments with JavaScript

Comments are also different from the // and /* */ comments you write inside <script> and <style> elements. Those follow JavaScript and CSS rules; HTML comment syntax only applies to the markup itself. You will meet both in Adding CSS to HTML and HTML and JavaScript.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is the best choice to write an HTML file?

    HTML files are plain text. A word processor stores formatting data in its own file format, which a browser cannot use as HTML.
  2. 2. What does the Emmet abbreviation ul>li*3 produce?

    > means child and *3 repeats the element before it, so you get one ul with three li children.
  3. 3. A visitor opens your page. What can they see of a comment?

    The browser does not render comments, but they are still part of the downloaded file, so anyone can read them with View Source.
  4. 4. Why does the text after an inner --> appear on the page when you wrap a block that already contains a comment?

    HTML comments cannot be nested. The parser ends the comment at the first --> it finds, and everything after it is treated as normal markup.

Summary

  • HTML is plain text: use a code editor (such as Visual Studio Code), not a word processor.
  • Look for syntax highlighting, auto-closing tags, live preview and extensions.
  • Emmet expands short abbreviations such as ul>li*3 or div.card#main into full markup; ! creates a whole page skeleton.
  • A comment is written <!-- ... -->. It is not displayed, but anyone can read it in the page source, so never hide secrets there.
  • Comments cannot be nested and cannot appear inside a tag. Use them to explain why, mark sections and temporarily disable code.

Next lesson: Text Formatting, where you make words bold, emphasized, marked and more, with the right meaning for each.