SVG(Scalable Vector Graphics,缩放矢量图形)是一种用于描述二维矢量图形的 XML 格式。HTML5 支持内联使用 SVG 图像,使得 SVG 可以直接嵌入到 HTML 页面中,而不需要引用外部文件。这为网页设计和开发带来了更大的灵活性,允许开发者直接操作图形,并且在响应式布局中表现优秀。
内联 SVG 是指将 SVG 代码直接嵌入到 HTML 文档中,而不是通过 <img>
标签加载外部的 SVG 文件。
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>php151 Bytes© 菜鸟-创作你的创作
<svg>
:定义一个 SVG 图形容器。width
和 height
:指定 SVG 的宽度和高度。xmlns
:声明 XML 命名空间,这是必需的属性。<circle>
:定义一个圆形。cx
和 cy
:圆心的坐标。r
:圆的半径。stroke
:圆的边框颜色。stroke-width
:边框的宽度。fill
:圆的填充颜色。<rect>
)<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <rect x="50" y="50" width="100" height="100" fill="blue" /></svg>php134 Bytes© 菜鸟-创作你的创作
<rect>
:绘制矩形。x
和 y
:矩形左上角的坐标。width
和 height
:矩形的宽度和高度。fill
:填充颜色。<line>
)<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="0" x2="200" y2="200" stroke="green" stroke-width="2" /></svg>php147 Bytes© 菜鸟-创作你的创作
<line>
:绘制直线。x1
和 y1
:直线起点坐标。x2
和 y2
:直线终点坐标。stroke
:线条的颜色。stroke-width
:线条宽度。<polygon>
)<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <polygon points="100,10 40,180 190,60 10,60 160,180" fill="purple" /></svg>php144 Bytes© 菜鸟-创作你的创作
<polygon>
:绘制多边形。points
:定义多边形的各个顶点坐标,坐标值用空格分隔。<path>
)<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <path d="M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80" stroke="black" stroke-width="2" fill="transparent" /></svg>php185 Bytes© 菜鸟-创作你的创作
<path>
:绘制任意形状的路径。d
:定义路径的绘制命令(如移动到某个点 M
,绘制贝塞尔曲线 C
,绘制弯曲的线 S
)。stroke
和 fill
:控制路径的边框和填充颜色。SVG 元素支持与 HTML 元素相同的样式属性,可以使用 CSS 来控制它们的外观。也可以通过 SVG 内部样式(style
)属性进行定义。
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" style="border: 1px solid black;"> <circle cx="100" cy="100" r="80" stroke="black" stroke-width="3" fill="yellow" /></svg>php189 Bytes© 菜鸟-创作你的创作
style
:为 SVG 元素应用 CSS 样式。<style> svg circle { fill: orange; stroke: blue; stroke-width: 4; }</style><svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <circle cx="100" cy="100" r="80" /></svg>php204 Bytes© 菜鸟-创作你的创作
<style>
标签中定义,也可以通过外部 CSS 文件链接到 SVG。SVG 支持动画效果,可以使用 CSS 动画和 SVG 动画元素,如 <animate>
和 <animateTransform>
。
<style> @keyframes moveCircle { 0% { cx: 50; cy: 50; } 100% { cx: 150; cy: 150; } } circle { animation: moveCircle 2s infinite alternate; }</style><svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="20" fill="red" /></svg>php290 Bytes© 菜鸟-创作你的创作
@keyframes
定义动画,动态地改变 cx
和 cy
属性来移动圆形。<animate>
元素<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <circle cx="50" cy="50" r="20" fill="blue"> <animate attributeName="cx" from="50" to="150" dur="2s" repeatCount="indefinite" /> </circle></svg>php218 Bytes© 菜鸟-创作你的创作
<animate>
:动画效果,通过 attributeName
指定要动画化的属性,from
和 to
指定动画的起始和结束值。你可以通过 JavaScript 操作和控制 SVG 图形,实现更复杂的交互和动画效果。
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <circle id="myCircle" cx="50" cy="50" r="20" fill="blue" /></svg><script> var circle = document.getElementById("myCircle"); circle.addEventListener("click", function() { circle.setAttribute("fill", "red"); });</script>php300 Bytes© 菜鸟-创作你的创作
HTML5 的内联 SVG 提供了一种强大的方式来在网页中展示图形和动画。通过内联 SVG,开发者能够直接在 HTML 文档中编写图形,享受其灵活性和可交互性,并且可以使用 CSS 和 JavaScript 来增强其功能。SVG 在现代网页设计中越来越重要,尤其在需要响应式图形和动态效果时。https://www.52runoob.com/archives/5091
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。