I have a large image within a div which i want to scroll. But instead of the div scrolling, i have the document scrolling.
How can i get the image to scroll within the DIV and NOT the parent or document?
My code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Force Scroll</title>
<style type="text/css">
.scrollable
{
overflow: auto;
}
</style>
</head>
<body>
<div class="scrollable">
<img src="image.jpg" alt="test image" />
</div>
</body>
</html>
Set a (max) width and height for the div and it should work. Otherwise the div will just adjust itself to the size of the image:
Example here: http://jsfiddle.net/x58RD/.
If it happens that you are forced to support ancient browsers like IE6, you need to use
widthandheightinstead ofmax-widthandmax-heightanyways or it still won’t work.