1

I'm with a little problem.

I have the container div for some elements which are images, inside other divs. Something like:

<div id="container">
    <div id="draggable">
        <img src="something">
    </div>
</div>

I need to center the container verticaly, but i can't use the top: -healfHeight; margin-top: 50%; because then the jquery ui drag won't work. So any other idea of how can i solve this? :(

JLFerrari
  • 155
  • 3
  • 16

4 Answers4

1

Try setting the parent container to position: relative, and the element you want to center to margin: auto.

mkaito
  • 1,005
  • 1
  • 10
  • 19
0
var wh = $(window).height();
var ch = $('#container').height();
var mt = (wh - ch) / 2;
$('#container').css('margin-top', mt)

http://jsfiddle.net/zJrKh/1/

Ram
  • 143,282
  • 16
  • 168
  • 197
0

I'm not sure how jquery positioning works, but if your answer involves only position on these divs with CSS, then:

Make the container div position:absolute; top:50%; left:50%;

Then your 'draggable' div needs this positioning: position:relative; top:(-half the total height of the div); left:(-half the width of the div);

For examples, if your draggable div is 100px by 100px, then the top would be -50px and the left would be -50px.

smilebomb
  • 5,123
  • 8
  • 49
  • 81
0
<div id="container">
    <div id="draggable">
        <img src="something">
    </div>
</div>

Marke sure the parent width is wider than the draggable element, then try:

#draggable{display:block;margin:0 auto}

Edit, sorry, just realised you need it centered vertically.

Try:

#container{display:table-cell}
#draggable{vertical-align:middle}
ConorLuddy
  • 2,217
  • 2
  • 19
  • 18