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?
LVL 10
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!
<script language="javascript">
function toggleDiv()
	var div = document.getElementById("floatDiv");
	if ( == "none")
	{ = "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);
			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);
	{ = "none";
Form Field One: <input name="ff1" type="text"/>
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

Open in new window

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

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.