W3Schools.com (2024)

The HTML id attribute is used to specify a unique id for an HTML element.

You cannot have more than one element with the same id in an HTML document.

Using The id Attribute

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document.

The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.

The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.

In the following example we have an <h1> element that points to the id name "myHeader". This <h1> element will be styled according to the #myHeader style definition in the head section:

Example

<!DOCTYPE html>
<html>
<head>
<style>
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}
</style>
</head>
<body>

<h1 id="myHeader">My Header</h1>

</body>
</html>

Try it Yourself »

Note: The id name is case sensitive!

Note: The id name must contain at least one character, cannot start with a number, and must not contain whitespaces (spaces, tabs, etc.).

Difference Between Class and ID

A class name can be used by multiple HTML elements, while an id name must only be used by one HTML element within the page:

Example

<style>
/* Style the element with the id "myHeader" */
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}

/* Style all elements with the class name "city" */
.city {
background-color: tomato;
color: white;
padding: 10px;
}
</style>

<!-- An element with a unique id -->
<h1 id="myHeader">My Cities</h1>

<!-- Multiple elements with same class -->
<h2 class="city">London</h2>
<p>London is the capital of England.</p>

<h2 class="city">Paris</h2>
<p>Paris is the capital of France.</p>

<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>

Try it Yourself »

Tip: You can learn much more about CSS in our CSS Tutorial.

HTML Bookmarks with ID and Links

HTML bookmarks are used to allow readers to jump to specific parts of a webpage.

Bookmarks can be useful if your page is very long.

To use a bookmark, you must first create it, and then add a link to it.

Then, when the link is clicked, the page will scroll to the location with the bookmark.

Example

First, create a bookmark with the id attribute:

<h2 id="C4">Chapter 4</h2>

Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same page:

Example

<a href="#C4">Jump to Chapter 4</a>

Try it Yourself »

Or, add a link to the bookmark ("Jump to Chapter 4"), from another page:

<a href="html_demo.html#C4">Jump to Chapter 4</a>

Using The id Attribute in JavaScript

The id attribute can also be used by JavaScript to perform some tasks for that specific element.

JavaScript can access an element with a specific id with the getElementById() method:

Example

Use the id attribute to manipulate text with JavaScript:

