đď¸ 21062025 1651
What is viewBox?â
- Defines the visible region of the SVGâs internal coordinate system.
- Syntax:
viewBox="<min-x> <min-y> <width> <height>" - Think of it like a camera: it determines what part of the canvas is visible, not the actual size of the SVG.
How Responsiveness Worksâ
viewBoxenables the SVG to scale independently of pixel size.- Changing the SVG's
widthorheightscales the contents defined within theviewBox. - Internal elements remain consistent relative to the
viewBoxdimensions.
Example Concept:â
- SVG with
viewBox="0 0 100 100"andwidth="300":- A shape with width
100will render as300pxwide.
- A shape with width
- Changing width to
500scales the entire content proportionally.
Key Concept: viewBox â Sizeâ
viewBoxaffects coordinate system, not rendered size.- Size is controlled by the
widthandheightattributes.
Aspect Ratio Behaviorâ
- The default aspect ratio is inferred from the
viewBox. - If only
widthorheightis provided:- Browser preserves the viewBox aspect ratio to calculate the missing dimension.
To override:â
- Set both
widthandheightexplicitly.
preserveAspectRatio Attributeâ
| Purpose | Example Value | Behavior |
|---|---|---|
| Anchor viewBox | xMidYMid | Centers content in the available space |
| Scale mode: preserve or crop | meet / slice | meet: fit all content; slice: fill & crop |
preserveAspectRatioaffects how the viewBox is fitted into the space defined bywidthandheight.
Summaryâ
viewBoxsets up a scalable coordinate space.width/heightcontrol rendered size.- SVGs scale their content based on the ratio between
viewBoxand output size. preserveAspectRatiofine-tunes how content is aligned and scaled.