Lesson 6
Objective: Learn to draw, position and style a rectangle.
<rect x="0" y="0" width="40" height="20"></rect>
<rect> ... </rect> The full rectangle element with open and closing tags.
<rect /> Because this does not contain any content it can also be used as a self-closing element.
The rectangle has four mandatory attributes you must know and remember. The default units are pixels.
x="0" The rectangle attribute for the top-left starting x coordinate.
y="0" The rectangle attribute for the top-left starting y coordinate.
width="40" The attribute for the width of the rectangle.
height="20" The attribute for the height of the rectangle.
Optional construction attributes allow you to create rounded corners. You should experiment with these to understand what they can do.
<rect x="0" y="0" width="40" height="20" rx="5" ry="5"></rect>
rx="5" The x-axis radius of the ellipse used to round of the corners of a rectangle
ry="5" The y-axis radius of the ellipse used to round of the corners of a rectangle.
You can use stroke, stroke-width and fill attributes on a rectangle.This first example uses full SVG attributes to define the stroke colour and size of the rectangle. This is the "minimum rectangle" property set.
<rect x="110" y="50" width="180" height="300" stroke="black" stroke-width="4" fill="none"> </rect>
This second example contains the complete rectangle property set.
<rect x="50" y="100" width="300" height="200" rx="40" ry="40" stroke="red" stroke-width="4" fill="none"> </rect>
Left-click on the image to open the SVG Editor and get practicing. Change, add and delete rectangles.
Remember the default image size and viewBox is 400px X 400px.
Here is the SVG for those who are too lazy to type the SVG. View the SVG Markup
Here are our standard SVG Cheatsheets you can download, print out and have ready when needed. There is also a text file containing annotated quickstarts for all major shapes and techniques.
SVG Editor. Access a full SVG Editor online at any time.