<!--container with jQuery UI draggable , resizable plugins applied-->
<div class="container">
<div class="stayInCenter" style="position:absolute;width:300px; height:400px">
I will aways stay in center of the container no matter where you are
and how big or small you become
</div>
</div>
Asked
Active
Viewed 1,063 times
2

Phrogz
- 296,393
- 112
- 651
- 745

qinHaiXiang
- 6,051
- 12
- 47
- 61
2 Answers
3
If you don't need vertical centering:
.container .stayInCenter {
position:relative; /* Not absolute */
margin:0 auto /* Make the left/right be centered */
}
If you do need vertical centering also:
.container .stayInCenter {
position:absolute;
/* Move to the center of the positioned parent */
left:50%; top:50%;
/* Give a fixed size */
width: 300px; height: 400px;
/* Move backwards by half the size, so the center is at 50% */
margin-left:-150px; margin-top:-200px;
}
Also, don't mix CSS with your HTML.

Phrogz
- 296,393
- 112
- 651
- 745
-
But with the JQuery UI draggable plugin applied ,the container's position will be set to absolute always! – qinHaiXiang Feb 10 '11 at 15:22
0
you put a fixed width, let's say 'width:500px'
and you put 'margin:auto'
if you need a top margin, you write eg. 'margin-top:100px' after the 'margin:auto'
should do the job

Mash
- 1,339
- 7
- 8