The width attribute defines the horizontal 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="https://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="https://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: