Menu

HTML Title Tag: Page Titles, Length and SEO

The <title> element names the page in the browser tab, in bookmarks and in search results. Learn where it goes, how long it should be, how it differs from <h1> and the title attribute, and how to change it with JavaScript.

This page includes runnable editors - edit, run, and see output instantly.

Where the title tag goes

Every page needs one <title> element inside <head>. Its text is the page name that browsers show in the tab and bookmarks, and that search engines usually show as the result headline:

The title itself never appears inside the page. This preview also runs inside the Coddy docs page, so your browser tab keeps showing the docs title; the small script prints what a tab would show if you opened the example as its own file. Edit the text in <title> and the line updates.

Writing a good title

The title is often the first thing a person reads about your page, in a search result or a row of tabs. A few rules cover most pages:

  • Be specific and unique. Every page on a site should have its own title that describes that page: "Chocolate Chip Cookies Recipe", not "Home" or "Page 1".
  • Put the important words first. Tabs are narrow and search results cut long titles. Keep the key phrase within the first 50 to 60 characters or so.
  • Add the site name at the end, separated by | or :, so the page topic still comes first.
  • Write it for people. A title stuffed with repeated keywords looks like spam, and search engines may rewrite it.

This mock tab clips the title at about the width of a real tab. Try a shorter or longer title:

With the site name last, a clipped tab still shows what the page is about. Swap the order ("Bake Club | How to...") and every tab from your site would start with the same two words.

Title tag vs h1 vs the title attribute

WhereShownJob
<title>In <head>, onceTab, bookmarks, search resultsNames the page
<h1>In <body>On the pageMain visible heading
title="..." attributeOn any elementTooltip on hoverExtra hint about that element

The title and the <h1> usually match in meaning, but the title can be phrased for search and carry the site name, while the heading can be shorter. The title attribute is unrelated, despite the name. Hover the underlined word:

Tooltips from the title attribute do not appear on touch screens and are not reliably read by screen readers, so do not put important information only there.

Only text goes in a title

The browser reads everything between <title> and </title> as plain text. Tags are not parsed, but character references such as &amp; are decoded, and runs of whitespace collapse to one space:

The result is Salt & Pepper <b>Bold?</b>: the entity became &, the three spaces became one, and the <b> tags stayed as literal characters. Emphasis is not possible in a title.

Changing the title with JavaScript

document.title is readable and writable. Web apps use it to show unread counts or the current state in the tab:

On a real page the browser tab changes at the same moment as the mock tab here. Search engines that run JavaScript can see a title set by script, but a title written in the HTML is the reliable choice for anything that should be indexed.

Common mistakes

  • No title at all. The browser falls back to the URL or file name, and the page is invalid HTML. Add one to every page, next to the meta viewport tag in your template.
  • The same title on every page. Tabs, bookmarks and search results become impossible to tell apart.
  • Putting <title> in <body>. Browsers still use it, but it belongs in <head>, and some tools will miss it.
  • Two <title> elements. Only the first one counts. Remove the others.
  • Starting every title with the site name. Lead with the page topic; the site name goes last.

Frequently Asked Questions

What is the title tag in HTML?

<title> sets the name of the page. Browsers show it in the tab and in bookmarks, and search engines usually show it as the clickable headline of the result. It goes inside <head> and contains only text.

How long should a title tag be?

Search engines cut off long titles by display width, so keep the important words in the first 50 to 60 characters or so. There is no hard limit in HTML, but anything past that point may be hidden behind an ellipsis.

What is the difference between the title tag and h1?

<title> is metadata: it names the page in tabs and search results and is not shown in the page itself. <h1> is the visible main heading in the body. They often say similar things, but the title can add the site name and be phrased for search.

Can I put HTML tags inside the title tag?

No. The title element holds text only. Tags inside it are shown literally, so <title>A <b>B</b></title> gives a tab that reads "A <b>B</b>". Character references such as &amp; do work.

How do I change the page title with JavaScript?

Assign to document.title, for example document.title = '(3) Inbox'. The browser updates the tab right away, which is how web apps show unread counts.

Is the title tag the same as the title attribute?

No. The <title> element names the whole page. The title attribute, which works on almost any element, adds a tooltip that appears when you hover that element.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED