前端——CSS(六)——CSS高级技巧

1. 精灵图

1.1 为什么需要精灵图
在这里插入图片描述
一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器会频繁地接收和发送请求图片,造成服务器请求压力过大,这将大大降低页面的加载速度。
因此,为了有效地减少服务器接收和发送请求的次数,提高页面的加载速度,出现了CSS精灵技术(也称CSS Sprites、CSS雪碧)
核心原理: 将网页中的一些小背景图像整合到一张大图中,这样服务器只需要一次请求就可以了。
在这里插入图片描述
1.2 精灵图(Sprites)的使用
使用精灵图核心:

1.精灵图技术主要针对背景图片使用,就是把多个小背景图片整合到一张大图片中。
2.这个大图片也称为sprites精灵图或者雪碧图
3.移动图片位置。此时可以使用background-position
4.移动的距离就是这个目标的x和y坐标,注意网页中的坐标有所不同
5.因为一般情况下都是往上往左移动,所以数值都是负值
6.使用精灵图的时候需要精确测量,每个小背景图片的大小和位置。

<style>
        .box1 {
            width: 60px;
            height: 60px;
            background: url(images/sprites.png) no-repeat -182px 0;
            margin: 100px auto;
            /* background-position: -182px 0; */
        }

        .box2 {
            width: 27px;
            height: 25px;
            margin: 200px;
            background: url(images/sprites.png) no-repeat -155px -106px;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>

2. 字体图标
2.1 字体图标的产生
字体图标使用场景:主要用于显示网页中通用、常用的一些小图标。

精灵图是有诸多优点,但是缺点很明显:
1.图片文件还是比较大
2.图片本身放大和缩小会失真
3.一旦图片制作完毕想要更换非常负责。

此时,有一种技术的出现很好的解决了以上问题,就是字体图标iconfont。
字体图标可以为前端工程师提供一种方便高效的图标使用方式。展示的是图标,本质属于字体。

2.2 字体图标的优点

·轻量级:一个字体图标要比一系列的图像要小,一旦字体加载了,图标就会马上渲染出来,减少了服务器的请求。
·灵活性:本质其实是文字,可以很随意的改变颜色,产生阴影、透明效果、旋转等
·兼容性:几乎支持所有的浏览器,请放心使用

注意:字体图标不能替代精灵技术,只是对工作中图标部分技术的提升和优化

总结:

1.如果遇到一些结构和样式比较简单的小图标,就用字体图标
2.如果遇到一些结构和样式复杂一点的小图片,就用精灵图。

2.3 字体图标的下载

在这里插入图片描述
2.4 字体图标的引入

1.把下载包里面的fonts文件夹放入页面根目录下
在这里插入图片描述
在这里插入图片描述
2.在CSS样式中全局声明字体:简单理解把这些字体文件通过css引入到我们页面中。
一定注意字体文件路径的问题。

 /* 字体声明 */
     @font-face {
  font-family: 'icomoon';
  src:  url('fonts/icomoon.eot?p4ssmb');
  src:  url('fonts/icomoon.eot?p4ssmb#iefix') format('embedded-opentype'),
  url('fonts/icomoon.ttf?p4ssmb') format('truetype'),
  url('fonts/icomoon.woff?p4ssmb') format('woff'),
  url('fonts/icomoon.svg?p4ssmb#icomoon') format('svg');
  font-weight: normal;
  font-style: normal;
  font-display: block;
}
 span {
        font-family: 'icomoon';
        font-size: 100px;
        color: red;
    }

3.html标签内添加小图标。
在这里插入图片描述
2.5 字体图标的追加
如果工作中,原来的字体图标不够用了,我们需要添加新的字体图标到原来的字体文件中。
把压缩包里面的selection.json重新上传,然后选中自己想要的新图标,重新下载压缩包,并替换原来的文件即可。
在这里插入图片描述
3. CSS三角
网页中常见一些三角形,使用CSS直接画出来就可以了,不必做成图片或者字体图标。
在这里插入图片描述

   .box2 {
            width: 0;
            height: 0;
            border: 10px solid transparent;
            border-top-color: pink;
            margin: 100px auto;
        }

4. CSS用户界面
4.1什么是界面样式

所谓界面样式,就是更改一些用户操作样式,以便提高更好地用户体验
·更改用户的图标样式
·表单轮廓
·防止表单域拖拽

4.2 鼠标样式cursor

li{cursor:pointer;}

设置或检索在对象上移动的鼠标指针采用何种系统预定的光标形状
在这里插入图片描述

 <ul>
        <li style="cursor:default;">我是默认的小白鼠标样式</li>
        <li style="cursor: pointer;">我是鼠标小手样式</li>
        <li style="cursor: move;">我是鼠标移动样式</li>
        <li style="cursor: text;">我是鼠标文本样式</li>
        <li style="cursor: not-allowed;">我是鼠标禁止样式</li>
    </ul>

4.3 轮廓线outline

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

input{outline: none;}

4.4 防止拖拽文本域resize
实际开发中,我们文本域右下角是不可以拖拽的。

textarea{resize: none;}
  <style>
        input {
            /* 取消表单轮廓 */
            outline: none;
        }

        textarea {
            resize: none;
            outline: none;
        }
    </style>
</head>

<body>
    <!-- 1.取消表单轮廓 -->
    <input type="text">
    <!-- 2.防止拖拽文本域 -->
    <textarea name="" id="" cols="30" rows="10"></textarea>
</body>

5. Vertical-align属性应用
CSS的vertical-align属性使用场景:经常用于设置图片或者表单(行内块元素)和文字垂直对齐。
官方解释:用于设置一个元素的垂直对齐方式,但是它只针对行内元素或者行内块元素有效。

语法:

vertical-align : baseline | top | middle | bottom;

在这里插入图片描述
在这里插入图片描述

5.1 图片、表单和文字对齐
图片、表单都属于行内块元素,默认的vertical-align是基线对齐。

此时可以给图片、表单这些行内块元素的vertical-align属性设置为middle就可以让文字和图片垂直居中对齐了。

<style>
        img {
            /* vertical-align: bottom; */
            /* 让图片和文字垂直居中 */
            vertical-align: middle;
            /* vertical-align: top; */
        }
    </style>
</head>

<body>
    <img src="images/ldh.jpg" alt="">我是刘德华
</body>

5.2 解决图片底部默认空白缝隙问题

bug:图片底侧会有一个空白缝隙,原因是行内块元素会和文字的基线对齐。
主要解决方法有两种:

1.给图片添加vertical-align:middle|top|bottom;等(提倡使用的)
2.把图片转换为块级元素display:block;

  <style>
        div {
            border: 2px solid red;
        }

        img {
            /* vertical-align: bottom; */
            display: block;
        }
    </style>
</head>

<body>
    <div>
        <img src="images/ldh.jpg" alt="">
    </div>
</body>

6. 溢出的文字省略号显示

6.1 单行文本溢出省略号显示——必须满足三个条件

  /* 1.如果文字显示不开,强制一行内显示,normal是默认自动换行 */
  white-space: nowrap;
   /* 2.溢出的部分隐藏起来 */
   overflow: hidden;
   /* 3.文字溢出的时候用省略号显示 */
   text-overflow: ellipsis;

6.2 多行文本溢出显示省略号

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

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

7. 常见布局技巧

7.1.margin负值运用
在这里插入图片描述
1.让每个盒子margin往左侧移动-1px正好压住相邻盒子边框
2.鼠标经过某个盒子的时候,提高当前盒子的层级即可(如果没有定位,则加相对定位(保留位置),如果有定位,则加z-index)

 <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 {
            /* 2.如果li都有定位,则利用z-index提高层级 */
            z-index: 1;
            border: 1px solid blue;
        }
    </style>

7.2 文字围绕浮动元素
在这里插入图片描述
巧妙运用浮动元素不会压住文字的特性。

<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: 10px;
        }

        .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>

