Home>Article>Web Front-end> [Summary] How many of these advanced CSS skills do you know?

[Summary] How many of these advanced CSS skills do you know?

青灯夜游
青灯夜游 forward
2022-11-03 19:51:27 1438browse

This article will take you to play with CSS and share some advanced CSS skills. Come and see if you know it!

[Summary] How many of these advanced CSS skills do you know?

Learning Objectives

  • Be able to use sprites
  • Be able to use font icons
  • Able to writeCSSTriangle
  • Able to write commonCSSUser interface styles
  • Able to describe common layout techniques

Sprites

1. Why do we need sprites?

When the client wants to access a web page, the browser will send a request to the server. After the server receives the request, it will return the request page to the client

A web page Many small background images are often used as decorations. When there are too many images in the web page, the server will frequently receive and send request images, causing excessive server request pressure, which will greatly reduce the loading speed of the page

Therefore, in order toeffectively reduce the number of times the server receives and sends requests and improve the page loading speed,CSSElf technology(also known asCSS Sprites,CSSSprite). [Learning video sharing:css video tutorial,web front-end]

Core principle: Integrate some small background images in the web page into one large image , so that the server only needs one request

2. Use of sprite chart

Use sprite chart core:

  • Elf technology is mainly used for background images, which is to integrate multiplesmall background imagesinto one large image

  • This large image is also calledspritesSprite or Sprite image

  • Move the background image position, you can usebackground-position

  • The moving distance is thexandycoordinates of the target image. Note that the coordinates on the web page are different

  • Because it usually moves up and to the left, the value is a negative value

  • When using sprites, you need to measure accurately. The size of each small background image and Location

On the official website of King of Glory, we can find such a sprite map

[Summary] How many of these advanced CSS skills do you know?

How to display this sprite map What about the specific part

      精灵图的使用  

Run result:

[Summary] How many of these advanced CSS skills do you know?

##3. Sprite chart application case

The material is here

[Summary] How many of these advanced CSS skills do you know?##Code implementation:

      利用精灵图拼出自己的名字  

Running result:

[Summary] How many of these advanced CSS skills do you know?How about it, go and try it out

Font icon

1. Generation of font iconThe circles in the picture below are font icons, not pictures.

[Summary] How many of these advanced CSS skills do you know?Usage scenarios of font icons: mainly used to display common and commonly used in web pages Some small icons

Sprite pictures have many advantages, but the shortcomings are also obvious

    The image file takes up a relatively large space
  • The picture itself will be distorted when zoomed in and out
  • Once the picture is made, it is very complicated to replace it
  • So, a technology The font icon appears.
iconfont

The font icon can provide front-end engineers with a convenient and efficient way to use icons.

displays icons, which essentially belong to the font

2. Advantages of font icons

Lightweight: A font icon is smaller than a series of images. Once the font Once loaded, the icon will be rendered immediately, reducing server requests
  • Flexibility: The essence is actually text, and you can change the color, generate shadows, transparency effects, rotate, etc.
  • Compatibility: Supports almost all browsers, please feel free to use it
  • Note: Font icons cannot replace elf technology, they are just an improvement and optimization of the icon technology at work

Summary:

  • 如果遇到一些结构和样式比较简单的小图标,就用字体图标

  • 如果遇到一些结构和样式复杂一点的小图片,就用精灵图

3. 下载字体图标

字体图标是一些网页常见的小图标,我们直接从网上下载即可,使用可以分为:

  • 字体图标的下载

  • 字体图标的引入(引入到我们html页面中)

  • 字体图标的追加(以后添加新的小图标)

推荐的下载网站:

  • icomoon 字库
    IcoMoon成立于2011年,推出了第一个自定义图标字体生成器,它允许用户选择所需要的图标,使它们呈一字型。该字库内容种类繁多,非常全面,唯一的遗憾是国外服务器,打开网速较慢
  • iconfont 字库
    这个是阿里巴巴M2UX的一个iconfont字体图标字库,包含了淘宝图标库和阿里巴巴图标库,可以使用AI制作图表上传生成,重点是 —— 免费

icomoon 字库来说,基本操作如下

[Summary] How many of these advanced CSS skills do you know?

[Summary] How many of these advanced CSS skills do you know?

4. 引入字体图标

下载完毕之后,注意原先的文件不要删,后面会用

  • 把下载包里面的fonts文件夹放入页面根目录下
    [Summary] How many of these advanced CSS skills do you know?
  • CSS样式中全局声明字体:简单理解为将这些字体文件通过css引入到我们页面中
    一定要注意文件路径的问题

[Summary] How many of these advanced CSS skills do you know?
我们用下面这一系列代码来声明字体,这段代码也可以在style.css中找到

@font-face { font-family: 'icomoon'; src: url('fonts/icomoon.eot?q42srk'); src: url('fonts/icomoon.eot?q42srk#iefix') format('embedded-opentype'), url('fonts/icomoon.ttf?q42srk') format('truetype'), url('fonts/icomoon.woff?q42srk') format('woff'), url('fonts/icomoon.svg?q42srk#icomoon') format('svg'); font-weight: normal; font-style: normal; font-display: block; }

打开demo.html我们可以复制下载的图标

[Summary] How many of these advanced CSS skills do you know?

[Summary] How many of these advanced CSS skills do you know?

      字体图标的使用  

[Summary] How many of these advanced CSS skills do you know?

5. 字体图标的追加

如果工作中,原来的字体图标不够用了,我们需要添加新的字体图标到原来的字体文件

把压缩包里面的selection.json重新上传,然后选中自己想要新的图标,重新下载压缩包,并替换原来的文件即可

