Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

SVG <rect>


SVG Shapes

SVG has some predefined shape elements that can be used by developers:

  • Rectangle <rect>
  • Circle <circle>
  • Ellipse <ellipse>
  • Line <line>
  • Polyline <polyline>
  • Polygon <polygon>
  • Path <path>

The following chapters will explain each element, starting with the <rect> element.


SVG Rectangle - <rect>

The <rect> element is used to create a rectangle and variations of a rectangle shape.

The <rect> element has six basic attributes to position and shape the rectangle:

Attribute Description
width Required. The width of the rectangle
height Required. The height of the rectangle
x The x-position for the top-left corner of the rectangle
y The y-position for the top-left corner of the rectangle
rx The x radius of the corners of the rectangle (used to round the corners). Default is 0
ry The y radius of the corners of the rectangle (used to round the corners). Default is 0

An SVG Rectangle

This example creates a rectangle with the six basic attributes and a fill color:

Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
  <rect width="200" height="100" x="10" y="10" rx="20" ry="20" fill="blue" />
</svg>
Try it Yourself »

Code explanation:

  • The width and height attributes of the <rect> element define the height and the width of the rectangle
  • The x and y attributes defines the x- and y-position of the top-left corner of the rectangle (x="10" places the rectangle 10px from the left margin and y="10" places the rectangle 10px from the top margin) in the SVG canvas
  • The rx and ry attributes defines the radius of the corners of the rectangle
  • The fill attribute defines the fill color of the rectangle

A Rectangle With Border

Let's look at another example that contains some new attributes:

Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg width="320" height="130" xmlns="http://www.w3.org/2000/svg">
  <rect width="300" height="100" x="10" y="10" style="fill:rgb(0,0,255);stroke-width:3;stroke:red" />
</svg>
Try it Yourself »

Code explanation:

  • The style attribute is used to define CSS properties for the rectangle
  • The CSS fill property defines the fill color of the rectangle
  • The CSS stroke-width property defines the width of the border of the rectangle
  • The CSS stroke property defines the color of the border of the rectangle


A Rectangle With Opacity

Let's look at another example that contains some new attributes:

Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg width="300" height="170" xmlns="http://www.w3.org/2000/svg">
  <rect width="150" height="150" x="10" y="10"
  style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
</svg>
Try it Yourself »

Code explanation:

  • The CSS fill-opacity property defines the opacity of the fill color (legal range: 0 to 1)
  • The CSS stroke-opacity property defines the opacity of the stroke color (legal range: 0 to 1)

Another Rectangle With Opacity

Define the opacity for the whole element:

Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg width="300" height="170" xmlns="http://www.w3.org/2000/svg">
  <rect width="150" height="150" x="10" y="10"
  style="fill:blue;stroke:pink;stroke-width:5;opacity:0.5" />
</svg>
Try it Yourself »

Code explanation:

  • The CSS opacity property defines the opacity value for the whole element (legal range: 0 to 1)

A Rectangle With Rounded Corners

Last example, create a rectangle with rounded corners:

Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg width="300" height="170" xmlns="http://www.w3.org/2000/svg">
  <rect width="150" height="150" x="10" y="10" rx="20" ry="20"
  style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>
Try it Yourself »

Code explanation:

  • The rx and the ry attributes rounds the corners of the rectangle

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.