Stuart Smith
Stuart Smith

Reputation: 3

Auto height iframe to content

just cant figure out how to get the below code to auto adjust the hight, dependant on the path in "src". As i dont want hard code the height as the src path may change. I have tried various js methods, but none seem to work.

<iframe src="C:\" height="200" width="100%"></iframe>

I have done the styles inline, but will be managing all styles in a css file.

Help.

Upvotes: 0

Views: 161

Answers (2)

kamatchikumar-India
kamatchikumar-India

Reputation: 127

<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>

<iframe  src="http://stackoverflow.com/" frameborder="0" scrolling="no" id="iframe" onload='javascript:resizeIframe(this);' />

Upvotes: 1

David Bradshaw
David Bradshaw

Reputation: 13097

You need to use JavaScript to adjust an iFrame to match the size of the content. Check out this library that will watch to content in the iFrame and keep the size upto date.

https://github.com/davidjbradshaw/iframe-resizer

Upvotes: 1

Related Questions