<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <style> svg { margin: 10px; padding: 5px; } </style> <!-- 其他SVG内容 --> </svg>
<svg width="100" height="100" style="margin: 10px; padding: 5px;" xmlns="http://www.w3.org/2000/svg"> <!-- 其他SVG内容 --> </svg>
viewBox 定义了一个 100x100 的内部坐标系统,通过设置 padding,可以在SVG元素内部创建一个边距效果。
注意事项
如果你将 SVG 作为 HTML 文档的一部分,那么 SVG 的 margin 和 padding 将会受到 CSS 的影响。
通过这些方法,你可以在 SVG 文件中设置 margin 和 padding,以实现所需的布局效果。
设置 SVG 颜色,可以使用 fill 属性:
<circle cx="50" cy="50" r="40" stroke="black" fill="red"/>
设置 SVG 颜色,使用 stroke 属性:
<circle cx="50" cy="50" r="40" stroke="blue" fill="green"/>
设置 SVG 颜色,使用 style 属性:
<circle cx="50" cy="50" r="40" style="fill: yellow; stroke: purple;"/>
设置 SVG 颜色,使用 class 和 CSS:
<circle cx="50" cy="50" r="40" class="my-circle"/>
.my-circle { fill: green; stroke: black; }
设置 SVG 颜色,使用 JavaScript:
var circle = document.getElementById('my-svg'); circle.setAttribute('fill', 'orange');
设置 SVG 颜色,SVG 支持线性渐变和径向渐变 - Gradients:
<defs> <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" /> <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" /> </linearGradient> </defs> <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)"/>