Press the button to center the div, press it again to align it to the left. This feature is compatible with all browsers except for IE6, which does not support margin: 0 auto;
. How can I find a solution to this issue? The width of the div is not fixed and can vary.
Visit jsfiddle at http://jsfiddle.net/hZ23J/1/
<button id="center">Left Align</button>
<div></div>
$('#center').toggle(function() {
$('div').css('margin', '0');
$(this).text('Center Align')
}, function() {
$('div').css('margin', '0 auto');
$(this).text('Left Align')
});