css选择器
1、id选择器 #red{color:red} <p id="red"></p>
2、类选择器 .red{color:red} <p class="red"></p>
3、属性选择器 [title]{color:red} <p title="css"></p>
4、属性和值选择器 [title=myTitle]{color:red} <p title="myTitle"></p><p title="css"></p>样式只有在前者起作用
插入样式表
1、外部样式表
<link rel="stylesheet" type="text/css" href="mycss.css"/>
2、内部样式表
注:单个文档需要特殊样式时使用(在<head>标签内部)
<html>
<head>
<style type="text/css">
hr{color:red}
</style>
</head>
<body>
</body>
</html>
3、内联样式
<html>
<head>
</head>
<body>
<p style="color:red;margin-left:20px">this is a paragraph</p>
</body>
</html>
css设置背景
1、背景颜色
p{background-color:gray;}
2、背景图片
p{background-image:url(/path/img.gif);}
css背景关联
若文档较长,当文档向下滚动时,背景图片也会随之滚动,当文档滚动超过图像的位置时,图像就会消失。
解决方案:
通过设置 background-attachment属性防止滚动
body{
background-image:url(/path/img.gif);
background-attachment:fixed
}