jQuery: Center div

Using jQuery (not plain CSS) how can I center a div in the middle of the window?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box</title>
<style type="text/css">

#v1 {
	position: absolute;
	top: 50px;
	z-index: 112;
	margin: 0 auto 0 auto;
	padding: 0;
	width: 800px;
	background: #fff;
	border: 2px solid #111;
	vertical-align: top;

}

#v1 * {
	vertical-align: top;
	padding: 0;
	margin: 0;
}

</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
alert('I want to center the #v1 div');
});
</script>

</head>
<body>

<h1>Hello World</h1>
<h2>This is a test</h2>
<p>Blah blah blah.</p>

<div id="v1">
 <p><img src="http://piccies.flybywire.org.uk/Gliding/2004/20040905/MeFinishing3.jpg"></p>
 </p>
</div>

</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tdotCommented:
attach to div center

$('div').css.( {
  'width':' 700px ', //set to the width to div
  'margin-left': 'auto',
  'margin-right': 'auto'
});
hankknightAuthor Commented:
tdot, your idea does NOT work because the position is absolute.  Please test your idea before making another post, thanks.
tdotCommented:
Try this, tested in firefox and ie8.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box</title>
<style type="text/css">
#v1 {
        position: absolute;
        top: 50px;
        z-index: 112;
        margin: 0 auto 0 auto;
        padding: 0;
        width: 800px;
        background: #fff;
        border: 2px solid #111;
        vertical-align: top;

}


#v1 * {
        vertical-align: top;
        padding: 0;
        margin: 0;
}


</style>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#v1').css ({
  'position':'absolute',
  'margin-left':'-400px',
  'left': '50%',
  'width': '800px'
  });

});
</script>

</head>
<body>

<h1>Hello World</h1>
<h2>This is a test</h2>
<p>Blah blah blah.</p>

<div id="v1">
 <p><img src="http://piccies.flybywire.org.uk/Gliding/2004/20040905/MeFinishing3.jpg"></p>
 </p>
</div>

</body>
</html>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tdotCommented:
Hanknight
have you given this a try yet..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.