7.3 行内块巧妙运用
在这里插入图片描述

<style>
        .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 {
            width: 45px;
            height: 36px;
            border: 1px solid #ccc;
            outline: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <a href="#" class="prev">&lt;&lt;上一页</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>
        到第
        <input type="text">
        页
        <button>确定</button>
        <a href="#" class="next">&gt;&gt;下一页</a>
    </div>
</body>

7.4 CSS三角强化
在这里插入图片描述

 <style>
        .box1 {
            width: 0;
            height: 0;
            /* border: 10px solid pink; */
            /* 把上面的边框宽度跳大 */
            /* border-top: 100px solid transparent;
            border-right: 50px solid blue; */
            /* 左边和下边的边框宽度设置为0 */
            /* border-bottom: 0 solid yellow;
            border-left: 0px solid purple; */
            /* 1.只保留右边的边框有颜色 */
            border-color: transparent red transparent transparent;
            /* 2.样式都是solid */
            border-style: solid;
            /* 3.上边框要大,右边框宽度稍小,其余边框改为0 */
            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;
        }

        .miaosha 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;
        }

        .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>

8. CSS初始化
在这里插入图片描述

/* 把我们所有标签的内外边距清零 */
* {
    margin: 0;
    padding: 0
}
/* em 和 i 斜体的文字不倾斜 */
em,
i {
    font-style: normal
}
/* 去掉li 的小圆点 */
li {
    list-style: none
}

img {
    /* border 0 照顾低版本浏览器 如果 图片外面包含了链接会有边框的问题 */
    border: 0;
    /* 取消图片底侧有空白缝隙的问题 */
    vertical-align: middle
}

button {
    /* 当我们鼠标经过button 按钮的时候,鼠标变成小手 */
    cursor: pointer
}

a {
    color: #666;
    text-decoration: none
}

a:hover {
    color: #c81623
}

button,
input {
    /* "\5B8B\4F53" 就是宋体的意思 这样浏览器兼容性比较好 */
    font-family: Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53", sans-serif
}

body {
    /* CSS3 抗锯齿形 让文字显示的更加清晰 */
    -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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值