views:

210

answers:

2

Hello, Im trying to use java script to center a page horizontally.

So far i got this:

<body onload="scrollBy((document.body['scrollWidth'] - 0) / 2, 0)">

It kinda works, but not very good. I have a large width flash component that I need centered when the page loads and also after any click on the internal buttons.

I was thinking of making a call from flash to a javascript that would scroll the page to an HTML anchor.

Something along the lines of:

 <script type="text/javascript">
 function pageMid() {
 window.location.href="#mid"
 }
 </script>

But the anchors don't align to middle.

Any ideas?

Thank you.

A: 

This worked:

<script type="text/javascript">
function pageMid() {
scrollTo(1000, 0);
}
</script>

 <body onload="pageMid();">

Now I just cant figure it out on how to call this from flash catalyst...

Minbot
A: 

It sounds like you want to center the element vertically, not horizontally. If you know the size of the element you're trying to center, use CSS:

<style>
#myThing {
   height: 300px;
   width: 400px;
   position:absolute;
   top:50%;
   left:50%;
   margin-top:-150px;
   margin-left:-200px;
}
</style>

<div id="myThing"></div>
Kent Brewster
Thank you, but im trying to get it to center horizontally :)
Minbot
This should do what you want with CSS. Substitute the rectanglar div of your choice for body:<style>body { text-align:center;}#myThing { margin:0 auto;}</style>
Kent Brewster