HTML Academy
The preserveAspectRatio Attribute
Dimensions in SVG6/15
Back to the list of tasks
  • 1. SVG Width and Height
  • 2. The viewBox Attribute
  • 3. viewBox and Dimensions
  • 4. Challenge: Fish
  • 5. The preserveAspectRatio Attribute
  • 6. Flexible Background with preserveAspectRatio
  • 7. Challenge: Flexible Stripes
  • 8. Alignment in preserveAspectRatio
  • 9. preserveAspectRatio and viewBox
  • 10. Filling the Space
  • 11. Challenge: Rainbow
  • 12. Units
  • 13. Coordinate systems
  • 14. Coordinate Systems and Transforms
  • 15. Challenge: Yellow Leaf
Challenge: Flexible Stripes
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Flexible Background with preserveAspectRatio

To get an elastic SVG background, use an SVG with a viewBox but no explicit dimensions: in this case, the image can resize with the element for which a background is set and will stretch together with it while maintaining the aspect ratio:

This is very convenient for icons: set the dimensions for the element that has the background, and the icon that is set as the background will stretch itself to match it.

If you do not want to maintain the same aspect ratio, add preserveAspectRatio="none". This is suitable for creating flexible backgrounds. In this task, background-size: 100% 100% makes the image’s viewport match the element’s dimensions:

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html> <head> <title>Flexible Background with preserveAspectRatio</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="background no-viewbox"></div> <div class="background has-viewbox"></div> <div class="background has-viewbox-psa-none"></div> </body> </html>
CSS
.background { height: 200px; background-repeat: no-repeat; background-position: 50% 0; background-size: 100% 100%; border: 2px solid gold; } .no-viewbox { background-image: url("no-viewbox.svg"); } .has-viewbox { } .has-viewbox-psa-none { }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Goalscompleted
0

    The background image assigned to the .no-viewbox block is an SVG without a viewBox, and its drawing does not stretch.

    1. Set has-viewbox.svg as the background image for the .has-viewbox block, and observe how the background with the viewBox behaves: The drawing scales to fit the available space while maintaining its aspect ratio.
    2. Set has-viewbox-psa-none.svg as the background image for the .has-viewbox-psa-none block. Note how the background image is now stretched without preserving its aspect ratio, and the background is completely flexible.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.