In HTML, the id attribute is used to specify a unique id for an HTML element (the value must be unique in the HTML file). The id value can be used in CSS or JavaScript to perform a certain task for a single element with that id value.

In CSS, to select an element with an id, use the character character (#) before the element id .

Example using CSS to style the element with id 'title'


This is the title

Note:

  1. The id attribute can be used with any HTML element.
  2. The id value is case sensitive.
  3. The id value must contain at least 1 character and no spaces.

Distinguish class and id in HTML

The HTML element can have only one id belonging to that element, while the class can be used for multiple elements.




My city




Hanoi


Hanoi is the capital of Vietnam.



Paris


Paris is the capital of France.



Tokyo


Tokyo is the capital of Japan.

The id attribute in HTML Picture 1
Compare the difference between class and id attributes in HTML

Use id attribute in JavaScript

JavaScript can access elements with a certain class name using getElementById()

 

Bookmark the page with ID and Link

Bookmarking in HTML allows readers to go to different parts of the page faster, very useful if the page is too long. To create a bookmark, first create a bookmark, then add the path to it. When clicking on the link, the page will scroll to the highlighted location.

First create a bookmark with the id attribute

Chapter 4

Then add the path to this bookmark ('Go to Chapter 4), from the same page.

 Đi tới Chương 4 

Or add a link to this bookmark from another page.

 Đi tới Chương 4 

Previous article: Class properties in HTML

The following article: Iframe in HTML