CSS的选择器
2017-07-16 本文已影响4人
阿凡提说AI
1.标签选择器、类选择器、id选择器、后代选择器
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*标签选择器*/
div{
color: red;
}
p{
color: blue;
}
/*类选择器*/
.one{
color: yellow;
}
/*id选择器*/
#main{
font-size: 40px;
}
/*后代选择器*/
#test2 div{
color: green;
}
</style>
</head>
<body>
<div id="main">1111111111</div>
<div>1111111111</div>
<div>1111111111</div>
<p>2222222222222</p>
<div class="one">222222</div>
<p class="one">444444444</p>
<div id="test2">
<p>
<div class="test1">
rewgrgrtwgrt
</div>
</p>
</div>
</body>
</html>
2.伪类
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div{
background-color: red;
width: 200px;
height: 300px;
}
div:hover{
background-color: green;
width: 100px;
height: 180px;
}
input:focus{
outline: none;
width: 500px;
height: 300px;
border: 10px double yellow;
}
</style>
</head>
<body>
<div>11111</div>
<p></p>
<input>
</body>
</html>
3.选择器的优先级别
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<!--
css选择器遵循:
1.在相同级别:1.叠加原则 2.就近原则
2. id选择器> 类选择器 > 标签选择器
3. 范围越小,优先级别越高
-->
<style>
div#main{ /*101*/
color: goldenrod;
}
/*id选择器*/
#main{ /*100*/
color: deeppink;
}
/*类选择器*/
.test2{ /*10*/
color: purple;
}
.test1{ /*10*/
color: yellow;
}
/*标签选择器*/
div{ /*1*/
color: red;
}
div{ /*1*/
color: green;
}
div{ /*1*/
color: blue;
font-size: 100px;
}
*{ /*1000*/
color: darkgreen !important;
}
</style>
</head>
<body>
<div id="main" class="test1 test2" style="color: red">111111</div>
</body>
</html>
4.html标签的类型
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*块级标签*/
div{
background-color: red;
width: 200px;
height: 180px;
/*隐藏*/
/*display: none;*/
/*display: inline;*/
display: inline-block;
}
/*p{*/
/*background-color: yellow;*/
/*width: 200px;*/
/*height: 50px;*/
/*}*/
/*行内标签*/
span{
background-color: yellow;
/*让行内标签变成块级标签*/
/*display: block;*/
/*width: 200px;*/
/*height: 50px;*/
}
/* 行内-块级标签*/
input{
width: 200px;
height: 300px;
}
</style>
</head>
<body>
<div>11111111</div>
<div>11111111</div>
<div>11111111</div>
<span>22222222</span>
<span>22222222</span>
<span>22222222</span>
<a href="#">百度一下</a>
<a href="#">百度一下</a>
<a href="#">百度一下</a><br><br><br>
<!--<p>22222</p>-->
<input>
<input>
<input>
</body>
</html>
5.css3新增属性
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div{
font-size: 30px;
width: 150px;
background-color: red;
margin: 20px;
/*设置不透明度*/
/*opacity: 0.2;*/
/*box-shadow: 4px 14px 20px black;*/
}
/*设置透明度:红色 绿色 蓝色 透明度*/
.test1{
background-color: rgba(255,0,0,1.0);
}
.test2{
background-color: rgba(255,0,0,0.8);
}
.test3{
background-color: rgba(255,0,0,0.6);
}
.test4{
background-color: rgba(255,0,0,0.4);
}
.test5{
background-color: rgba(255,0,0,0.2);
}
p{
font-size: 50px;
color: red;
/*设置文字阴影, 水平方向 垂直方向 模糊 颜色*/
text-shadow: 5px 5px 10px green;
}
.test6{
background-color: red;
width: 400px;
height: 250px;
border: 20px solid green;
/*设置圆角*/
/*border-radius: 40px;*/
border-top-left-radius: 90px;
border-bottom-left-radius:100px;
}
</style>
</head>
<body>
<div class="test1">111</div>
<div class="test2">222</div>
<div class="test3">333</div>
<div class="test4">444</div>
<div class="test5">555</div>
<p>小码哥</p>
<div class="test6">
dewhdewjhdhjew
</div>
</body>
</html>
6.css的常用属性
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
/*设置字体大小*/
font-size: 30px;
/*设置文字颜色*/
color: red;
/*字体加粗*/
font-weight: bolder;
}
div{
/*复合属性,可用于设置颜色和图片*/
background: url("img/7.jpg");
/*设置背景的尺寸, cover跟随标签的尺寸显示*/
background-size: cover;
width: 300px;
height: 300px;
/*水平居中,取值:left right center*/
text-align: center;
/*都是隐藏内容,区别:
1.display会连同尺寸一起隐藏
2.visibility不会隐藏尺寸
*/
display: none;
visibility: hidden;
/*设置光标的显示类型: crosshair pointer*/
cursor: pointer;
}
span{
/*背景颜色*/
background-color: blue;
/*宽度*/
width: 300px;
/*高度*/
height: 200px;
/*行高,用于垂直居中*/
line-height: 300px;
}
a{
/*设置去除下划线,用于a标签*/
text-decoration: none;
}
.one{
/*设置缩进: % */
text-indent: 3%;
}
ul{
/*设置列表的类型: none square*/
list-style:square;
}
#main{
/*设置背景颜色*/
background-color: red;
/*设置宽度*/
width: 90px;
/*设置高度*/
height: 120px;
/* 超出标签的内容处理:hidden, auto */
overflow: hidden;
}
</style>
</head>
<body>
<!--<div>111111</div>-->
<!--<span>2222222</span>-->
<!--<p>33333333333</p>-->
<div>
<span>12112323322332</span>
</div>
<p class="one">21323243423434efwqewqfewqfewqfewq</p>
<p>21323243423434fewqfewqfew</p>
<p>21323243423434fewfew</p>
<p>fewfewfewfewefw</p>
<a href="#">百度一下</a>
<ul>
<li>111111</li>
<li>111111</li>
<li>111111</li>
<li>111111</li>
</ul>
<div id="main">44444444444444444444</div>
</body>
</html>
7.盒子模型
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div{
background-color: red;
width: 250px;
height: 250px;
margin: 130px;
/*设置边框: 边框宽度 边框样式 边框颜色*/
border: 10px solid green;
/*内边距: 上右下左*/
padding:20px 40px 100px 20px;
}
</style>
</head>
<body>
<div>122112</div>
</body>
</html>
8.居中
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<!--
标签水平居中:
行内标签, 行内块级标签: text-align: center;
块级标签: margin: 0px auto;
标签垂直居中:
line-height: 400px;
-->
<style>
.test1{
background-color: red;
width: 300px;
height: 400px;
text-align: center;
line-height: 400px;
}
span{
background-color: purple;
}
.test2{
background-color: green;
width: 90px;
/*margin-left: auto;*/
/*margin-right: auto;*/
margin: 0px auto;
text-align: center;
}
</style>
</head>
<body>
<div class="test1">
<!--行内标签-->
<span>12212</span>
<!--行内-块级标签-->
<button>百度一下</button>
<!--块级标签-->
<div class="test2">efwf</div>
</div>
</body>
</html>