要在 HTML 中嵌入 SVG,可以使用 <svg>
标签。在 <svg>
标签中,使用 <path>
、<circle>
、<rect>
等元素来定义 SVG 图形。以下是具体步骤:
<svg>
标签来定义 SVG 图形。例如:<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
</svg>
上述代码定义了一个半径为 40、圆心坐标为 (50,50)、边框为黑色、填充为红色的圆形。
<svg>
标签中,使用 <path>
、<circle>
、<rect>
等元素来定义 SVG 图形。例如:<svg width="200" height="200">
<rect x="50" y="50" width="100" height="100" stroke="black" stroke-width="2" fill="none" />
<path d="M 50 50 L 150 150 M 150 50 L 50 150" stroke="black" stroke-width="2" fill="none" />
</svg>
上述代码定义了一个边框为黑色、填充为透明、宽度为 100、高度为 100 的矩形,以及两条对角线。
<style>
circle {
fill: red;
stroke: black;
stroke-width: 2px;
}
</style>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" />
</svg>
上述代码使用 CSS 来设置圆形的填充、边框颜色和宽度。
需要注意的是,在 SVG 中,坐标系的原点在左上角,x 轴向右延伸,y 轴向下延伸。此外,SVG 支持很多图形元素和属性,具体可以参考相关文档。