大家好,又见面了,我是你们的朋友全栈君。
今天学习css精灵图技术,并且通过用它拼接自己的英文名字,拿起小本本记好了哦!
我们常常在网页中可以看到一些小图标,我们都知道这种小图标要么是图片做的,要么就是字体图标。举个例子,我们看王者荣耀官网:
这里的“下载游戏”按理说应该是一张单一的图,但是我们却看到他的background里的图片一大块里有很多其他小图标,很容易想到他把这个网站用到的一些图标都放到这一张图片里呢,这就是精灵图,包括我们常学习的学习通网站,经过我以前的分析,也发现了精灵图的影子,由此可见精灵图技术在网页中十分常见。
一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器就会频繁地接收和发送请求图片,造成服务器请求压力过大,这将大大降低页面的加载速度。 因此,为了有效地减少服务器接收和发送请求的次数,提高页面的加载速度,出现了 CSS 精灵技术(也称 CSS Sprites 、 CSS 雪碧)。 核心原理:将网页中的一些小背景图像整合到一张大图中,这样服务器只需要一次请求就可以了,请求一次,这张图片就会被缓存到浏览器本地,下次就不用在发送请求了。
我们把所有需要的图标都放到一张图片中,那么我们怎么把相应的小图标布局到对应的地方呢?
当给我们有大小的盒子添加背景图片后,默认是图片的左上角内容作为盒子背景,当我们需要将特定背景放到盒子里时,就需要 background – position属性来移动背景图,使得特定图标显示到特定位置。
使用精灵图核心:
至于移动的像素的大小可以用工具量出来,电脑自带的画图也能量。
用到的图片:123.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> /* 顶部装饰及其位置 */ .top{
width: 730px; height: 550px; margin: 10px auto; border: 1px dotted pink; border-radius: 20%; background: url("123.png") no-repeat -30px -50px ; } .demo{
width: 320px; height: 80px; margin: 0 auto; border-bottom: 5px dashed skyblue; } .demo div{
float: left; width:70px; height: 80px; background: url("123.png") no-repeat ; } /* 字母A的位置 */ .demo .demo1{
background-position: -35px -692px; margin-right: 40px; } /* 字母N的位置 */ .demo .demo2{
background-position: -352px -826px; } /* 字母I的位置 */ .demo .demo3{
background-position: -685px -692px; } /* 字母U的位置 */ .demo .demo4{
background-position: -196px -953px; } span{
position: absolute ; right:35%; bottom: 2%; font-size: large; color: orangered; } </style>
</head>
<body>
<div class="top"></div>
<div class="demo">
<div class="demo1"></div>
<div class="demo2"></div>
<div class="demo3"></div>
<div class="demo4"></div>
</div>
<span>阿牛</span>
</body>
</html>
喜欢就给个三连吧!!!我们一起加油!!!有问题评论区留言或者私信博主,我会一一回的!!!💖 💖 💖
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140966.html原文链接:https://javaforall.cn