css、类选择器、id选择器、html选择器
2013-03-06 14:54
337 查看
一、使用CSS
1、为什么要使用css:统一网站风格
csss使用语法:
选择器{
属性1:属性值;
属性2:属性值;
}
dome1.html 文件:
<html>
<head>
<link rel="stylesheet" type="text/css" href="demo1.css">
</head>
<body>
<!--span元素是用于存放一小块内容-->
<span class="css1">标题一</span><br />
<span class="css2">标题二</span><br />
<span class="css3">标题三</span><br />
<span class="css4">标题四</span><br />
<span class="css5">标题五</span><br />
<br />
</body>
</html>
css文件:
.css1{
font-size:14px;
color:red;
}
.css2{
font-size:20px;
color:green;
font-style:italic;
}
.css3{
font-size:30px;
color:silver;
font-weight:bold;
}
.css4{
font-size:25px;
color:yellow;
text-decoration:underline;
}
.css5{
font-size:20px;
background-color:blue;
}
效果:
![](http://images.cnitblog.com/blog/275842/201303/06134652-d4adcc9315ab4e7f98d332f2a0dd9517.png)
2、css使用滤镜效果:
代码如下:
<html>
<head>
<title>css滤镜效果以及css内部链接的使用</title>
<!--css内部链接用style标签对,只能对本页面有效-->
<style type="text/css">
/*这里用到伪类展示移动过程中的效果*/
a:link img{ /*a:link:设置 a 对象在未被访问前的样式。*/
filter:gray;
}
a:hover img{ /*a:hover:设置对象在其鼠标悬停时的样式。*/
filter:"";
}
</style>
</head>
<body>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
</body>
</html>
二、常用4中选择器
1、类选择器(class选择器)
基本使用
.类选择器{
属性名:属性值;
.........
} 如:
.style1{
/*css文件是通过这样定义注释的*/
width:400px;
height:350px;/*特别要注意分号不能少,一定要写一个分号,少了分号后面就有问题了*/
border:1px solid red;
background-color:silver;
margin-left:100px;
margin-top:50px;
padding-top:20px;
padding-left:30px;
}
2、id选择器
基本使用
#id选择器{
属性名:属性值;
.........
}
如:
#mid{
font-size:12px;
color:red;
}
3、html元素选择器
使用方法
html元素{
属性名:属性值;
.........
}
如
p{
background-color:red;
font-size:14px;
}
table{
border:1 solid;
}
小技巧:对html元素样式分类:在2个p标签中的内容显示方式不一样。下面看看是怎么实现的(注意看标有红颜色字体)?
<html>
<head>
<title>使用html选择器对2个p元素内容不同处理方法</title>
<!--css内部链接用style标签对,只能对本页面有效-->
<style type="text/css">
p.num{
font-size:14px;
color:red;
text-decoration:underline;
}
p.num1{
font-size:16px;
color:green;
}
</style>
</head>
<body>
<p class="num">他说,对这个事,重庆市委态度是鲜明的,措施是坚决的,就是要依法办事</p>
<p class="num1">他强调,对涉及不雅视频的干部,要深入调查,切实以事实为依据,以法律为准绳,去依法依纪处理,最后的处理结果,有关部门会适时向外界公布</p>
</body>
</html>
效果图:
![](http://images.cnitblog.com/blog/275842/201303/06145019-182f6a9ec5e84dc7b91532bce11cb95e.png)
4、通配符选择器
该选择器可以用到所有的html元素,但是其优先权最低
优先级表现:Id选择器> class选择器> html选择器> 通配符选则器
css 文件可以使用在各种文件(如html,php,…)
*{
属性名:属性值;
.........................
}
小技巧:让body内容在网页一直居中显示:
body{
border:1px solid red;
width:500px;/*高与宽自己需求设定*/
height:500px;
margin:0px auto;
}
1、为什么要使用css:统一网站风格
csss使用语法:
选择器{
属性1:属性值;
属性2:属性值;
}
dome1.html 文件:
<html>
<head>
<link rel="stylesheet" type="text/css" href="demo1.css">
</head>
<body>
<!--span元素是用于存放一小块内容-->
<span class="css1">标题一</span><br />
<span class="css2">标题二</span><br />
<span class="css3">标题三</span><br />
<span class="css4">标题四</span><br />
<span class="css5">标题五</span><br />
<br />
</body>
</html>
css文件:
.css1{
font-size:14px;
color:red;
}
.css2{
font-size:20px;
color:green;
font-style:italic;
}
.css3{
font-size:30px;
color:silver;
font-weight:bold;
}
.css4{
font-size:25px;
color:yellow;
text-decoration:underline;
}
.css5{
font-size:20px;
background-color:blue;
}
效果:
![](http://images.cnitblog.com/blog/275842/201303/06134652-d4adcc9315ab4e7f98d332f2a0dd9517.png)
2、css使用滤镜效果:
代码如下:
<html>
<head>
<title>css滤镜效果以及css内部链接的使用</title>
<!--css内部链接用style标签对,只能对本页面有效-->
<style type="text/css">
/*这里用到伪类展示移动过程中的效果*/
a:link img{ /*a:link:设置 a 对象在未被访问前的样式。*/
filter:gray;
}
a:hover img{ /*a:hover:设置对象在其鼠标悬停时的样式。*/
filter:"";
}
</style>
</head>
<body>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
</body>
</html>
二、常用4中选择器
1、类选择器(class选择器)
基本使用
.类选择器{
属性名:属性值;
.........
} 如:
.style1{
/*css文件是通过这样定义注释的*/
width:400px;
height:350px;/*特别要注意分号不能少,一定要写一个分号,少了分号后面就有问题了*/
border:1px solid red;
background-color:silver;
margin-left:100px;
margin-top:50px;
padding-top:20px;
padding-left:30px;
}
2、id选择器
基本使用
#id选择器{
属性名:属性值;
.........
}
如:
#mid{
font-size:12px;
color:red;
}
3、html元素选择器
使用方法
html元素{
属性名:属性值;
.........
}
如
p{
background-color:red;
font-size:14px;
}
table{
border:1 solid;
}
小技巧:对html元素样式分类:在2个p标签中的内容显示方式不一样。下面看看是怎么实现的(注意看标有红颜色字体)?
<html>
<head>
<title>使用html选择器对2个p元素内容不同处理方法</title>
<!--css内部链接用style标签对,只能对本页面有效-->
<style type="text/css">
p.num{
font-size:14px;
color:red;
text-decoration:underline;
}
p.num1{
font-size:16px;
color:green;
}
</style>
</head>
<body>
<p class="num">他说,对这个事,重庆市委态度是鲜明的,措施是坚决的,就是要依法办事</p>
<p class="num1">他强调,对涉及不雅视频的干部,要深入调查,切实以事实为依据,以法律为准绳,去依法依纪处理,最后的处理结果,有关部门会适时向外界公布</p>
</body>
</html>
效果图:
![](http://images.cnitblog.com/blog/275842/201303/06145019-182f6a9ec5e84dc7b91532bce11cb95e.png)
4、通配符选择器
该选择器可以用到所有的html元素,但是其优先权最低
优先级表现:Id选择器> class选择器> html选择器> 通配符选则器
css 文件可以使用在各种文件(如html,php,…)
*{
属性名:属性值;
.........................
}
小技巧:让body内容在网页一直居中显示:
body{
border:1px solid red;
width:500px;/*高与宽自己需求设定*/
height:500px;
margin:0px auto;
}
相关文章推荐
- css基础-html中使用css、基本选择器、类选择器、id选择器、通用选择器、符合选择器、css权重
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第八讲:DIV+CSS中类选择器、id选择器和html选择器介绍
- [HTML] CSS Id 和 Class选择器
- HTML基础之CSS-id选择器
- html css学习笔记-派生选择器 id选择器 类选择
- 韩顺平_php从入门到精通_视频教程_第11讲_初识css_类选择器_id选择器_html选择器_学习笔记_源代码图解_PPT文档整理
- 02 初识css 、 css类选择器、 id选择器、 html选择器
- 第009讲 初识css 类选择器 id选择器 html选择器
- css入门之html选择器,ID选择器,类选择器,属性选择器
- css 类选择器 id选择器 html选择器 通配符选择器 父子选择器
- css笔记10:多个id选择器/类选择器包含相同部分问题的探讨
- html和css基础知识复习(1)--四种语言的联系与区分,HTML语义化,id和name
- CSS id 选择器
- html中css三种常见的样式选择器
- CSS中的id选择器和类选择器的用法
- 04---HTML+CSS---CSS外联和选择器
- 011初识css 类选择器 id选择器 html选择器
- 10006---CSS id 和 class 选择器
- CSS id选择器
- Css基础-id选择器