đď¸ 21062025 1651
đ
svg_viewbox
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â
viewBox
enables the SVG to scale independently of pixel size.- Changing the SVG's
width
orheight
scales the contents defined within theviewBox
. - Internal elements remain consistent relative to the
viewBox
dimensions.
Example Concept:â
- SVG with
viewBox="0 0 100 100"
andwidth="300"
:- A shape with width
100
will render as300px
wide.
- A shape with width
- Changing width to
500
scales the entire content proportionally.
Key Concept: viewBox
â Sizeâ
viewBox
affects coordinate system, not rendered size.- Size is controlled by the
width
andheight
attributes.
Aspect Ratio Behaviorâ
- The default aspect ratio is inferred from the
viewBox
. - If only
width
orheight
is provided:- Browser preserves the viewBox aspect ratio to calculate the missing dimension.
To override:â
- Set both
width
andheight
explicitly.
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 |
preserveAspectRatio
affects how the viewBox is fitted into the space defined bywidth
andheight
.
Summaryâ
viewBox
sets up a scalable coordinate space.width
/height
control rendered size.- SVGs scale their content based on the ratio between
viewBox
and output size. preserveAspectRatio
fine-tunes how content is aligned and scaled.