当前位置: 首页 > news >正文

CSS高级技巧---精灵图、字体图标、布局技巧

1、精灵图

1)原理

        核心原理:将网页中的一些小背景图像整合到一张大图中,这样服务器只需要一次请求就可以了。

        精灵技术的目的:为了有效减少服务器接收和发送请求的次数,提高页面的加载速度。

2)使用

使用精灵图核心:

1.精灵技术主要针对于背景图片使用。就是把多个小背景图片整合到一张大图中。

2.这个大图片也称为sprites精灵图或者雪碧图

3.移动背景图片位置,此时可以使用background-position

4.移动的距离就是这个目标图片的x和y坐标。注意网页中的左边有所不同。

5.一般情况下往上往左移动,数值是负值。(一般情况下,都是负值)

6.使用精灵图的时候需要精确测量,每个小背景图片的大小和位置。

    <style>.box1 {width: 60px;height: 60px;margin: 100px auto;background: url(images/sprites.png) no-repeat;background-position: -182px 0;}.box2 {width: 27px;height: 25px;margin: 200px;background: url(images/sprites.png) no-repeat;background-position: -155px -106px;}</style>
<body><div class="box1"></div><div class="box2"></div>
</body>

2、字体图标

        字体图标展示的是图标,但是本质上是字体,具有字体的属性。

字体图标优点:

字体图标下载

下载网站:

icomoon字库 http://icomoon.io

阿里iconfont字库 http://www.iconfont.cn/

阿里的字体图标使用方法:

https://www.iconfont.cn/help/detail?spm=a313x.7781069.1998910419.d8cf4382a&helptype=code

3、CSS三角

由下面代码可知,制作一个三角可以让其他三个三角是透明的即可。

<style>.box1 {width: 0;height: 0;border-top: 10px solid skyblue;border-right: 10px solid rgb(92, 221, 116);border-bottom: 10px solid rgb(226, 235, 148);border-left: 10px solid rgb(245, 155, 210);}</style>
<body><div class="box1"></div>
</body>

    <style>.box2 {width: 0;height: 0;/* 为了照顾兼容性 */line-height: 0;font-size: 0;border: 10px solid transparent;border-top-color: skyblue;}</style>
<body><div class="box2"></div>
</body>

3、用户界面样式

1)更改用户的鼠标样式

语法:li { cursor: pointer; }

2)表单轮廓线

        给表单添加outline:0;或者outline:none;样式之后,就可以去掉默认的蓝色边框。

3)防止表单域拖拽

语法:textarea{ resize: none; }

4、vertical-align属性应用

1)对齐方式

        vertical-align属性应用场景:用于设置图片或者表单(行内块元素)和文字垂直对齐

但是它只针对于行内元素或者行内块元素有效。

语法(默认和基线对齐):

vertical-align : baseline | top | middle | bottom

2)图片底侧空白缝隙问题

        由于图片默认和文字是基线对齐,所以图片底侧会出现空白缝隙。

解决方法:

1.给图片添加vertical-align :  top | middle | bottom

2.把图片转换为块级元素display:block;

5、溢出的文字省略号显示

1)单行文本溢出显示省略号

必须满足三步:

1.先强制一行内显示文本

nowrap不换行,默认normal自动换行

white-space: nowrap;

2.超出部分隐藏

overflow: hidden;

3.文字用省略号替代超出的部分

     text-overflow: ellipsis;
2)多行文本溢出显示省略号

        多行文本溢出显示省略号,有较大兼容性问题, 适合于webKit浏览器或移动端(移动端大部分是webkit内 核)。

overflow: hidden; 
text-overflow: ellipsis; 
/* 弹性伸缩盒子模型显示 */ 
display: -webkit-box; 
/* 限制在一个块元素显示的文本的行数 */ 
-webkit-line-clamp: 2; 
/* 设置或检索伸缩盒对象的子元素的排列方式 */ 
-webkit-box-orient: vertical;

6、常见布局技巧

1)margin负值的运用
    <style>ul li {float: left;list-style: none;width: 150px;height: 200px;border: 1px solid skyblue;margin-left: -1px;}ul li:hover {border: 1px solid red;}</style>
<body><ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
</body>

由此,我们发现第四个盒子的右边框被压到了,此时只需要当前的盒子添加position: relative;因为相对定位会压住其他标准流的盒子。

    <style>ul li {float: left;list-style: none;width: 150px;height: 200px;border: 1px solid skyblue;margin-left: -1px;}ul li:hover {position: relative;border: 1px solid red;}</style>

注意:如果盒子都有定位,则加z-index。

2)文字围绕浮动元素

        ---运用浮动元素不会压住文字的技巧

    <style>* {margin: 0;padding: 0;}.box {float: left;width: 250px;height: 100px;background-color: skyblue;margin: 0 auto;padding: 5px}.photo {float: left;width: 180px;height: 80px;margin-right: 5px;}.photo img {width: 100%;}</style>
<body><div class="box"><div class="photo"><img src="images/jianlai.jpg" alt=""></div><p>岁岁平,岁岁安,岁岁平安</p></div>
</body>

