详细介绍SVG的stroke描边属性,包括颜色设置、透明度控制、描边位置等。通过实例掌握描边的各种用法。 stroke属性定义图形轮廓的颜色,是SVG中最常用的样式属性之一。
<element stroke="颜色值"/>
<rect x="20" y="20" width="70" height="60" fill="none" stroke="#e74c3c" stroke-width="3"/>
<rect x="115" y="20" width="70" height="60" fill="none" stroke="#3498db" stroke-width="3"/>
<rect x="210" y="20" width="70" height="60" fill="none" stroke="#2ecc71" stroke-width="3"/>
<rect x="20" y="20" width="70" height="60" fill="none" stroke="rgb(231, 76, 60)" stroke-width="3"/>
<rect x="115" y="20" width="70" height="60" fill="none" stroke="rgb(52, 152, 219)" stroke-width="3"/>
<rect x="210" y="20" width="70" height="60" fill="none" stroke="rgba(46, 204, 113, 0.5)" stroke-width="3"/>
<rect x="20" y="20" width="70" height="60" fill="none" stroke="red" stroke-width="3"/>
<rect x="115" y="20" width="70" height="60" fill="none" stroke="blue" stroke-width="3"/>
<rect x="210" y="20" width="70" height="60" fill="none" stroke="green" stroke-width="3"/>
<circle cx="100" cy="50" r="40" fill="#3498db" stroke="none"/>
<svg style="color: #e74c3c;">
<circle cx="100" cy="50" r="40" fill="none" stroke="currentColor" stroke-width="3"/>
</svg>
单独控制描边的透明度,不影响填充。
<rect x="20" y="20" width="70" height="60" fill="#ecf0f1" stroke="#3498db" stroke-width="5" stroke-opacity="1"/>
<rect x="115" y="20" width="70" height="60" fill="#ecf0f1" stroke="#3498db" stroke-width="5" stroke-opacity="0.5"/>
<rect x="210" y="20" width="70" height="60" fill="#ecf0f1" stroke="#3498db" stroke-width="5" stroke-opacity="0.2"/>
<defs>
<linearGradient id="strokeGrad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#3498db"/>
<stop offset="100%" style="stop-color:#e74c3c"/>
</linearGradient>
</defs>
<rect x="20" y="20" width="160" height="60" fill="none" stroke="url(#strokeGrad)" stroke-width="5"/>
描边位于路径的中心线上,一半在图形内部,一半在外部。
<rect x="50" y="50" width="100" height="50" fill="#3498db" stroke="#e74c3c" stroke-width="10"/>
<circle cx="60" cy="50" r="35" fill="none" stroke="#3498db" stroke-width="3"/>
<path d="M 45,50 L 55,60 L 75,40" fill="none" stroke="#2ecc71" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<text x="100" y="60" text-anchor="middle" font-size="40" font-weight="bold" fill="#3498db" stroke="#2980b9" stroke-width="2">SVG</text>
<text x="100" y="65" text-anchor="middle" font-size="50" font-weight="bold" fill="none" stroke="#3498db" stroke-width="2">TEXT</text>
<text x="50" y="60" text-anchor="middle" font-size="30" font-weight="bold" fill="#3498db">Fill</text>
<text x="150" y="60" text-anchor="middle" font-size="30" font-weight="bold" fill="#3498db" stroke="#e74c3c" stroke-width="1">Both</text>
<text x="250" y="60" text-anchor="middle" font-size="30" font-weight="bold" fill="none" stroke="#2ecc71" stroke-width="2">Stroke</text>
stroke属性控制图形轮廓: