Font字体小图标应用_html/css_WEB-ITnose
我们知道在页面上载入小图标有多种方式,比如我们使用比较多的是css sprite背景图片定位,就是将很多小图标集成到一张大图,然后使用CSS定位背景。还有一种比较现代的方式是使用font字体图标,它利用@font-face原理载入图标字体库,然后调用图标。
查看演示 下载源码
使用css sprite方式非常方便,可以有很多色彩缤纷的图标,而且兼容性好,但是缺点就是图标不能缩放,或者说缩放了会失真,另外维护也不方便,修改一个图标要对整张大图就行修改,并且定位像素需要借助PS等工具确定位置。而使用图标字体库越来越受开发者青睐,它的维护简单,而且对图标缩放自由,随时都可以展示高清的图标,可以添加阴影效果和动画效果,设置也非常方便,唯一差点的就是图标颜色比较单一,就是设置的时候只能设置一个图标为单一颜色。下面我们来看下如何使用font字体小图标。
HTML
目前市面上有很多字体图标库,有收费的也有免费使用的。常见的有 icomoon, Font-Awesome以及 Glyphicon Halflings,Glyphicon在bootstrap下使用时免费的。当然国内 阿里系也开放了自己的图标库。
本文讲解下使用icomoon字体库,我们可以在官网上选择喜欢的字体图标然后下载并部署到项目中,当然也可以自己制作图标然后上传到官网上。下载好的字体库,在一个fonts/的文件夹里。我们先布置好html结构:
<ul class="icons"> <li class="sp"><i class="icon-spades spades"></i> 黑桃K</li> <li class="red"><i class="icon-file-picture"></i> 照片</li> <li><i class="icon-tablet"></i> iPad</li> <li><a href="#"><i class="icon-bubbles"></i> 评论</a></li></ul>
我们在上面的html代码中,给元素li里添加了,然后加上class属性,class值就对应了css文件里的字体图标样式了。
CSS
首先我们使用@font-face命名字体名称,以及加载的字体文件,需要加载多个字体文件以兼容各浏览器和系统。然后定义class含有icon的样式,注意其中使用了抗锯齿处理,就是图片放大的时候不会出现边缘锯齿现象,然后使用伪类,设置content即图标内容。
@font-face { font-family: 'icomoon'; src: url('../fonts/icomoon.eot?42vx6u'); /* IE9 兼容模式 */ src: url('../fonts/icomoon.eot?42vx6u#iefix') format('embedded-opentype'), url('../fonts/icomoon.ttf?42vx6u') format('truetype'), url('../fonts/icomoon.woff?42vx6u') format('woff'), url('../fonts/icomoon.svg?42vx6u#icomoon') format('svg'); font-weight: normal; font-style: normal;}[class^="icon-"], [class*=" icon-"] { font-family: 'icomoon' !important; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; line-height: 1; /* 抗锯齿处理 =========== */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}.icon-spades:before { content: "\e900";}.icon-file-picture:before { content: "\e901";}.icon-tablet:before { content: "\e902";}.icon-bubbles:before { content: "\e903";}
然后,我们运行下html页面,是不是可以看到几个图标了,方便实用吧。
如果想给图标定义动画效果,可以使用css来实现。
.spinner{ -webkit-animation: spinner 1s infinite linear; animation: spinner 1s infinite linear;}@-webkit-keyframes spinner{ 0%{ -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100%{ -webkit-transform: rotate(359deg); transform: rotate(359deg); }}
以上代码定义了图标旋转动画,我们可以结合js实现这样的效果:当鼠标滑上图标时展示图标旋转动画,当鼠标离开时,停止旋转动画。这个效果的代码大家可以参照demo中的源码。demo中我还加上了Font Awesome字体图标示例和Glyphicons字体图标示例,大家可以查看demo或下载源码了解。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...
