发新话题
打印

为何设置的CSS ID 不起作用?

为何设置的CSS ID 不起作用?

HTML代码如下,可是ID为sidebar的P元素内中华人民共和国字没有样式?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
#sidebar p{
font-style: italic;
color: #F00;
text-align: right;
margin-top: 0.5em;
}
-->
</style>
</head>
<body>
<div>
<p id="sidebar">中华人民共和国</p>
</div>

</body>
</html>
你错误的定义CSS选择器
应该是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
p#sidebar {
font-style: italic;
color: #F00;
text-align: right;
margin-top: 0.5em;
}
-->
</style>
</head>
<body>
<div>
<p id="sidebar">中华人民共和国</p>
</div>

</body>
</html>
<div>
<p id="sidebar">中华人民共和国</p>
</div>

改成
<div id="sidebar">
<p>中华人民共和国</p>
</div>

也可以
发新话题