以前做j2ee,超级烦写css,属性太多,看来还是绕回来了!
1、元素选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<!--描述:元素选择器 -->
<style type="text/css">
div{
background-color: red;
font:italic normal bold 14pt normal 楷体_GB2312;
}
</style>
</head>
<body>
<divr>
师然学习JAVA
</div>
</body>
</html>
效果如下:
2、ID选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<!--描述:元素选择器 -->
<style type="text/css">
div{
background-color: red;
font:italic normal bold 14pt normal 楷体_GB2312;
}
#shiran{
width:200px;
height:30px;
background-color: #ddd;
padding:3px
}
</style>
</head>
<body>
<div id="shiran">
师然学习JAVA
</div>
</body>
</html>
效果如下:
如果只想对某一id的某一个元素起作用,可以这样写
例如:只对多个div元素中的某一个id为shiran的元素起作用:
div#shiran{
width:200px;
height:30px;
background-color: #ddd;
padding:3px
}
3、class选择器
我以前会叫它“类选择器”,别人也这么叫,但是这样的称呼并不合适,它和类没有关系。只是好像把一些属性放到了一起而已,谁用都可以。
这个class选择器也和ID选择器一样,可以写上元素名。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<style type="text/css">
.shiranClass{
background-color: red;
}
</style>
</head>
<body>
<div class="shiranClass">师然学习JAVA</div>
</body>
</html>
效果如下:
4、包含选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<style>
div{
font-family: "微软雅黑";
background-color: yellow;
}
div.shiran{
font-family: "微软雅黑";
background-color: red;
}
</style>
</head>
<body>
<div>师然学java</div>
<div>
<section>
<div class="shiran">师然学HD5</div>
</section>
</div>
<p class="shiran">师然学iOS</p>
</html>
效果如下:
5、子选择器
子选择器和包含选择器有点像,区别在于:
包含选择器的目标元素还要在父元素下就可以,孙子元素也可以。
但是子选择器就必须是儿子,也就是说目标元素必须是直接子元素,孙子都不行。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<style type="text/css">
div{
width: 200px;
height: 30px;
background-color:red ;
margin: 5px;
}
div>.shiran{
width: 300px;
height: 40px;
background-color:yellow ;
margin: 10px;
}
</style>
<body>
<div>我是1号DIV</div>
<div><p class="shiran">我是2号DIV</p></div>
<div><section><p class="shiran">我是3号DIV</p></section></div>
</body>
</html>
效果如下:
6、兄弟选择器
这个有点难理解,我理解的意思是这样,目标选择器作用的位置元素在匹配的某选择器的 后面。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>师然学习笔记</title>
<style type="text/css">
#android ~ .long{
background-color: #00FF00;
}
</style>
</head>
<body>
<div>
<div>师然学习Java</div>
<div class="long">师然学习H5</div>
<div id="android">师然学习iOS</div>
<p class="long"> 师然学习安卓</p>
<div class="long">师然学习.NET</div>
</div>
</body>
</html>
看代码,android是我的第一个选择器,是一个ID选择器,然后有一个class选择器叫long.
那么,再看我的几个html元素。在ID为android后面的元素,如果class是long,这个才生效。