id属性是用于指定的唯一ID的HTML文件的一个元素。它分配CSS和JavaScript用于执行某些任务的唯一标识符。
<h3 class="h3"">句法
<tag id="value">
示例 1:以下示例描述了如何在 CSS 文档中使用 id 属性:
<!DOCTYPE html>
<html>
<head>
<title>
Example of Id attribute in CSS
</title>
<style>
#Cars {
padding: 40px;
background-color: lightblue;
color: black;
text-align: center;
}
#Bikes
{
padding: 50px;
background-color: lightGreen;
text-align: center;
}
</style>
</head>
<body>
<p> Use CSS to style an element with the id: </p>
<h1 id="Cars"> Cars </h1>
<h1 id="Bikes"> Bikes </h1>
</body>
</html>输出:

示例 2:以下示例描述了如何在 JavaScript 中使用 ID 属性。
<!DOCTYPE html>
<html>
<head>
<title> Date Attribute </title>
<script>
function viewdate() {
var x = document.getElementById("dob").value;
document.getElementById("demo").innerHTML = x;
</script>
</head>
<body>
Employee Name: <input type="text" placeholder="Your Good name"/>
<br>
<br>
Date of Joining:
<input type="date" id="dob">
<br>
<button onclick="viewdate()"> Submit
</button>
<br>
<h2 id="demo"> </h2>
</body>
</html>输出:

| 元素 | Chrome | IE | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| <id> | Yes | Yes | Yes | Yes | Yes |