<script>
function displayResult() {
document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>

Try it Yourself »

Tip: Study JavaScript in the HTML JavaScript chapter, or in ourJavaScript Tutorial.

Chapter Summary

  • The id attribute is used to specify a unique id for an HTML element
  • The value of the id attribute must be unique within the HTML document
  • The id attribute is used by CSS and JavaScript to style/select a specific element
  • The value of the id attribute is case sensitive
  • The id attribute is also used to create HTML bookmarks
  • JavaScript can access an element with a specific id with the getElementById() method

HTML Exercises

Video: HTML ID

W3Schools.com (1)
W3Schools.com (2)

W3schools Pathfinder

Track your progress - it's free!

W3Schools.com (2024)

FAQs

Is W3Schools completely free? ›

Both My Learning and W3Schools Public Profile are completely free of charge, however there are available upgrade plans that can enhance your learning experience. Where can I find My Learning? You can access your My Learning profile through the upper right corner on any W3Schools page.

Why do people avoid W3Schools? ›

Their examples are outdated, insecure, and don't really explain why things work in a certain way. Their tutorials are optimized for copy & paste, and God knows how many security bugs in the wild are due to somebody copying W3Schools.

Is W3Schools certification legit? ›

W3Schools is Trusted by Top Companies

Our certificates are recognized and valued by companies looking to employ skilled developers.

Is W3Schools a good website? ›

W3schools is a great resource for looking up how to write a specific piece of code, but it doesn't have the flow or continuity that the other courses do. Basically, as others keep mentioning, W3schools is good for syntax, but it doesn't really teach you to think like a developer.

What is W3Schools used for? ›

W3Schools is a freemium educational website for learning coding online. Initially released in 1998, it derives its name from the World Wide Web but is not affiliated with the W3 Consortium. W3Schools offers courses covering many aspects of web development. W3Schools also publishes free HTML templates.

Can I get a job with W3Schools certificate? ›

Becoming certified by W3Schools is great proof to show to employers and makes your resume stand out among other candidates.

Which is better, Codecademy or W3Schools? ›

Of the two, Codecademy has a simpler structure. You can easily find the topic you want and start learning. Its fixed pricing plans are also easier to understand. In addition, the biggest issue with W3Schools is the ads that keep popping up as you learn.

Does W3Schools cover everything? ›

W3Schools is a school for web developers, covering all the aspects of web development: HTML Tutorial. CSS Tutorial. JavaScript Tutorial.

Is W3Schools a good place to learn SQL? ›

"Perfect e-learning course for beginners. The training is very well structured together with quizes after almost every lesson. Great job!

How does w3school make money? ›

Through ads : through ads alone they will earn alot in w3schools. They would earn nearly 1–2lacks per day in ads alone. Through donation:. Many people who completed their courses in w3schools, after few years , they would donate some money for the growth of the site .

How long does W3Schools take? ›

W3Schools is a tech training provider offering a 24-week, part-time, live online, Web Development Bootcamp.

How popular is W3Schools? ›

The Most Popular Browsers

W3Schools has over 60 million monthly visits.

Is W3 free? ›

W3schools is and will always be a completely free developer resource.

Who is the CEO of W3Schools? ›

CEO, W3Schools Network The World's largest web developer site with 75 million monthly visits and over 3.5 billion page views per year.

Is W3Schools copyright free? ›

Fair use defines our exceptions to the rights protected by copyright laws. Fair use of W3Schools includes using copyrighted material: In research. In news reporting.

Is W3 free to use? ›

W3. CSS is free to use. No license is necessary.

Is W3Schools space free? ›

Getting started with Spaces is straightforward: simply sign up for an account to begin with a Free subscription, which requires no payment.

Top Articles
Forex Trading Experts: Strategies, Tips & Insights
Geld anlegen mit einem guten Gewissen: Was sind ethischen Banken und bringen sie was?
Top 11 Best Bloxburg House Ideas in Roblox - NeuralGamer
Design215 Word Pattern Finder
How Many Cc's Is A 96 Cubic Inch Engine
Displays settings on Mac
Big Y Digital Coupon App
Space Engineers Projector Orientation
Shariraye Update
123Moviescloud
Ree Marie Centerfold
Socket Exception Dunkin
Radio Aleluya Dialogo Pastoral
Kaomoji Border
Ts Lillydoll
Craiglist Galveston
065106619
Aberration Surface Entrances
London Ups Store
Second Chance Maryland Lottery
Https Paperlesspay Talx Com Boydgaming
Qhc Learning
Myhr North Memorial
Rochester Ny Missed Connections
Conscious Cloud Dispensary Photos
Sound Of Freedom Showtimes Near Movie Tavern Brookfield Square
Democrat And Chronicle Obituaries For This Week
Best Town Hall 11
Pokémon Unbound Starters
Darknet Opsec Bible 2022
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Purdue Timeforge
Street Fighter 6 Nexus
Gasbuddy Lenoir Nc
Sports Clips Flowood Ms
Ma Scratch Tickets Codes
Laurin Funeral Home | Buried In Work
Whitehall Preparatory And Fitness Academy Calendar
Natashas Bedroom - Slave Commands
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
Craigslist Ludington Michigan
Download Diablo 2 From Blizzard
Panorama Charter Portal
2132815089
Best GoMovies Alternatives
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Craigslist Pet Phoenix
Whitney Wisconsin 2022
City Of Irving Tx Jail In-Custody List
Best brow shaping and sculpting specialists near me in Toronto | Fresha
What Are Routing Numbers And How Do You Find Them? | MoneyTransfers.com
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5948

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.