1# 基础知识
2
3
4Svg组件主要作为svg画布的根节点使用,也可以在svg中嵌套使用。具体用法请参考[Svg](../reference/apis-arkui/arkui-js/js-components-svg.md)。
5
6
7> **说明:**
8>
9> svg父组件或者svg组件需要定义宽高值,否则不进行绘制。
10
11
12## 创建Svg组件
13
14pages/index目录下的hml文件中创建一个Svg组件。
15
16
17```html
18<!-- xxx.hml -->
19<div class="container">
20  <svg width="400" height="400">  </svg>
21</div>
22```
23
24
25```css
26/* xxx.css */
27.container{
28  width: 100%;
29  height: 100%;
30  flex-direction: column;
31  align-items: center;
32  justify-content: center;
33  background-color: #F1F3F5;
34}
35svg{
36  background-color: blue;
37}
38```
39
40![zh-cn_image_0000001218280036](figures/zh-cn_image_0000001218280036.png)
41
42
43## 设置属性
44
45通过设置width、height、x、y和viewBox属性为Svg设置宽度、高度、x轴坐标、y轴坐标和Svg视口。
46
47
48```html
49<!-- xxx.hml -->
50<div class="container">
51  <svg width="400" height="400" viewBox="0 0 100 100">
52    <svg class="rect" width="100" height="100" x="20" y="10">
53    </svg>
54  </svg>
55</div>
56```
57
58
59```css
60/* xxx.css */
61.container{
62  width: 100%;
63  height: 100%;
64  flex-direction: column;
65  align-items: center;
66  justify-content: center;
67  background-color: #F1F3F5;
68}
69svg{
70  background-color: yellow;
71}
72.rect{
73  background-color: red;
74}
75```
76
77![zh-cn_image_0000001218599996](figures/zh-cn_image_0000001218599996.png)
78
79> **说明:**
80> - x和y设置的是当前Svg的x轴和y轴坐标,如果当前Svg为根节点,x轴和y轴属性无效。
81>
82> - viewBox的宽高和svg的宽高不一致,会以中心对齐进行缩放。