Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll svg inside div

I have a simple use case. I have an outer div and an svg inside. Like,

<div>
  <svg>
     ...
     ...
  </svg>
</div>

I am trying to get the svg to scroll inside the div, but without any luck :( I tried setting:

div {
  position: relative;
  width: 100%;
  overflow: scroll;
}

svg {
  width: 100%;
}

But it does not work, can you guys help me out? Thanks for the help!

like image 601
waka-waka-waka Avatar asked Apr 20 '15 03:04

waka-waka-waka


3 Answers

you have to define the container height, otherwise the container height will be adjusted to the svg height.

html:

<div>
    <svg viewbox="0 0 400 400">
        <path d="M 200 100 l 100 200 l -200 0 Z" stroke-width="5" stroke="red"></path>
    </svg>
</div>

css:

div {
  position: relative;
  width: 100%;
  height: 400px;
  overflow-y: scroll;
}

see https://jsfiddle.net/Lvnozzn2/1/

like image 125
Pavel Gatnar Avatar answered Sep 28 '22 06:09

Pavel Gatnar


Usually scrolling will show up when you set a size to the div and the content (in your case the svg) overflows the size. for instance this is the css class that I wrote earlier today that has a scroll bar on it once the content starts overflowing outside the div

#outbox_div {
border: 3px solid grey;
border-radius:2px;
height:200px;
width:80%;
overflow-y:scroll;
}
like image 26
Mark Avatar answered Sep 28 '22 07:09

Mark


I would mark the div as overflow:scroll.

This way, as the svg component grows the div will scroll fit it.

When using svg components I always put them inside their own div to compartmentalize the graphics code from the rest of the page.

like image 33
Marcus Henry Avatar answered Sep 28 '22 05:09

Marcus Henry