Table of Contents
Basic Shapes in SVG
Following are the basic shape elements used in SVG.
<rect> element
Rounded rectangles can be achieved by setting appropriate values for attributes rx and ry. If only rx is specified, ry is equal to rx.
Following is an example code to create rectangle shape with “lightgreen” fill color and “red” stroke having 10px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<rect x=”5″ y=”5″ width=”200″ height=”100″ rx=”40″ ry=”10″
stroke=”red” stroke-width=”10″
fill=”lightgreen” fill-opacity=”0.6″ />
</svg>
</body>
</html>
<circle> element
Similar to the x, y attributes of <rect> element, if the attribute is not specified for cx and cy, the effect is as if a value of “0” were specified. Another attribute r of this element is the length of the radius of the circle.
Following is an example code to create circle with “pink” fill color and “green” stroke having 5px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<circle cx=”80″ cy=”80″ r=”60″
stroke=”green” stroke-width=”5″ fill=”pink” fill-opacity=”0.6″/>
</svg>
</body>
</html>
<ellipse> element
Following is an example code to create ellipse with “lightgreen” fill color and “brown” stroke having 0.9px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<ellipse cx=”110″ cy=”55″ rx=”90″ ry=”50″
stroke=”brown” stroke-width=”0.9″
fill=”lightgreen” fill-opacity=”0.6″ />
</svg>
</body>
</html>
<line> element
Following is an example code to create line with “green” stroke having 10px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<line x1=”30″ y1=”10″ x2=”280″ y2=”100″ stroke=”green” stroke-width=”10″ stroke-linecap=”round”/>
</svg>
</body>
</html>
<polyline> element
Following is an example code to create polyline with “pink” fill color and “lime” stroke having 10px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<polyline points=”100,30 140,130 210,130 250,30″
fill=”pink” fill-opacity=”0.7″ stroke=”lime”
stroke-width=”10″ stroke-linecap=”round” stroke-opacity=”0.5″ />
</svg>
</body>
</html>
<polygon> element
Similar to polyline element polygon element also require the points attribute, which contains a list of x,y value pairs.
Following is an example code to create polygon with “blue” fill color and “red” stroke having 10px stroke width.
<html>
<body>
<svg xmlns=”http://www.w3.org/2000/svg”>
<polygon points=”100,10 85,65 40,75 85,95 100,145 110,95 155,75 110,65″ fill=”blue” fill-opacity=”0.9″ stroke=”red”
stroke-width=”10″ stroke-opacity=”0.7″ stroke-linejoin=”miter”/>
</svg>
</body>
</html>
Read Next:How To Use Presentation Attributes in SVG