Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show scrollbars if SVG elements overflows their container

I have an embedded SVG with dynamic content inside, which may grow in all directions. This content may be grow bigger than the fix sized container around. My expected behavoir is, to show scrollbars, if any element inside the SVG needs more place than the container provides.

See the following simplified example:

<html>
    <head>
    </head>
    <body>
    <div style="overflow: auto; position: absolute; top: 60px; left: 60px; background-color: gray; height: 200px; width: 300px;">
        <svg style="height: 190px;">
                <rect x="-50" y="0" width="100" height="50" fill="red"></rect>
        </svg>
    </div>
    </body>
</html>

What is the way to do this? Is it really true, there is no concept in SVG to support such behavoir? Any suggestions how to do it right "by hand"?

like image 427
Matthias Kientz Avatar asked Feb 20 '26 23:02

Matthias Kientz


1 Answers

Svg does'nt support auto resizing to inside elements of itself.
So, you should resize manually the svg graphic to be able to scroll by outer svg element.

var svg = document.querySelector("svg");
var bbox = svg.getBBox();
svg.setAttribute("viewBox", [bbox.x, bbox.y, bbox.width, bbox.height]);
svg.width.baseVal.valueAsString = bbox.width;
svg.height.baseVal.valueAsString = bbox.height;
like image 151
defghi1977 Avatar answered Feb 22 '26 12:02

defghi1977



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!