CSS实战与技巧
第一章 css常用技巧
1-1 hover使用技巧(hover可以设置的元素)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#zxw{
width: 400px;height: 400px;background: #000;
}
.zxw2{
width: 150px;height: 400px;background: #ff0000;
}
/*.zxw2:hover{
width: 300px;height: 300px;background: #00ff00; 设置自己元素
}*/
/*#zxw:hover .zxw2{
width: 300px;height: 300px;background: #00ff00; 设置自己的后代元素
}*/
/*#zxw:hover .zxw2 p{
color: blue;font-size: 30px; 设置自己的后代的后代元素
}*/
/*#zxw3:hover{
color: #fff;font-size: 20px; 设置自己元素
}*/
/*#zxw3:hover+p{
color: #fff;font-size: 20px; 加号可以设置自己元素的下一个元素的属性
}*/
/*#zxw3:hover~p{
color: #fff;font-size: 20px; ~可以设置自己元素之后的所有元素的属性
}*/
/*使用以上css验证 解开注释即可验证*/
</style>
</head>
<body>
<!--元素在hover状态时:
1.可以设置自己的属性
2.可以设置自己的后代元素的属性
3.可以设置自己之后兄弟元素的属性
4.不能设置自己的祖先元素-->
<div id="zxw">
<div class="zxw2">
<p>我要自学网</p>
<p>我要自学网</p>
<p>我要自学网</p>
<p id="zxw3">我要自学网</p>
<p>我要自学网</p>
<p>我要自学网</p>
</div>
</div>
</body>
</html>
1-2 移入弹出大图(移入后弹出大图效果)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
#zxw{
width: 300px;height: 235px;position: relative;
}
.one{
width: 100%;height: 100%;
}
.two{
width:600px;height: 470px;position: absolute;
top:0;left:300px;display: none;
}
#zxw:hover .two{
display: block;
}
/*对上一节课的实践*/
</style>
</head>
<body>
<div id="zxw">
<img class="one" src="../../img/01.jpg"/>
<img class="two" src="../../img/02.jpg"/>
</div>
</body>
</html>
1-3 多级导航(多级导航栏的编辑方式)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
.ul1{
width: 502px;height: 52px;border: 1px solid #000;
margin: 100px auto;text-align:center;
}
.li1{
width: 98px;height: 50px;line-height: 50px;margin:1px 1px;
float: left;position: relative;
}
.li1 div{
width: 98px;height: 50px;background: #ccc;
}
.ul2{
position: absolute;top:52px;left:0;display: none;
}
.li1:hover .ul2{
display: block;
}
.li2{
background:#ccc;width: 98px;height: 50px;border-bottom:1px solid #000000
}
</style>
</head>
<body>
<nav>
<!--ul.ul1>li.li1{一级导航$}*5-->
<ul class="ul1">
<li class="li1">
<div>一级导航1</div>
<ul class="ul2">
<li class="li2">二级导航1</li>
<li class="li2">二级导航2</li>
<li class="li2">二级导航3</li>
<li class="li2">二级导航4</li>
<li class="li2">二级导航5</li>
</ul>
</li>
<li class="li1">
<div>一级导航2</div>
<ul class="ul2">
<li class="li2">二级导航1</li>
<li class="li2">二级导航2</li>
<li class="li2">二级导航3</li>
<li class="li2">二级导航4</li>
<li class="li2">二级导航5</li>
</ul>
</li>
<li class="li1">
<div>一级导航3</div>
<ul class="ul2">
<li class="li2">二级导航1</li>
<li class="li2">二级导航2</li>
<li class="li2">二级导航3</li>
<li class="li2">二级导航4</li>
<li class="li2">二级导航5</li>
</ul>
</li>
<li class="li1">
<div>一级导航4</div>
<ul class="ul2">
<li class="li2">二级导航1</li>
<li class="li2">二级导航2</li>
<li class="li2">二级导航3</li>
<li class="li2">二级导航4</li>
<li class="li2">二级导航5</li>
</ul>
</li>
<li class="li1">
<div>一级导航5</div>
<ul class="ul2">
<li class="li2">二级导航1</li>
<li class="li2">二级导航2</li>
<li class="li2">二级导航3</li>
<li class="li2">二级导航4</li>
<li class="li2">二级导航5</li>
</ul>
</li>
</ul>
</nav>
</body>
</html>
1-4 纯css点击切换(css点击标题切换内容)
第一种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
/*这里运用的是锚链接和overflow属性*/
.box{
width: 302px;height: 302px;border:1px solid #000;
text-align: center;margin: 0 auto;
}
nav{
width: 302px;height: 30px;border-bottom:2px solid #000;
}
nav a{
display: block;width: 100px;height: 28px;line-height: 28px;
float: left;
}
.content{
width: 300px;height:270px;overflow: hidden;
}
.cont{
width: 300px;height:270px;line-height: 270px;
font-size: 40px;color: #fff;
}
#one{
background: red;
}
#two{
background: green;
}
#three{
background: blue;
}
</style>
</head>
<body>
<div class="box">
<nav>
<a href="#one">按钮1</a>
<a href="#two">按钮2</a>
<a href="#three">按钮3</a>
</nav>
<div class="content">
<div id="one" class="cont">内容1</div>
<div id="two" class="cont">内容2</div>
<div id="three" class="cont">内容3</div>
</div>
</div>
</body>
</html>
第二种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
/*这里运用的是锚链接,定位和target伪类*/
.box{
width: 302px;height: 302px;border:1px solid #000;
text-align:center;margin: 0 auto;
}
nav{
width: 302px;height: 30px;border-bottom:2px solid #000;
}
nav a{
display: block;width: 100px;height: 28px;line-height: 28px;
float: left;
}
.content{
width: 300px;height:270px;position: relative;
}
.cont{
width: 302px;height:270px;line-height: 270px;
font-size: 40px;color: #000;
position: absolute;top:0;left: 0;
}
#one{
background: red;z-index: 1;
}
#two{
background: green;
}
#three{
background: blue;
}
#one:target,#two:target,#three:target{
z-index: 9;font-size:50px;
}
</style>
</head>
<body>
<!--target选择的是当前被链接到目标元素-->
<!--注意:target伪类加在目标元素上-->
<div class="box">
<nav>
<a href="#one">按钮1</a>
<a href="#two">按钮2</a>
<a href="#three">按钮3</a>
</nav>
<div class="content">
<div id="one" class="cont">内容1</div>
<div id="two" class="cont">内容2</div>
<div id="three" class="cont">内容3</div>
</div>
</div>
</body>
</html>
点击相应的按钮即可跳转到相应的内容
1-5 纯css横向滚动(css设置可以横向滚动的效果)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
.box{
width: 100%;height: 302px;border:2px solid #000000 ;margin-top:100px ;
overflow-y: scroll;
/*移动端横向滚动overflow-x: scroll;*/
/*移动端纵向滚动overflow-y: scroll;*/
/*横向滚动,移动端不会出现滚动条*/
}
ul{
width: 200%;height: 300px;
}
ul li{
width: 15.666%;height: 300px;margin: 0 0.5%;float: left;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li style="background: red;"></li>
<li style="background:green;"></li>
<li style="background: blue;"></li>
<li style="background: yellow;"></li>
<li style="background: brown;"></li>
<li style="background: orange;"></li>
</ul>
</div>
</body>
</html>
1-6 清除图片下方间隙(清除图片下方的间隙和清除图片边框)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width:460px;margin: 100px auto;
border:1px solid #000;ver
}
.box span{
font-size: 30px;
}
.img{
vertical-align: top;/*文本垂直对齐方式*/
}
</style>
</head>
<body>
<!--英语本中四线划分三格,倒数第二条是基线(默认值),使用vertical-align:top;即可清除图片下方间隙-->
<!--ie10以下几个老版本浏览器中图片在a元素中如果没有设置跳转路径会带有边框,因此样式重置reset.css中使用img{border:none;}清除图片边框-->
<div class="box">
<span>YYYyy</span>
<a href="">
<img src="../../img/01.jpg">
</a>
</div>
</body>
</html>
1-7 文本溢出显示省略号(文本溢出文本框后显示省略号)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
p{
width: 202px;height: 32px;border:1px solid #000;
margin: 100px auto;line-height: 30px;
/*溢出隐藏*/
overflow: hidden;
/*文字变成一行显示*/
white-space: nowrap;
/*溢出时显示省略号*/
text-overflow: ellipsis;
/*三个属性进行配合才可以实线溢出时显示省略号的效果*/
}
</style>
</head>
<body>
<p>我要自学网我要自学网我要自学网我要自学网我要自学网我要自学网</p>
</body>
</html>
1-8 元素透明度设置(设置一个元素的整体透明度)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width: 224px;height: 310px;margin: 100px auto;
border:5px solid #000;
}
.box:hover{
opacity: 0;
/*元素透明度设置:
1.整个元素及元素内的后代元素,都会随之改变透明度;
2.不能使用百分比
3.从0.0(完全透明)到1.0(完全不透明)*/
}
img{
width:224px;height: 220px;
}
p{
height: 50px;line-height: 50px;font-size: 30px;font-weight: 700;color: red;
background: #0000FF;
}
img:hover{
/*图像透明度设置,只对图像有效*/
/*filter: opacity(30%);*/
}
</style>
</head>
<body>
<div class="box">
<img src="../../img/01.jpg"/>
<p>我要自学网</p>
</div>
</body>
</html>
1-9 隔行变色
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
ul{
width: 400px;margin: 100px auto;
}
/*li:nth-child(9n+2){
color: red;
} 设置到那个结束*/
li:nth-child(9n+2):nth-child(-n+12){
color: red;
}
/*nth-child(an+b):*/
/*1.a变化周期,相邻两个元素序号大减小*/
/*2.n计数器,从零开始*/
/*3.b偏移值,从第几号元素开始*/
</style>
</head>
<body>
<ul>
<li>我要自学网1</li>
<li>我要自学网2</li>
<li>我要自学网3</li>
<li>我要自学网4</li>
<li>我要自学网5</li>
<li>我要自学网6</li>
<li>我要自学网7</li>
<li>我要自学网8</li>
<li>我要自学网9</li>
<li>我要自学网10</li>
<li>我要自学网11</li>
<li>我要自学网12</li>
<li>我要自学网13</li>
<li>我要自学网14</li>
<li>我要自学网15</li>
<li>我要自学网16</li>
<li>我要自学网17</li>
<li>我要自学网18</li>
<li>我要自学网19</li>
<li>我要自学网20</li>
</ul>
</body>
</html>
1-10 外边距击穿(清除外边距击穿)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
.box{
width: 400px;height: 400px;background: red;
/*padding-top:1px;border:1px solid #000 ;overflow:hidden;*/
}
.zxw{
width: 200px;height: 200px;background: green;
margin-top: 100px;
}
/*img{
width:200px;height: 200px;margin-top:100px ;
}
span{
margin-left: 100px;
}*/
</style>
</head>
<body>
<!--外边距击穿:当有两个相邻的盒元素时,他们的外边距会重合,当其中一个设置了外边距之后,另一个也会应用这个外边距-->
<!--相邻的定义为:同级或嵌套 的盒元素,他们之间没有非空的内容,并且父元素没有外边距和边框分割-->
<!--如果子元素不是块级元素,就不会出现外边距击穿问题-->
<!--外边距击穿的处理方法:-->
<!--1.父元素设置溢出隐藏-->
<!--2.父元素设置内边距-->
<!--3.父元素设置边框-->
<div class="box">
<div class="zxw"></div>
</div>
<!--<div class="box">
<img src="../../img/01.jpg"/>
<span>我要自学网</span>
</div>-->
</body>
</html>
1-11 优先级提升(提升元素属性的优先级)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
#zxw1{
color: blue;
}
.zxw{
color:red;
}
p{
color: yellow!important;
}
</style>
</head>
<body>
<!--1.!important把被修饰的语句的优先级提升到最大-->
<!--2.只能提升一个语句的优先级,不能提生一个选择器的优先级-->
<p class="zxw" id="zxw1">我要自学网</p>
</body>
</html>
继承样式<l浏览器预设的样式<通用选择器<标签选择器<类选择器<id选择器<行内样式(style属性)
1-12 禁止选中文本(禁止选中文本效果)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
p{
width: 300px;height: 50px;font-size: 30px;
margin: 50px auto;
}
a{
user-select:none;
}
</style>
</head>
<body>
<!--user-select:none;禁止选中文本效果,页面美观-->
<p>
<a href="#">我要自学网我要自学网我要自学网我要自学网</a>
</p>
</body>
</html>
第二章 矢量图标及背景精灵
2-1 矢量图标浅解
矢量图:是根据几何特性来绘制图形,他的特点时放大后图像不会失真,和分辨率无关
位图:是由称作像素(图片元素)的单个点组成的,放大后,会变成马赛克
2-2 矢量图标下载及引入
1.进入阿里巴巴矢量图标库:http://www.iconfont.cn/
2.登录,可以微博登录
3.鼠标悬停在需要的图标,点击收藏 入库(购物车图标)
4.把购物车图标加入项目
5.点击下载至本地,download.zip
6.下载完成后进行解压,把文件复制到网站项目中
2-3 font-class方式引入图标
font-class是Unicode书写不直观,语意不明确的问题
与Unicode使用方法相比,具有如下特点:
1.兼容性良好,支持IE8+,及所有现代浏览器
2.相比于Unicode语意明确,书写更直观,可以很容易分辨这个icon是什么
3.因为使用class来定义图标,所以当要替换图标时,只需要修改class里面的Unicode引用
4.不过因为本质上还是使用的字体,所以多色图标还是不支持的
2-4 单标签使用矢量图标
第一步:引入项目下面生成的fontclass代码:
第二部:挑选相应图标并获取类名,应用于页面: ## 2-5 symbol方式引入图标 这是一种全新的使用方式,应该说这才是未来的主流,也是平台目前推荐的用法。相关介绍可以参考这篇文章这种用法其实是做了一个SVG的集合,与另外两种相比具有如下特点: 1.支持多色图标了,不再受单色限制 2.通过一些技巧,支持像字体那样,通过font-class,color来调整样式 3.兼容性较差,支持IE9+,及现代浏览器 4.浏览器渲染SVG的性能一般,还不如png 使用步骤如下: 1.引入项目下面生成的symbol代码: 2.加入通过CSS代码(引入一次就行) 3.挑选相应图标并获取类名,应用于页面: 改变图片的大小和字体一样 通过改变svg标签的css来改变 ## 2-6 实战矢量图标引用<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<link rel="stylesheet" href="../../icon/font_51zxw/iconfont.css"/>
<style type="text/css">
#zxw{
width:50px;padding:0 5px;border: 1px solid #8cc63e;margin: 100px auto;
}
#zxw a{
display: block;width: 50px;height: 52px;border-bottom: 2px solid #8cc63e;text-align:center;
}
#zxw #nobord{
height: 50px;border-bottom: none;
}
#zxw a .iconfont{
font-size:36px;line-height: 50px;color:#8cc63e;
}
</style>
</head>
<body>
<div id="zxw">
<a href="">
<span class="iconfont icon-shouye"></span>
</a>
<a href="">
<span class="iconfont icon-QQ"></span>
</a>
<a id="nobord" href="">
<span class="iconfont icon-shouji"></span>
</a>
</div>
</body>
</html>
2-7 背景精灵简介
1.背景精灵技术(雪碧图):就是在一张大图片上集成了很多张小图片上。使用背景定位来显示要显示的图片
2.背景精灵技术 的作用:可以减少向服务器请求的次数,降低服务器的压力,加快浏览器加载
2-8 背景大图制作技巧
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
.zxw{
width: 400px;height:50px;background: bisque;
margin:100px auto;
}
.zxw a{
display: block;width:80px;height: 50px;line-height: 50px;
float: left;text-align:right;padding-right: 20px;
background: url(../../img/zxw1.png) no-repeat;
}
#a1{
background-position:0 0;
}
#a2{
background-position:0 -50px;
}
#a3{
background-position:0 -100px;
}
#a4{
background-position:0 -150px;
}
</style>
</head>
<body>
<div class="zxw">
<a href="" id="a1">
<span>首页</span>
</a>
<a href="" id="a2">
<span>QQ</span>
</a>
<a href="" id="a3">
<span>微信</span>
</a>
<a href="" id="a4">
<span>手机</span>
</a>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
.zxw{
width: 50px;background: bisque;
margin:100px auto;
}
.zxw a{
display: block;width:50px;height: 80px;text-align: center;
padding-top: 50px;box-sizing: border-box;
background: url(../../img/zxw2.png) no-repeat;
}
#a1{
background-position:0 0;
}
#a2{
background-position:-50px 0;
}
#a3{
background-position:-100px 0;
}
#a4{
background-position:-150px 0;
}
</style>
</head>
<body>
<div class="zxw">
<a href="" id="a1">
<span>首页</span>
</a>
<a href="" id="a2">
<span>QQ</span>
</a>
<a href="" id="a3">
<span>微信</span>
</a>
<a href="" id="a4">
<span>手机</span>
</a>
</div>
</body>
</html>
制作背景大图,适用于背景图片,没有专门的盒子存放
1.图标和文字上下排列,要做单行背景图,每个图标所占的宽度,等于元素的宽度
2.图标和文字左右排列,要做单列背景图,每个图标所占的高度,等于元素的高度
2-9.固定尺寸背景精灵设置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
<style type="text/css">
/*远程培训班开始*/
.ycpx{
height:160px;background: #fff;padding-top:22px ;width:1200px;margin: 0 auto;
}
.ycpx a{
display: block;width:210px;height: 114px;float: left;
margin: 0 15px;border-radius: 12px;overflow: hidden;
color:#fff;font-size:24px;font-weight: 700px;text-align: center;line-height: 114px;
background: url(../../img/zxw3.png) no-repeat;
}
.ycpx a:hover{
/*background-size:110% 110% ;*/
}
.ycpx .a1{
background-position:0 0 ;
}
.ycpx .a2{
background-position:-208px 0;
}
.ycpx .a3{
background-position:-416px 0;
}
.ycpx .a4{
background-position:-624px 0;
}
.ycpx .a5{
background-position:-832px 0;
}
/*远程培训班结束*/
</style>
</head>
<body>
<!--远程培训班开始-->
<div class="ycpx">
<a class="a1" href="#">
</a>
<a class="a2" href="#">
</a>
<a class="a3