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 Margins
  • 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 Margins
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Flexible Background with preserveAspectRatio

An SVG that is set as a background behaves the same as an inline SVG, so to get an elastic background, use an SVG with viewBox but no dimensions: in this case, the image will resize itself to the size of 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 parent element, 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:

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 is assigned to the .no-viewbox block without a viewbox, and it does not stretch.

    1. Set a background image address has-viewbox.svg for the .has-viewbox block, and observe how the background with the viewbox behaves: The image fills the available space while maintaining its aspect ratio.
    2. Set a background image address has-viewbox-psa-none.svg 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−2025

    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.