/    Sign up×
Community /Pin to ProfileBookmark

How can I know where is the center of the window?

Hallo,
I have a <div> with a table and I would like to be posed exactly in the center of the window. Different user have different screen configuration.
How can I know where exactly the center of the window is, and pose the div there?

Thanks.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoJan 13.2007 — Well, the screen width and height can be accessed via, screen.width and screen.height

However, users don't always have the window maximized, and some people have scroll bars enabled while other dont use scroll bar. There for you would need to tka the window height and width, and then the offset height and wifth. Then get the coordinates for the top left corner of the window and find the center.
Copy linkTweet thisAlerts:
@tome100Jan 13.2007 — This is probably a big no no, but what if you put the div position to absolute, then make the width and height 100%, then take your content and put it in another div or table and align it to center and valign it to middle?
Copy linkTweet thisAlerts:
@TNOJan 13.2007 — This should work: (cross-browser vertical alignment)

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Universal vertical center with CSS&lt;/title&gt;
&lt;style&gt;
.greenBorder {border: 1px solid green;} /* just borders to see it */
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div class="greenBorder" style="display: table; height: 400px; #position: relative; overflow: hidden;"&gt;
&lt;div style=" #position: absolute; _top: 50%;display: table-cell; vertical-align: middle;"&gt;
&lt;div class="greenBorder" style=" #position: relative; #top: -50%"&gt;
any text&lt;br&gt;
any height&lt;br&gt;
any content, for example generated from DB&lt;br&gt;
everything is vertically centered
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Mr_JJan 13.2007 — Try this example

[code=php]
<style type="text/css">

#testdiv{
position:absolute;
left:50%;
top:50%;
width:100px;
height:150px;
margin-left:-50px;
margin-top:-75px;
border:2px solid blue;
}


</style>

<div id="testdiv">Centering with CSS</div> [/code]


If you position position the layer top:50% left:50% this will place the top left corner of the layer in the middle of the window, then by using margin-left and margin-top and giving a negative value equal to half the width and height repectively the layer is centered.
Copy linkTweet thisAlerts:
@Betty_SauthorJan 13.2007 — So, instead of 'px' I should use '%'.

Thanks Mr J, your example was both simple and helpful.

However, I didn't understand TNO example:

What does '#' maens? ("style=" #position: relative; #top: -50%")

Thanks!? :o ?
Copy linkTweet thisAlerts:
@Mr_JJan 14.2007 — I tried TNO's code but it did not work in my IE7 and I have not come across that style code before
Copy linkTweet thisAlerts:
@jayapalchandranJan 14.2007 — screenXmid=document.body.clientWidth/2

screenYmid=document.body.clientHeight/2

consider that you have a div and you want position it in the center screen

horizontally and vertically

let the div properties be like the following

<div id='me' style="position:absolute; left:0;top:0;width:200;height:200;border:1px solid gray;">

some text<br>

some text

<div>

so the code is ...


<html>

<head>

<script>

var screenXmid,screenYmid

screenXmid=document.body.clientWidth/2

screenYmid=document.body.clientHeight/2

var divId=document.getElementById('me')

var divXmid= divId.offsetWidth/2

var divYmid=divId.offsetHeight/2

divId.style.left= screenXmid - divXmid

divId.style.top= screenYmid - divYmid

</script>

</head>

<body>

<div id='me' style="position:absolute; left:0;top:0;width:200;height:200;border:1px solid gray;">

some text<br>

some text

<div>

</body>

</html>

else you can do like this

<body>

<div id='me' style="position:absolute; left:0;top:0;width:200;height:200;border:1px solid gray;">

some text<br>

some text

<div>

<script>

var screenXmid,screenYmid

screenXmid=document.body.clientWidth/2

screenYmid=document.body.clientHeight/2


var divXmid= me.offsetWidth/2

var divYmid=me.offsetHeight/2

me.style.left= screenXmid - divXmid

me.style.top= screenYmid - divYmid

</script>

</body>

if you have some problem when using this and that would be of because

the screen pixcel is returned as 10px or 20 px

so what you have to do is to remove the px from 10 so that it will be treated as integer for calculation

you can use parseInt(divXmid) to get the absolute value...

if you want a real example you can go to this page and see the source code

http://jayapalchandran.sindhimodel.com/main/credits/credits.html

even if you have some trouble then contact me...
Copy linkTweet thisAlerts:
@Betty_SauthorJan 15.2007 — thanks?
×

Success!

Help @Betty_S spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...