Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

centering div tags

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
0
0
CA
Is there anyway i can use javascript to center a div tag on a page? I'm using style sheets to position the tag on the screen right now but i want the contents of the tag centered regardless of the screen resolution. Any help is greatly appreciated.

Thanks
 
Unfortunately if i use left:50% it moves the top left-hand corner of the div tag to the center of the page which means that all the contents of the div tag start at the center of the page and i want the center of the div's contents to be in the center of the page, in effect centering th div tag
 
function center(el) {
var x=screen.availWidth-el.offsetWidth;
var y=screen.availHeight-el.offsetHeight;

el.style.left=x; el.style.y=y;
}

Hope that helps!

Happy coding! :)
 
Not sure If I know what I am talking about so feel free to correct.

<div align=&quot;center&quot;>center this</div>

good luck
 

>>i want the contents of the tag centered regardless of the screen resolution
What's the connection between content alignment and screen resolution?
use this in css:

text-aligh: center;

This will center the content of your divs.

 
As a reference to my previous post, the code was incorrect (I just wasn't thinking). Here is the correct code.

function center(obj) {
var x=screen.availWidth/2-obj.offsetWidth;
var y=screen.availHeight/2-obj.offsetHeight;

obj.style.left=x; obj.style.top=y;
}

Hope that helps!

Happy coding! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top