The id attribute in HTML

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

4 ★ | 1 Vote

May be interested

  • Attribute in C #Attribute in C #
    the attribute in c #, is a declaration tag, used to transmit information to the runtime about the behavior of various elements such as classes, methods, structures, enum, assemblies, etc. in the program. yours. you can add declaration information to the program using attribute.
  • eQuiz - Multiple choice test on HTML knowledgeeQuiz - Multiple choice test on HTML knowledge
    in this test, we will proceed with the small html test, there will be 30 questions in total with no response time, you must choose the answer in each question. can continue to the next sentence.
  • Paths in HTMLPaths in HTML
    you can find links (links) throughout the web. link allows clicking to switch to another page.
  • Why write neat and organized HTML?Why write neat and organized HTML?
    you will get many benefits from writing clean and precise html code. here's why it's a good idea to write optimal html code.
  • HTML formHTML form
    the form in html contains form elements - the element types that take input data such as filling in text fields, checkboxes, buttons, submit buttons ...
  • HTML ISO Country Code ReferenceHTML ISO Country Code Reference
    in html, the country code can be used as a complement to the language code in the lang attribute.
  • Text in HTMLText in HTML
    the element in html is used to define a paragraph of text.
  • How to Use Text Color Tags in HTMLHow to Use Text Color Tags in HTML
    this article explains how to change the font color in html documents. although the html style tag is obsolete in html5, you can use css to color content on an html page. if you use an older version of html, you can use the html text color tag as needed.
  • Format text in HTMLFormat text in HTML
    format elements in html help determine the style and importance of text.
  • Elements in HTMLElements in HTML
    the html element usually goes in pairs, including the opening tag, closing tag, and the content inside the two tags.