Open In App

SVG width Attribute

Last Updated : 31 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The width attribute defines the vertical length of an element.

Syntax:

width= "width"

Attribute Values:

  • length: Length at which we want to set the width attribute.
  • percentage: Percentage at which we want to set the width attribute.

We will use the width attribute for setting the width of the element.

Example 1: In this example we will use the width attribute for setting the width of rect using length value.




<!DOCTYPE html>
<html>
  
<body>
    <svg viewBox="0 0 900 300" 
        xmlns="http://www.w3.org/2000/svg">
          
        <rect y="45" x="45" width="30" 
            height="20" fill="green" />
          
        <rect y="90" x="45" width="30" 
            height="20" fill="green" />
    </svg>
</body>
  
</html>


Output:

Example 2: In this example we will use the height attribute for setting the height of rect using percentage value.




<!DOCTYPE html>
<html>
  
<body>
    <svg viewBox="0 0 900 300" 
        xmlns="http://www.w3.org/2000/svg">
          
        <rect y="45" x="45" width="30%" 
            height="10%" fill="green" />
          
        <rect y="90" x="45" width="30%" 
            height="10%" fill="green" />
    </svg>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads