一些CSS选择器前面有#,这是什么意思?
发布于 2010-09-14 13:05:52
它是ID选择器,这是CSS标准的一个基本特性。它根据id属性将HTML元素与给定的ID进行匹配(当然,假设是一个符合规范的文档)。有关详细信息,请参阅the W3C Selectors spec。
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
#my-div {
color: #f00;
}
</style>
</head>
<body>
<div id="my-div">This text will be red.</div>
<div id="another-div">This text will not be red.</div>
</body>
</html>您可能已经看到在URL片段标识符中用于引用命名锚点(<a name="some-anchor"></a>)的#表示法。它们还可以指向页面中具有特定ID的元素,就像命名锚点一样,我猜这就是为什么CSS使用相同的符号来选择ID。
发布于 2010-09-14 13:08:41
选择器#foo将匹配ID属性为"foo“的任何元素。
<style type='text/css'>
#foo { color: red; }
</style>
<div id='foo'>red text</div>发布于 2010-09-14 13:08:05
它根据html元素的id进行选择...
http://www.w3.org/TR/CSS2/selector.html#id-selectors
<style>
#myDiv { }
</style>
<div id="myDiv">
</div>https://stackoverflow.com/questions/3706236
复制相似问题