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:

Menggunakan CSS untuk Style element dengan ID "#headerSaya"

Header Gue

Note: The id name is case sensitive!

Note: The id name harus mengandung setidaknya 1 karakter, tidak dapat dimulai dengan angka, dan tidak mengandung whitepaces (spaces, tabs, etc.).


Perbedaan antara Class dan ID

Class Name dapat digunakan untuk multiple HTML element, sementara ID name hanya bisa digunakan oleh 1 Html di halaman yg sama

My Cities

London

London is the capital of England.

Paris

Paris is the capital of France.

Tokyo

Tokyo is the capital of Japan.

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:

Hello World!