SVG <rect>
The SVG (Scalable Vector Graphics) <rect> element is used to draw rectangles. It is one of the basic shapes commonly used in SVG, allowing you to draw rectangles and control their position, size, rounded corners, and other styles through attribute settings.
Basic Syntax
<rect x="x-coordinate" <!-- x-coordinate of the rectangle's top-left corner -->
y="y-coordinate" <!-- y-coordinate of the rectangle's top-left corner -->
width="width-value" <!-- width of the rectangle -->
height="height-value" <!-- height of the rectangle -->
rx="rx-value" <!-- corner radius of the rectangle (horizontal) -->
ry="ry-value" <!-- corner radius of the rectangle (vertical) -->
fill="fill-color" <!-- fill color of the rectangle -->
stroke="stroke-color" <!-- stroke color of the rectangle -->
stroke-width="width-value" <!-- stroke width of the rectangle --> />
Attribute Explanation:
- The
xandyattributes specify the coordinates of the rectangle's top-left corner, i.e., its starting point. - The
widthandheightattributes define the width and height of the rectangle. - The
rxandryattributes are used to specify the corner radius of the rectangle. If onlyrxis set, all corners will have the same radius. If bothrxandryare set, you can specify different radii for horizontal and vertical directions. - The
fillattribute defines the fill color of the rectangle. - The
strokeattribute defines the stroke color of the rectangle. - The
stroke-widthattribute defines the stroke width of the rectangle.
The following code draws a rectangle with blue fill, black stroke, and 2-pixel stroke width. Its top-left corner is at (50, 50), with a width of 100 and height of 80.
Example 1
Below is the SVG code:
Example
Click to view: View SVG file.
Code Explanation:
- The
widthandheightattributes of the rect element define the height and width of the rectangle. - The
styleattribute is used to define CSS properties. - The CSS
fillproperty defines the fill color of the rectangle (RGB value, color name, or hexadecimal value). - The CSS
stroke-widthproperty defines the border width of the rectangle. - The CSS
strokeproperty defines the border color of the rectangle.
Example 2
Let's look at another example with some new attributes:
Example
Click to view: View SVG file.
Code Explanation:
- The
xattribute defines the left position of the rectangle (e.g., x="0" means the rectangle is 0px from the left edge of the browser window). - The
yattribute defines the top position of the rectangle (e.g., y="0" means the rectangle is 0px from the top edge of the browser window). - The CSS
fill-opacityproperty defines the transparency of the fill color (valid range: 0 to 1). - The CSS
stroke-opacityproperty defines the transparency of the stroke color (valid range: 0 to 1).
Example 3
Defining the opacity of the entire element:
Below is the SVG code:
Example
For Opera users: View SVG file.
- The CSS
opacityproperty defines the transparency value of the element (range: 0 to 1).
Example 4
Finally, an example creating a rounded rectangle:
Below is the SVG code:
Example
For Opera users: View SVG file.
- The
rxandryattributes can create rounded corners for the rectangle.
YouTip