3)行内块巧妙运用
<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;line-height: 36px;text-decoration: none;color: #333;font-size: 14px;}.box .prev,.box .next {width: 85px;}.box .now,.box .will {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>
<body><div class="box"><a href="#" class="prev">&lt;&lt;上一页</a><a href="#" class="now">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">6</a><a href="#" class="will">...</a><a href="#" class="next">>>下一页</a>到第<input type="text">页<button>确定</button></div>
</body>

4)CSS三角强化
    <style>* {margin: 0;padding: 0;}.box1 {width: 0;height: 0;/* border-top: 20px solid transparent;border-right: 10px solid skyblue;border-bottom: 0px solid rgb(226, 235, 148);border-left: 0px solid rgb(245, 155, 210); */border-color: transparent skyblue transparent transparent;border-style: solid;border-width: 20px 10px 0 0;}</style>

<style>* {margin: 0;padding: 0;}.box1 {margin: 30px 0;}.price {width: 160px;height: 24px;line-height: 24px;border: 1px solid red;margin: 0 auto;color: #fff;font-weight: 700px;}.ms {position: relative;float: left;width: 90px;height: 100%;background-color: red;text-align: center;}.ms i {position: absolute;right: 0;top: 0;width: 0;height: 0;border-color: transparent #fff transparent transparent;border-style: solid;border-width: 24px 12px 0 0;}.ori {font-size: 12px;color: gray;text-decoration: line-through;}</style>
<body><div class="box1"></div><div class="price"><span class="ms">¥1650<i></i></span><span class="ori">¥5650</span></div>
</body>

7、CSS初始化

每个网页都必须首先进行CSS初始化。


文章转载自:

http://MV6GK2AZ.sxfmg.cn
http://2eaNcdzF.sxfmg.cn
http://xkGXP5tL.sxfmg.cn
http://O8MjsASt.sxfmg.cn
http://2dCBDVP7.sxfmg.cn
http://GTA8jNrw.sxfmg.cn
http://jIQPiJe3.sxfmg.cn
http://d1wrueSp.sxfmg.cn
http://FH0TZHP7.sxfmg.cn
http://FB4XEcaF.sxfmg.cn
http://i3XlB6aW.sxfmg.cn
http://NTld5U0m.sxfmg.cn
http://sXvf1bZI.sxfmg.cn
http://U8FGM56N.sxfmg.cn
http://R040Mupv.sxfmg.cn
http://4leo2ayx.sxfmg.cn
http://xH7t2Apj.sxfmg.cn
http://tAU1GFv0.sxfmg.cn
http://w3jboSA4.sxfmg.cn
http://2BWgzx2s.sxfmg.cn
http://Fc7J9Nwb.sxfmg.cn
http://XgvQNr48.sxfmg.cn
http://nldvGk3p.sxfmg.cn
http://euYEOBgX.sxfmg.cn
http://H9hIt00w.sxfmg.cn
http://36Dz1Orm.sxfmg.cn
http://EvQ3Jzan.sxfmg.cn
http://3yNLLA33.sxfmg.cn
http://iBNZOMLE.sxfmg.cn
http://2KOniVyD.sxfmg.cn
http://www.dtcms.com/a/382491.html

相关文章:

  • 克服 MongoDB C# 驱动程序的局限性
  • 详解MySQL JSON字段索引设置方案
  • 从基础到实践(四十五):车载显示屏LCD、OLED、Mini-LED、MicroLED的工作原理、设计差异等说明
  • 汽车座椅固定装置及头枕强度动静态试验系统
  • 机器学习-过拟合和欠拟合
  • win11business和consumer版本有什么区别
  • 一个支持多平台的 AI 客户端
  • 浏览器调试工具详解
  • <测量透明液体折射率的深度学习方法>论文总结
  • 下载 MNIST 数据集方法 mnist_train.csv 和 mnist_test.csv
  • 【完整源码+数据集+部署教程】足球场景分割系统: yolov8-seg-C2f-EMBC
  • 算法 --- 链表
  • 技术演进中的开发沉思-99 Linux服务编程系列:程序员视角下的 TCP/IP 通信案例
  • 面试(二)
  • 计算机技术在国有企业档案信息化建设的应用
  • 2025.9.11英语红宝书【必背1-5】
  • Python核心语法篇【1】:环境安装配置与第一个“Hello World”程序
  • 【C++练习】18.C++求两个整数的最小公倍数(LCM)
  • yolo识别手势释放忍术
  • Amass 被动与主动子域收集
  • 【左程云算法08】栈和队列相互实现
  • RocketMQ详解,消息队列实战
  • 4.6 我国股票的类型(43)
  • ​​抢占储能新高地:汇川DSP驱动软件开发范式变革与人才重塑​
  • tree 遍历目录
  • 不邻排列:如何优雅地避开“数字CP“
  • Vue3应用执行流程详解
  • css 高度从 0 到 auto 的动画效果 `interpolate-size: allow-keywords`
  • 8-获取文件和目录信息
  • SPAR类比推理模型学习(与常见小目标检测方法总结)