tags:

views:

315

answers:

1

Hi,

I have a div called #whatnewstoggler and it's width changes dynamically. This causes problems because I cannot centre this horizontally. The only way is to get the width and add inline css dynamically using jquery.

The js works in all browsers apart from IE6 and IE7. IE8 works fine.

I have narrowed the problem down to this line var totalOffset = halfslideshow - offset;

When I alert(''+ totalOffset +''); it gives me '0' but when I alert(''+ offset +''); it gives me the correct with before the final calculation.

Here is the code.

var halfslideshow = 312;
var width = $('#whatnewstoggler').width();
var offset = width / 2;      
var totalOffset = halfslideshow - offset;
//alert(''+ totalOffset +'');
$('#whatnewstoggler').css('margin-left',totalOffset);

Thanks for your help!

+2  A: 

the following css should automatically centre a div:

display: block;
margin: 0 auto;
iAn
That doesn't work in IE6
Steerpike
Toon Van Acker
It does work in IE6. It does not work in IE5.5, and if you need to support that browser, there's an easy way around it: body{text-align:center;} #container{margin:0 auto;text-align:left;}
cpharmston
None of those work. I solved it by setting the containing div to inline, measure the width with javascript, then add display block with javascript.By the way, this feature would look very odd visually if I did not implement this for IE6 of which 6,050 people per month use(gathered via Google Analytics and Serverside analytics). Thanks for the help guys.
Model Reject
"6,050 people per month use" lol. That's not the number of people worldwide that use IE6. The number of people that visit this particular website. Doh!
Model Reject
I can't understand why this wouldn't work, as it's almost industry standard in terms of centre aligning a fixed sized container: even this site uses it! You might want to try a minimum-width in the css to force the box model in IE. This seems like an awful lot of work to centre a div.
iAn