Show top/left div corner in the middle of the browser screen?

I'm trying to show a "div" element to the user and i want it to show in the center of is browser screen.

I want to position the div top/left corner at the top/left/center of the user screen..

Anybody can help?
Thanks..
LVL 10
jabcocoAsked:
Who is Participating?
 
Repus024Connect With a Mentor Commented:
Hello jabcoco,

Please look at the attached code snippet.  This should be a simple example of what you are trying to accomplish.  This takes the div and places it in the center of the user's browser window.

Please note that the div has "position:absolute" specified.  This is what allows it to be positioned anywhere dynamically.

Please let me know if you have trouble with this script or if it does not do what you are trying to accomplish.

Hope this helps!
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
function toggleDiv()
{
	var div = document.getElementById("floatDiv");
	if (div.style.display == "none")
	{
		div.style.display = "inline";
		var IE=document.all;
		var iXPos;
		var iYPos;
		if (!IE)
		{
			iXPos = self.innerWidth / 2;
			iYPos = self.innerHeight / 2;
			alert("self.innerWidth: " + self.innerWidth
			+ "\n self.innerHeight: " + self.innerHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		else
		{
			iXPos = (document.body.clientWidth) / 2;
			iYPos = (document.body.clientHeight) / 2;
			alert("document.body.clientWidth: " + document.body.clientWidth
			+ "\n document.body.clientHeight: " + document.body.clientHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		div.style.left=iXPos;
  		div.style.top=iYPos;
	}
	else
	{
		div.style.display = "none";
	}
}
</script>
</HEAD>
 
<BODY>
<form>
Form Field One: <input name="ff1" type="text"/>
<br/>
Form Field Two: <input name="ff2" type="text"/>
<br/><input type="button" value="Toggle Div" onclick="toggleDiv()"/>
<div id="floatDiv" style="position:absolute;display:none">
Floating Div
</div>
</form>
</BODY>
 
</HTML>

Open in new window

0
 
jabcocoAuthor Commented:
Work's perfectly!
Exactly what I'm looking for! :)

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.