The id attribute in HTML, the ID tag


The previous article you read with Taimienphi.vn learn about class properties in HTML. The next article below Taimienphi.vn will introduce you more about the id attribute in HTML.


The id attribute specifies a unique id for the HTML element (this value must be unique in the HTML document). Refer to the following article of Taimienphi.vn to find out the id attribute in HTML.

The id attribute in HTML

Table of Contents:
1. The id attribute in HTML
2. What is the difference between class and id attribute in HTML?
3. Create bookmarks in HTML
4. Use the id attribute in JavaScript

first. The id attribute in HTML

CSS and JavaScript can use id values ​​to perform specific tasks for an element with the specified id value.

In CSS, to select an element with a specific id, we use the character (#), followed by the id of the element.

For example: In the example below we use CSS to find the element with id “Taimienphi.vn”:

id idioms in html

The result will look like this:

id idioms in html 2

Note:

– The id attribute can be used on any HTML element.

– Id values ​​must be capitalized.

– The id value must contain at least one character and cannot contain spaces (spaces, tabs, …).

2. What is the difference between class and id attribute in HTML?

Each element in HTML has a unique id, whereas a class name can be used by many other elements.

For example: Refer to the example below to find out the difference between class attribute and id in HTML:

id idioms in html 3

The result will look like this:

id idioms in html 4

3. Create bookmarks in HTML

Bookmarks in HTML are used to allow readers to navigate to specific sections of a web page.

Bookmarking is especially useful, especially on long web pages.

To create bookmarks in HTML, we must first create a bookmark, then add a link for that bookmark.

When the user clicks on the link, the page will scroll to the location where the bookmark is.

For example: In the example below we will create a bookmark in HTML:

First we use the attribute id To create a bookmark:

id idioms in html 5

The next step is to add the link to the bookmark (click to page 4) on the same page:

id idioms in html 6

Or add links to bookmarks (nhay sang chuong 4) from other pages:

id idioms in html 7

id idioms in html 8

The result will look like this:

id idioms in html 9

4. Use the id attribute in JavaScript

JavaScript can access an element with the specified id using the method getElementById ().

For example: In the example below we use the id attribute to manipulate text in JavaScript:

id idioms in html 10

The result will look like this:

id idioms in html 11

So the article above Taimienphi.vn has just introduced you about the id attribute in HTML. In addition, if you have any questions or questions, you can leave your comments in the comment section below the article.

https://thuthuat.taimienphi.vn/thuoc-tinh-id-rong-html-50637n.aspx
In the next article, Taimienphi.vn will introduce you more about properties color in HTML.

.

Add a Comment

Your email address will not be published. Required fields are marked *