CSS ID 类选择器


ID 选择器和 类选择器 比较相似, 但是有一些重要的区别:

  1. ID 选择器使用 #, 类选择器使用 ..
  2. ID 选择器用来匹配 id 属性, 而类选择器用来匹配 class 属性.
  3. ID 是唯一的,因此你的整个文档中应该只有一个特定值的 id 属性,不能把相同的值赋给不同元素的 id 属性.

例子


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Simple styling of a simple document</title>
<style type="text/css">
html { background-color: white; }
*#lead-para {font-weight: bold;}
</style>
</head>
<body>
<p id="lead-para">This paragraph will be boldfaced.</p>
<p>This paragraph will NOT be bold.</p>
</body>
</html>

例子中 *#lead-para {font-weight: bold;} 中的 * 可以省略. #lead-para {font-weight: bold;} 这样写效果是一样的.

Published at:
June 18, 2025
Keywords:
css
ui
selector