SVG <rect>
- Página Anterior SVG em HTML
- Próxima Página SVG Circular
SVG has some predefined shape elements that can be used and manipulated by developers.
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 chapters will explain these elements, starting with the rectangle element.
The <rect> tag
The <rect> tag can be used to create rectangles and variants of rectangles.
To understand how it works, copy this code into a notepad and save it as a "rect1.svg" file. Place this file in the web directory:
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1; stroke:rgb(0,0,0)"/> </svg>
Explicação do Código:
- The width and height attributes of the rect element can define the height and width of the rectangle
- The style attribute is used to define CSS properties
- The CSS fill property defines the fill color of the rectangle (rgb values, color names, or hexadecimal values)
- The CSS stroke-width property defines the width of the rectangle border
- The CSS stroke property defines the color of the rectangle border
Let's look at another example that includes new properties:
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" width="250" height="250" style="fill:blue;stroke:pink;stroke-width:5; fill-opacity:0.1;stroke-opacity:0.9"/> </svg>
Example explanation:
- The x attribute defines the left position of the rectangle (for example, x="0" defines the distance from the left of the rectangle to the left of the browser window is 0px)
- The y attribute defines the top position of the rectangle (for example, y="0" defines the distance from the top of the rectangle to the top of the browser window is 0px)
- The CSS fill-opacity property defines the transparency of the fill color (the valid range is: 0 - 1)
- The CSS stroke-opacity property defines the transparency of the stroke color (the valid range is: 0 - 1)
define a transparency for the entire element:
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" width="250" height="250" style="fill:blue;stroke:pink;stroke-width:5; opacity:0.9"/> </svg>
Explicação do Código:
A propriedade opacity do CSS define o valor de transparência do elemento inteiro (o intervalo válido é: 0 - 1)
Último exemplo, criar um retângulo com cantos arredondados:
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <rect x="20" y="20" rx="20" ry="20" width="250" height="100" style="fill:red;stroke:black; stroke-width:5;opacity:0.5"/> </svg>
Explicação do Código:
Os atributos rx e ry permitem que os retângulos tenham cantos arredondados.
- Página Anterior SVG em HTML
- Próxima Página SVG Circular