HTML Id

An id is a unique name for a single element on the page.

Unlike a class, an id should appear only once per page. That uniqueness makes it perfect for jump links and for scripts that target one exact element.

The id Attribute

A unique id

<!DOCTYPE html>
<html>
<body>

<h2 id="pricing">Pricing</h2>
<p>See our plans below.</p>

<h2 id="contact">Contact</h2>
<p>Reach out any time.</p>

</body>
</html>

Class vs Id

classid
ReusableYes, many elementsNo, one element
Main useStyling groupsAnchors and scripts

Linking to an Id

A jump link

<!DOCTYPE html>
<html>
<body>

<a href="#pricing">Go to pricing</a>

<p>Welcome to our homepage. Scroll down to see the plans.</p>

<h2 id="pricing">Pricing</h2>
<p>Starter plan: $9/month.</p>

</body>
</html>
Note: Clicking a link whose href is #pricing scrolls the page to the element with that id.

Exercise: HTML Id

Which CSS selector syntax targets an HTML id?