[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

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?
1 Solution
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 (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);
			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.display = "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! :)


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now