Responsive SVG Layouts
Build SVGs that scale perfectly across all devices while maintaining vector crispness.
What is SVG Responsiveness?
SVGs naturally scale without loss of quality, but responsive layouts require careful design. Responsive SVGs adapt to screen size using:
viewBox
attribute for scaling- Relative units (% or em) over fixed values
preserveAspectRatio
to control scaling behavior- CSS media queries for dynamic adjustments
<svg width="100%" height="200" viewBox="0 0 400 200" preserveAspectRatio="none">
<rect width="100%" height="100%" fill="url(#grad)" />
<line x1="0" y1="0" x2="400" y2="200" stroke="indigo" stroke-width="2" />
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:indigo" />
<stop offset="100%" style="stop-color:purple" />
</linearGradient>
</defs>
</svg>
Advanced Techniques
ViewBox Magic
The viewBox
defines how SVG scales:
<svg viewBox="0 0 400 200">
Preserve Aspect Ratio
Control how SVG fills space:
<svg preserveAspectRatio="xMidYMid meet">
CSS-Driven Adapation
Use media queries for dynamic SVG styles:
@media (max-width: 640px) {
svg {
height: auto !important;
width: 100%;
}
}
@media (min-width: 1024px) {
svg {
height: 400px;
}
}
Try It Yourself
Resize your browser to see the SVG adapt!