How to make an svg scale with its parent container


SVG, or Scalable Vector Graphics, is a markup language built on XML which is employed to produce vector graphics. SVG pictures can be scaled to any size without losing quality because they are independent of resolution. They are the best option for designing visuals that must be viewed on many devices with various screen widths. We'll talk about making an SVG scale with its parent container in this article. The fundamentals of SVG, how to design one, and how to make it responsive will all be covered.

What is SVG?

Let's first define SVG before learning how to make an SVG scale with its parent container.

SVG is a markup language for vector graphics that are based on XML. Vector graphics are composed of forms, lines, and curves that are specified by mathematical equations as opposed to raster graphics, which are composed of pixels like those in JPG or PNG.

SVG pictures can be resized without losing quality at any size. They are therefore the best option for designing visuals that must be viewed on many devices with various screen widths. SVG pictures are very customizable and may be altered using both CSS and JavaScript.

Approaches

One can make an SVG scale with its parent container using the following two methods −

  • Utilizing CSS.

  • Utilizing javascript.

Let us look into both approaches −

Approach 1: Utilizing CSS

An SVG can be made to scale with its parent container using CSS. Steps to be followed −

  • Step 1 − Adjust the parent container's width and height to the dimensions that you desire for the SVG.

  • Step 2 − To the parent container, add the SVG.

  • Step 3 − Set the SVG's width as well as height to 100% using CSS.

  • Step 4 − To guarantee that the SVG's aspect ratio is preserved, use the preserveAspectRatio attribute.

Example

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
      .parentContainer {
         width: 400px;
         height: 400px;
      }
      svg {
         width: 100%;
         height: 100%;
      }
   </style>
</head>
<body>
   <div class="parentContainer">
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
         <!-- Placing SVG content here -->
         <circle cx="20" cy="20" r="15" stroke="black" stroke-width="2" fill="green" />
      </svg>
   </div>
</body>
</html>

Approach 2: Utilizing Javascript

An SVG can be made to scale with its parent container using JavaScript as well. Below are the steps that have to be followed to scale svg.

  • Step 1 − Get a reference to the SVG as well as the parent container.

  • Step 2 − Set the parent container's width and height as the SVG's width and height.

  • Step 3 − To guarantee that the SVG's aspect ratio is preserved, utilize the preserveAspectRatio attribute.

  • Step 4 − When the window is resized, add an event listener to the window object.

  • Step 5 − Update the SVG's width and height to correspond with the parent container's width and height when the window is resized.

Example

<!DOCTYPE html>
<html lang="en">
<body>
   <div class="parentContainer">
   <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
      <!-- Placing SVG content here -->
      <circle cx="20" cy="20" r="15" stroke="black" stroke-width="2" fill="green" /> 
   </svg> 
   <!--add a script tag for adding javascript-->
   <script>const parent = document.querySelector('.parentContainer');
      const svg = parent.querySelector('svg');
      function scaleSVG() {
         const { width, height } = parent.getBoundingClientRect();
         svg.setAttribute('height', height);
         svg.setAttribute('width', width);
      }
      window.addEventListener('resize', scaleSVG);
      scaleSVG();
   </script>
</body>
</html>

Note

  • Circles as well as rectangles are some basic shape elements in SVG and can be utilized to create a variety of aesthetic designs.

  • To generate a circular shape, a circle element is utilized, and to produce a rectangular shape, a rectangle element is utilized.

  • Both components can have their appearance altered with CSS properties like fill, and stroke, as well as using stroke-width.

  • Both items can also be changed in position, rotation, and size by applying transformations like translate, rotate, and scale.

  • These components serve as the basic building blocks for SVG graphics and can be combined with various shapes and components to produce intricate designs.

  • Polygons, lines, pathways, ellipses, and arcs are a few additional fundamental shapes that can be made in SVG.

Conclusion

SVG is a flexible and adaptable markup language for vector graphics, to sum up. An SVG can be made to scale with its parent container using CSS or JavaScript, enabling responsive design. SVGs are a fantastic option for multi-device viewing since designers can build high-quality visuals using them that can be scaled to any size without sacrificing quality.

Updated on: 22-Nov-2023

723 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements