SVG <rect>
SVG Shapes
SVG has some predefined shape elements that can be used and manipulated by developers:
- Rectangle
<rect> - Circle
<circle> - Ellipse
<ellipse> - Line
<line> - Polyline
<polyline> - Polygon
<polygon> - Path
<path>
The following sections will explain these elements, starting with the rectangle element.
SVG Rectangle - <rect>
Example 1
The <rect> tag is used to create a rectangle and variations of a rectangle shape:
Here is the SVG code:
Example
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
Code Analysis:
- The
widthandheightattributes of therectelement define the width and height of the rectangle. - The
styleattribute is used to define CSS properties. - The CSS
fillproperty defines the fill color of the rectangle (rgb values, color names, or hexadecimal values). - The CSS
stroke-widthproperty defines the width of the rectangle's border. - The CSS
strokeproperty defines the color of the rectangle's border.
Example 2
Let's look at another example that includes some new attributes:
Example
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
Code Analysis:
- The
xattribute defines the left position of the rectangle (e.g.,x="0"defines the distance of the rectangle from the left side of the browser window as 0px). - The
yattribute defines the top position of the rectangle (e.g.,y="0"defines the distance of the rectangle from the top of the browser window as 0px). - The CSS
fill-opacityproperty defines the opacity of the fill color (valid range: 0 - 1). - The CSS
stroke-opacityproperty defines the opacity of the outline color (valid range: 0 - 1).
Example 3
Defining the opacity of the entire element:
Here is the SVG code:
Example
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
- The CSS
opacityproperty is used to define the transparency value of the element (range: 0 to 1).
Example 4
The last example, creating a rounded rectangle:
Here is the SVG code:
Example
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
- The
rxandryattributes make the rectangle have rounded corners.