整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

SVG 在前端的7种使用方法,你还知道哪几种?

SVG 在前端的7种使用方法,你还知道哪几种?

文简介

点赞 + 关注 + 收藏=学会了


技术一直在演变,在网页中使用 SVG 的方法也层出不穷。每个时期都有对应的最优解。

所以我打算把我知道的 7种 SVG 的使用方法列举出来,有备无患~

如果你还知道其他方法,可以在评论区补充~



1. 在浏览器直接打开

<?xml version="1.0" ?>

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <title>雷猴</title>
  <circle cx="50" cy="50" r="50" fill="hotpink"></circle>
</svg>

xml 是浏览器能读取的格式,但如果希望 svg 能在浏览器中渲染出来,需要使用 xmlns 声明渲染规则。

所以必须使用 xmlns="http://www.w3.org/2000/svg"。



2. 内嵌到 HTML 中(推荐???)

<!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>svg demo</title>
</head>
<body>
  <div>
    <!-- 内嵌到 HTML 中 -->
    <svg width="100%" height="100%" version="1.1">
      <circle cx="50" cy="50" r="50" fill="hotpink"></circle>
    </svg>
  </div>
</body>
</html>

可以看到上面的代码中,<svg> 标签并没有使用 xmlns="http://www.w3.org/2000/svg" 声明命名空间,这是因为 HTML 5 文档使用 <!DOCTYPE html> 标记,它允许跳过 SVG 命名空间声明,HTML 解析器会自动识别 SVG 元素和它的子元素,除了 <foreignObject> 元素的子元素。


在写本文时,将 SVG 内嵌到 HTML 中 的做法是最常见的,也是比较推荐的方式之一。

做特效时,这种使用方式也是比较轻松的。



3. CSS 背景图(推荐?)

<style>
.svg_bg_img {
  width: 100px;
  height: 100px;
  background: url('./case1.svg') no-repeat;
  background-size: 100px 100px;
}
</style>

<div class="svg_bg_img"></div>

SVG 也是一种图片格式,所以按理说是能当做背景图来使用的。

一试,果然可以~



4. 使用 img 标签引入(推荐?)

<img src="./case1.svg" width="100" height="100">

既然 SVG 可以在 CSS 中当背景图使用,那也可以在 <img> 标签里使用咯~



5. 使用 iframe 标签引入(不推荐?)

<iframe
  src="./case1.svg"
  width="100"
  height="100"
></iframe>

iframe 可以在网页里再嵌套一个网页,既然 SVG 可以直接在浏览器打开,那使用 <iframe> 引用 SVG 同样也是可以的。

需要注意的是,<iframe> 默认是有个边框样式的,如果你使用这种方式引入 SVG 可能还需要自己手动调节一下样式。



6. 使用 embed 标签引入(不推荐?)

<embed
  src="./case1.svg"
  width="100"
  height="100"
/>

<embed> 标签定义了一个容器,用来嵌入外部应用或者互动程序。它也可以插入各种媒体资源。

<embed> 标签已经被标准采用了。但它不能包含任何子内容,因此如果嵌入失败就没有备用选项了。所以现阶段来看,我不太推荐使用 embed 的方式引入 SVG 。



7. 使用 object 标签引入(不推荐?)

<object
  data="./case1.svg"
  type="image/svg+xml"
  codebase="http://www.adobe.com/svg/viewer/install"
></object>

<object> 是通过 data 属性引入资源的。它可以用于嵌入图像,类似 <img> ,也可以用于嵌入独立的 HTML 文档,类似 <iframe> 。

使用 <object> 嵌入 SVG 可以让那些不能直接显示 SVG 但又有 SVG 插件的老旧浏览器展示 SVG。

需要注意的是,在某些现代浏览器中,type 和 codebase 是可以不写的。

type 用来声明当前引入的资源是属于什么类型。



总结

在写本时,我推荐使用 内嵌到 HTML 的方式来做日常开发。

其他方式按照你实际需求去使用即可。

最后的 embed 和 object 这两种方式可以当做备用方案去使用。



代码仓库

?雷猴 SVG



推荐阅读

《Canvas 从入门到劝朋友放弃(图解版)》

《Fabric.js 从入门到膨胀》

《『Three.js』起飞!》

《console.log也能插图!!!》

《纯css实现117个Loading效果》

《视差特效的原理和实现方法》

《这18个网站能让你的页面背景炫酷起来》

绘图 Drawing Shapes

SVG stands for Scalable Vector Graphics, and is used to draw shapes with HTML-style markup.

It offers several methods for drawing paths, boxes, circles, text, and graphic images.

SVG is not pixel-based, so it can be magnified infinitely with no loss of quality.

SVG代表可伸缩矢量图形,用于绘制具有HTML风格标记的形状。

它提供了几种绘制路径,框,圆,文本和图形图像的方法。

SVG不是基于像素的,所以它可以无限放大,没有质量损失。

2 插入SVG图像 Inserting SVG Images

An SVG image can be added to HTML code with just a basic image tag that includes a source attribute pointing to the image:

只需一个基本的图像标签

3 绘制

To draw shapes with SVG, you first need to create an SVGelement tag with two attributes: width and height.

要使用SVG绘制形状,您首先需要创建一个SVG元素标签,其中包含两个属性:width和height。

To create a circle, add a <circle> tag:

- cxpushes the center of the circle further to the right of the screen- cypushes the center of the circle further down from the top of the screen- r defines the radius- filldetermines the color of our circle- strokeadds an outline to the circle

天给大家推荐一款非常值得一用的开源矢量图标库CssGG

css.gg 又一款供设计和前端开发的工具图标库,star高达6.5K+。拥有超过700+个矢量图标,可被用于.css|.svg|.tsx|.fig|.xd|.json|.xml等多种格式。

作为前端开发,平时需要使用图标都会去阿里iconfont图标库查找。

今天就多了一种新的选择css.gg。只要是优秀的,我们就有理由去使用它。

安装

$ npm i css.gg -S

包含以下目录及文件。

快速使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <!-- You can add this link or any other CDN alternatives as mentioned above -->
    <link href='https://css.gg/css' rel='stylesheet'>

</head>
<body>

    <!-- Using i tag  -->
    <i class="gg- {ICONNAME} "></i>

    <!-- Using div tag  -->
    <div class="gg- {ICONNAME} "></div>

    <!-- Using custom tag  -->
    <gg-icon class="gg- {ICONNAME} "></gg-icon>

</body>
</html>

另外还可 CSS @import引入,SVG、JSON、XML、React等其它方式。

CSS、SVG、Figma UI Icons 可用在 SVG雪碧图,styled-components等。

so perfect,非常不错的一款开源图标库。

# 官网地址
https://css.gg/

# 仓库地址
https://github.com/astrit/css.gg

ok,今天就介绍到这里。如果小伙伴们有兴趣的话,可以去试一试哈!