1 reply [Last post]
Chris
Chris's picture
Offline
Regular
Last seen: 19 years 42 weeks ago
Timezone: GMT+10
Joined: 2003-06-10
Posts: 13
Points: 0

Is there a way to position a inline-frame to be centered verticaly and horizontaly. please HELP!

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 1 week 4 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

positioning inline-frames! HELP!

Hi Chris,
Not as simple a task as it should be.
Here is couple of things you could try:
Firstly you will need to set the display of the div to block or wrap the iframe in a div.
Then to center it horizontally you can use margin-left:auto and margin-right:auto as long as you know the width.
Unfortunately margin-top and margin-bottom set to auto would be way to easy, so the browsers have made it more chalanging for you.
What I would do is use margin-top set to a specific value or % and be happy that it's close to the center in most browsers.

You could use the magic of Dhtml to verticaly center it.

function vertcenter(x){
    var x=document.getElementById(x);
    var h=x.style.height;
    var sch=screen.availHeight;
    var dt= (sch-h)/4;
    x.style.marginTop=dt+"px";
}

div#theid{ margin-left:auto; margin-right:auto; width:300px; height:300px; margin-top:0; margin-bottom:0;}
<body onload="javascript:vertcenter('theid');">
<div id="theid">
<iframe  src="sitecheck12.html" width="300px" height="300px"></iframe></div>