在CSS(层叠样式表)中,ID选择器用于选择具有特定ID属性的HTML元素。ID在HTML文档中应该是唯一的,这意味着每个ID只能在文档中出现一次。
#id-name {
property: value;
}
ID选择器主要有以下几种类型:
#
后跟ID名称。#
后跟ID名称。getElementById
方法。getElementById
方法。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ID Selector Example</title>
<style>
#header {
background-color: blue;
color: white;
padding: 10px;
}
#main-content {
padding: 20px;
}
</style>
</head>
<body>
<div id="header">This is the header</div>
<div id="main-content">This is the main content</div>
</body>
</html>
通过以上信息,您可以更好地理解CSS中的ID选择器及其应用。
领取专属 10元无门槛券
手把手带您无忧上云