CSS

ID选择器 | ID selectors

ID selectors

在HTML文档中,CSS ID选择器根据其id属性的值匹配元素。所选元素的ID属性必须与选择器中给定的值完全匹配。

/* The element with id="demo" */ div#demo { border: red 2px solid; }

语法

#id_value { style properties }

请注意,这相当于以下内容attribute selector

[id=id_value] { style properties }

实例

CSS

div#identified { background-color: skyblue; }

HTML

<div id="identified">This div has a special ID on it!</div> <div>This is just a regular div.</div>

结果

规范

SpecificationStatusComment
Selectors Level 4The definition of 'ID selectors' in that specification.Working Draft
Selectors Level 3The definition of 'ID selectors' in that specification.Recommendation
CSS Level 2 (Revision 1)The definition of 'ID selectors' in that specification.Recommendation
CSS Level 1The definition of 'ID selectors' in that specification.RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support??(Yes)????