[Summary] How many of these advanced CSS skills do you know?

CSS 三角

1. CSS三角制作

网页中常见一些三角形,使用CSS直接画出来就可以,不必做成图片或者字体图标

      CSS三角制作  

[Summary] How many of these advanced CSS skills do you know?

2. CSS三角应用-京东效果

      CSS三角应用  

[Summary] How many of these advanced CSS skills do you know?

CSS 用户界面样式

1. 什么是界面样式

所谓的界面样式,就是更改一些用户操作样式,以便提供更好的用户体验

  • 更改用户的鼠标样式
  • 表单轮廓
  • 防止表单域拖拽

2. 更换用户鼠标样式

li { cursor: pointer; }

设置或检索在对象上移动的鼠标指针采用何种系统预定义的光标形状

属性值 描述
default 小白,默认
pointer 小手
move 移动
text 文本
not-allowed 禁止
      鼠标样式 
  • 我是鼠标默认样式
  • 我是鼠标小手样式
  • 我是鼠标移动样式
  • 我是鼠标文本样式
  • 我是鼠标禁止样式

3. 表单轮廓线

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

4. 防止表单域拖拽

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

给文本域添加resize: none;样式之后,就可以防止被拖拽

      去掉表单轮廓线和防止文本域拖拽  

[Summary] How many of these advanced CSS skills do you know?

vertical-align 属性应用

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

功能:用于设置一个元素的垂直对齐方式,但是它只针对于行内元素或者行内块元素有效

语法:

vertical-align: baseline | top | middle | bottom
描述
baseline 默认,元素放置在父元素的基线上
top 把元素的顶端于行中最高元素的顶端对齐
middle 把此元素放置在父元素的中部
bottom 把元素的顶端与行中最低的元素的顶端对齐
      vertical-align  
一生之敌

[Summary] How many of these advanced CSS skills do you know?

1. 图片、表单和文字对齐

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

[Summary] How many of these advanced CSS skills do you know?

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

[Summary] How many of these advanced CSS skills do you know?

2. 解决图片底部默认白色间隙问题

bug:图片底侧会有一个空白间隙,原因是行内块元素会和文字的基线对齐

主要解决方法有两种:

  • 给图片添加vertical-align: middle | top | bottom;等 (推荐)

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

      解决图片底部空白间隙  

[Summary] How many of these advanced CSS skills do you know?

溢出的文字省略号显示

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

/* 1.文字显示不开时强制显示 */ white-space: nowrap; /* 2. 超出部分隐藏 */ overflow: hidden; /* 3. 文字用省略号替代超出的部分 */ text-overflow: ellipsis;
      单行文字溢出  
衣带渐宽终不悔,为伊消得人憔悴

[Summary] How many of these advanced CSS skills do you know?

2. 多行文本溢出显示省略号

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

/* 弹性伸缩盒子模型显示 */ display: -webkit-box; /* 限制在一个块元素显示的文本的行数 */ -webkit-line-clamp: 3; /* 设置或检索伸缩盒子对象的子元素的排列方式 */ -webkit-box-orient: vertical;
      多行文字溢出  
衣带渐宽终不悔,为伊消得人憔悴.衣带渐宽终不悔,为伊消得人憔悴

[Summary] How many of these advanced CSS skills do you know?

更推荐让后台人员来做这个效果,因为后台人员可以设置显示多少个字,操作更简单

常见布局技巧

1. margin负值的运用一

让每个盒子margin往左侧移动-1px正好压住相邻盒子边框

      margin负值的巧妙运用  
  • 1
  • 2
  • 3
  • 4
  • 5

[Summary] How many of these advanced CSS skills do you know?

2. margin负值的运用二

鼠标经过某个盒子的时候,提高当前盒子的层级即可(如果没有定位,则加相对定位(保留位置),如果有定位,则加z-index

      margin负值的巧妙运用  
  • 1
  • 2
  • 3
  • 4
  • 5

[Summary] How many of these advanced CSS skills do you know?

3. 文字围绕浮动元素

类似于下图这样的效果,该怎么实现呢?

[Summary] How many of these advanced CSS skills do you know?

      文字围绕浮动元素  

河南一景区爆火,风景优美物价低,吸引许多游客前来打卡

[Summary] How many of these advanced CSS skills do you know?

4. 行内块的巧妙运用

      行内块的巧妙运用  
<<上一页 2 3 4 5 6 ... >>下一页 到第

[Summary] How many of these advanced CSS skills do you know?

5. CSS三角强化

      CSS三角巧妙运用  
¥9.99 ¥9999

效果图:

[Summary] How many of these advanced CSS skills do you know?

CSS初始化

不同浏览器对有些标签的默认值是不同的,为了消除不同浏览器对HTML文本呈现的差异,照顾浏览器的兼容,我们需要对CSS进行初始化

简单理解:CSS初始化是指重设浏览器的样式,也称为CSS reset

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

这里我们以京东CSS初始化代码为例,打开京东商城源代码,找到其CSS代码

[Summary] How many of these advanced CSS skills do you know?

再找到初始化代码

[Summary] How many of these advanced CSS skills do you know?

我们来看一下它都做了些什么工作

/* 把所有标签的内外边距清零 */ * { 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 { /* 抗锯齿性,让文字显示得更加清晰 */ -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 }

Unicode 编码字体:

把中文字体的名称用相应的Unicode编码来代替,这样就可以有效地避免浏览器解释CSS代码时候出现乱码

比如:

  • 黑体\9ED1\4F53
  • 宋体\5B8B\4F53
  • 微软雅黑\5FAF\8F6F\96C5\9ED1

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of [Summary] How many of these advanced CSS skills do you know?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete