精灵图 为什么需要精灵图?
一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器就会频繁地接收和发送 请求图片,造成服务器请求压力过大,这将大大降低页面的加载速度。
因此,为了有效地减少服务器接收和发送请求的次数,提高页面的加载速度,出现了 CSS 精灵技术(也称 CSS Sprites、CSS 雪碧)。
核心原理:将网页中的一些小背景图像整合到一张大图中 ,这样服务器只需要一次请求就可以了。
淘宝精灵图 王者荣耀精灵图
精灵图的使用 使用精灵图核心: 1.精灵技术主要针对于背景图片使用。就是把多个小背景图片整合到一张大图片中。 2.这个大图片也称为 sprites 精灵图 或者 雪碧图 3. 移动背景图片位置, 此时可以使用 background-position 。 4. 移动的距离就是这个目标图片的 x 和 y 坐标。注意网页中的坐标有所不同 5. 因为一般情况下都是往上往左移动,所以数值是负值。 6. 使用精灵图的时候需要精确测量,每个小背景图片的大小和位置。
使用精灵图核心总结:
精灵图主要针对于小的背景图片使用。
主要借助于背景位置来实现—background-position
。
一般情况下精灵图都是负值。(千万注意网页中的坐标: x轴右边走是正值,左边走是负值, y轴同理。)
案例:拼出自己名字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 利用精灵图拼出自己名字</title > <style > span { display: inline-block; background: url(images/abcd.jpg) no-repeat; } .p { width: 100px; height: 112px; background-position: -493px -276px; } .i { width: 60px; height: 108px; background-position: -327px -142px; } .n { width: 108px; height: 109px; background-position: -215px -141px; } .k { width: 105px; height: 114px; background-position: -495px -142px; } </style > </head > <body > <span class ="p" > p</span > <span class ="i" > i</span > <span class ="n" > n</span > <span class ="k" > k</span > </body > </html >
字体图标 字体图标的产生 字体图标使用场景: 主要用于显示网页中通用、常用的一些小图标。 精灵图是有诸多优点的,但是缺点很明显。 1.图片文件还是比较大的。 2.图片本身放大和缩小会失真。 3.一旦图片制作完毕想要更换非常复杂。
此时,有一种技术的出现很好的解决了以上问题,就是字体图标 iconfont
。 字体图标可以为前端工程师提供一种方便高效的图标使用方式,展示的是图标,本质属于字体 。
字体图标的优点
轻量级:一个图标字体要比一系列的图像要小。一旦字体加载了,图标就会马上渲染出来,减少了服务器请求
灵活性:本质其实是文字,可以很随意的改变颜色、产生阴影、透明效果、旋转等
兼容性:几乎支持所有的浏览器,请放心使用
注意: 字体图标不能替代精灵技术,只是对工作中图标部分技术的提升和优化。
总结: 1.如果遇到一些结构和样式比较简单的小图标,就用字体图标。 2.如果遇到一些结构和样式复杂一点的小图片,就用精灵图。
字体图标是一些网页常见的小图标,我们直接网上下载即可。 因此使用可以分为: 1.字体图标的下载 2.字体图标的引入(引入到我们html页面中) 3.字体图标的追加 (以后添加新的小图标)
推荐下载网站:
icomoon 字库 http://icomoon.io
推荐指数 ★★★★★ IcoMoon 成立于 2011 年,推出了第一个自定义图标字体生成器,它允许用户选择所需要的图标,使它们成 一字型。该字库内容种类繁多,非常全面,唯一的遗憾是国外服务器,打开网速较慢。
阿里 iconfont 字库 http://www.iconfont.cn/
推荐指数 ★★★★★ 这个是阿里妈妈 M2UX 的一个 iconfont 字体图标字库,包含了淘宝图标库和阿里妈妈图标库。可以使用 AI 制作图标上传生成。 重点是,免费!
字体图标的引入 下载完毕之后,注意原先的文件不要删,后面会用。
把下载包里面的 fonts 文件夹放入页面根目录
字体文件格式 不同浏览器所支持的字体格式是不一样的,字体图标之所以兼容,就是因为包含了主流浏览器支持的字体文件。 1.TureType(.ttf )格式.ttf字体是Windows和Mac的最常见的字体,支持这种字体的浏览器有IE9+、Firefox3.5+、Chrome4+、Safari3+、Opera10+、iOS Mobile、Safari4.2+; 2.Web Open Font Format(.woff )格式woff字体,支持这种字体的浏览器有IE9+、Firefox3.5+、Chrome6+、Safari3.6+、Opera11.1+; 3.Embedded Open Type(.eot )格式.eot字体是IE专用字体,支持这种字体的浏览器有IE4+; 4.SVG(.svg )格式.svg字体是基于SVG字体渲染的一种格式,支持这种字体的浏览器有Chrome4+、Safari3.1+、Opera10.0+、iOS Mobile Safari3.2+;
2.在 CSS 样式中全局声明字体: 简单理解把这些字体文件通过css引入到我们页面中。 一定注意字体文件路径的问题。
1 2 3 4 5 6 7 8 9 10 @font-face {font-family : 'icomoon' ;src : url ('fonts/icomoon.eot?7kkyc2' );src : url ('fonts/icomoon.eot?7kkyc2#iefix' ) format ('embedded-opentype' ), url ('fonts/icomoon.ttf?7kkyc2' ) format ('truetype' ), url ('fonts/icomoon.woff?7kkyc2' ) format ('woff' ), url ('fonts/icomoon.svg?7kkyc2#icomoon' ) format ('svg' );font-weight : normal;font-style : normal; }
html 标签内添加小图标。
给标签定义字体。
1 2 3 span { font-family : "icomoon" ; }
务必保证 这个字体和上面@font-face
里面的字体保持一致
字体图标的追加 如果工作中,原来的字体图标不够用了,我们需要添加新的字体图标到原来的字体文件中。
把压缩包里面的 selection.json
从新上传,然后选中自己想要新的图标,从新下载压缩包,并替换原来的文件即可。
字体图标加载的原理:
CSS 三角 网页中常见一些三角形,使用 CSS 直接画出来就可以,不必做成图片或者字体图标。(设置宽度为0,就出来了。) 一张图, 你就知道 CSS 三角是怎么来的了, 做法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html > <html > <head > <title > CSS 三角</title > <style type ="text/css" > .box1 { width: 0px; height: 0px; border-top: 100px solid pink; border-right: 100px solid red; border-bottom: 100px solid blue; border-left: 100px solid green; } </style > </head > <body > <div class ="box1" > </div > </body > </html >
或
1 2 3 4 5 6 7 8 div { width : 0 ; height : 0 ; line-height : 0 ; font-size : 0 ; border : 50px solid transparent; border-left-color : pink; }
案例:京东三角
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <!DOCTYPE html > <html > <head > <title > CSS 三角</title > <style type ="text/css" > .box2 { position: relative; left: 50%; margin-top: 100px; margin-left: -100px; width: 200px; height: 200px; background-color: red; } .box2 .box1 { position: absolute; width: 0px; height: 0px; right: 80px; top: -50px; border: 20px solid transparent; border-bottom-color: pink; } </style > </head > <body > <div > <div class ="box2" > <div class ="box1" > </div > </div > </div > </body > </html >
案例:完成如下图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <!DOCTYPE html > <html > <head > <title > CSS 三角</title > <style type ="text/css" > .box2 { position: relative; left: 50%; margin-top: 100px; margin-left: -100px; width: 300px; height: 100px; background-color: red; } .box2 .box1 { position: absolute; width: 0px; height: 0px; right: -40px; top: 30px; border: 20px solid transparent; border-left-color: pink; } </style > </head > <body > <div > <div class ="box2" > <div class ="box1" > </div > </div > </div > </body > </html >
CSS 用户界面样式 什么是界面样式 所谓的界面样式,就是更改一些用户操作样式,以便提高更好的用户体验。
鼠标样式 cursor
设置或检索在对象上移动的鼠标指针采用何种系统预定义的光标形状。
轮廓线 outline 给表单添加 outline: 0;
或者 outline: none;
样式之后,就可以去掉默认的蓝色边框。
vertical-align 属性应用 CSS 的 vertical-align
属性使用场景: 经常用于设置图片或者表单(行内块元素)和文字垂直对齐。
官方解释: 用于设置一个元素的垂直对齐方式 ,但是它只针对于行内元素或者行内块元素有效。
语法:
1 vertical-align : baseline | top | middle | bottom
图片、表单和文字对齐 图片、表单都属于行内块元素,默认的 vertical-align 是基线对齐。 此时可以给图片、表单这些行内块元素的 vertical-align
属性设置为 middle
就可以让文字和图片垂直 居中对齐了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 利用vertical-align实现图片文字垂直居中对齐</title > <style > img { vertical-align: middle; } textarea { vertical-align: middle; } </style > </head > <body > <img src ="images/ldh.jpg" alt ="" > pink老师是刘德华 <br > <textarea name ="" id ="" cols ="30" rows ="10" > </textarea > 请您留言</body > </html >
解决图片底部默认空白缝隙问题 bug:图片底侧会有一个空白缝隙,原因是行内块元素会和文字的基线对齐。 主要解决方法有两种: 1.给图片添加 vertical-align:middle | top| bottom
等。(提倡使用的) 2.把图片转换为块级元素 display: block;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 图片底侧空白缝隙解决方案</title > <style > div { border: 2px solid red; } img { display: block; } </style > </head > <body > <div > <img src ="images/ldh.jpg" alt ="" > </div > </body > </html >
溢出的文字省略号显示 1. 单行文本溢出显示省略号
2. 多行文本溢出显示省略号
单行文本溢出显示省略号–必须满足三个条件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 单行文本溢出显示省略号</title > <style > div { width: 150px; height: 80px; background-color: pink; margin: 100px auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style > </head > <body > <div > 啥也不说,此处省略一万字 </div > </body > </html >
多行文本溢出显示省略号 多行文本溢出显示省略号,有较大兼容性问题, 适合于webKit浏览器或移动端(移动端大部分是webkit内 核)
1 2 3 4 5 6 7 8 overflow: hidden; text-overflow: ellipsis; /* 弹性伸缩盒子模型显示 */ display: -webkit-box; /* 限制在一个块元素显示的文本的行数 */ -webkit-line-clamp: 2; /* 设置或检索伸缩盒对象的子元素的排列方式 */ -webkit-box-orient: vertical;
更推荐让后台人员来做这个效果,因为后台人员可以设置显示多少个字,操作更简单。
常见布局技巧 margin负值运用
1.让每个盒子margin
往左侧移动 -1px
正好压住相邻盒子边框 2.鼠标经过某个盒子的时候,提高当前盒子的层级即可(如果没有有定位,则加相对定位(保留位置),如 果有定位,则加z-index
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > margin负值的巧妙运用</title > <style > ul li { position: relative; float: left; list-style: none; width: 150px; height: 200px; border: 1px solid red; margin-left: -1px; } /* ul li:hover { 1. 如果盒子没有定位,则鼠标经过添加相对定位即可 position: relative; border: 1px solid blue; } */ ul li :hover { z-index: 1; border: 1px solid blue; } </style > </head > <body > <ul > <li > 1</li > <li > 2</li > <li > 3</li > <li > 4</li > <li > 5</li > </ul > </body > </html >
文字围绕浮动元素 巧妙运用浮动元素不会压住文字的特性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 文字围绕浮动元素的妙用</title > <style > * { margin: 0; padding: 0; } .box { width: 300px; height: 70px; background-color: pink; margin: 0 auto; padding: 5px; } .pic { float: left; width: 120px; height: 60px; margin-right: 5px; } .pic img { width: 100%; } </style > </head > <body > <div class ="box" > <div class ="pic" > <img src ="images/img.png" alt ="" > </div > <p > 【集锦】热身赛-巴西0-1秘鲁 内马尔替补两人血染赛场</p > </div > </body > </html >
行内块巧妙运用
页码在页面中间显示:
把这些链接盒子转换为行内块, 之后给父级指定 text-align:center;
利用行内块元素中间有缝隙,并且给父级添加text-align:center;
行内块元素会水平会居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 行内块的巧妙运用</title > <style > * { margin: 0; padding: 0; } .box { text-align: center; } .box a { display: inline-block; width: 36px; height: 36px; background-color : #f7f7f7 ; border : 1px solid #ccc ; text-align: center; line-height: 36px; text-decoration: none; color : #333 ; font-size: 14px; } .box .prev , .box .next { width: 85px; } .box .current , .box .elp { background-color : #fff ; border: none; } .box input { height: 36px; width: 45px; border : 1px solid #ccc ; outline: none; } .box button { width: 60px; height: 36px; background-color : #f7f7f7 ; border : 1px solid #ccc ; } </style > </head > <body > <div class ="box" > <a href ="#" class ="prev" > < < 上一页</a > <a href ="#" class ="current" > 2</a > <a href ="#" > 3</a > <a href ="#" > 4</a > <a href ="#" > 5</a > <a href ="#" > 6</a > <a href ="#" class ="elp" > ...</a > <a href ="#" class ="next" > > > 下一页</a > 到第 <input type ="text" > 页 <button > 确定</button > </div > </body > </html >
CSS 三角强化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!DOCTYPE html > <html > <head > <title > CSS 三角</title > <style type ="text/css" > * { margin: 0; padding: 0; } .box1 { width: 0; height: 0; border-top: 50px solid pink; border-right: 50px solid skyblue; border-bottom: 50px solid blue; border-left: 50px solid green; } </style > </head > <body > <div class ="box1" > </div > </body > </html >
原理:
代码
1 2 3 4 5 width : 0;height : 0;border-color : transparent red transparent transparent ;border-style : solid ;border-width : 22px 8px 0 0;
京东秒杀价代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > CSS三角强化的巧妙运用</title > <style > .box1 { width: 0; height: 0; /* border-top: 100px solid transparent; border-right: 50px solid skyblue; */ /* border-bottom: 0 solid blue; border-left: 0 solid green; */ border-color: transparent red transparent transparent; border-style: solid; border-width: 100px 50px 0 0 ; } .price { width: 160px; height: 24px; line-height: 24px; border: 1px solid red; margin: 0 auto; } .miaosha { position: relative; float: left; width: 90px; height: 100%; background-color :red ; text-align: center; color : #fff ; font-weight: 700; margin-right: 8px; } .miaosha i { position: absolute; right: 0; top: 0; width: 0; height: 0; border-color : transparent #fff transparent transparent ; border-style: solid; border-width: 24px 10px 0 0; } .origin { font-size: 12px; color: gray; text-decoration: line-through; } </style > </head > <body > <div class ="box1" > </div > <div class ="price" > <span class ="miaosha" > ¥1650 <i > </i > </span > <span class ="origin" > ¥5650</span > </div > </body > </html >
CSS 初始化 不同浏览器对有些标签的默认值是不同的,为了消除不同浏览器对HTML文本呈现的差异,照顾浏览器的兼 容,我们需要对CSS 初始化。 简单理解: CSS初始化是指重设浏览器的样式。(也称为CSS reset) 每个网页都必须首先进行 CSS初始化。 这里我们以 京东CSS初始化代码为例。
Unicode编码字体: 把中文字体的名称用相应的Unicode编码来代替,这样就可以有效的避免浏览器解释CSS代码时候出现乱码 的问题。 比如: 黑体 \9ED1\4F53 宋体 \5B8B\4F53 微软雅黑 \5FAE\8F6F\96C5\9ED1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 * { margin : 0 ; padding : 0 }em ,i { font-style : normal }li { list-style : none }img { border : 0 ; vertical-align : middle }button { cursor : pointer }a { color : #666 ; text-decoration : none }a :hover { color : #c81623 }button ,input { font-family : Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53" , sans-serif }body { -webkit-font-smoothing : antialiased; background-color : #fff ; font : 12px /1.5 Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53" , sans-serif; color : #666 }.hide ,.none { display : none }.clearfix :after { visibility : hidden; clear : both; display : block; content : "." ; height : 0 }.clearfix { *zoom: 1 }