jquery - dynamically positioning a absolute element related to another element -


i have particular reason position logo on banner , logo has postion:absolute, when resize browser window have alignment problem, far figured out solution jquery goes this:

$(document).ready(function(){      alert($(document).height());     var lopos = $('.banner1').children('img').height()/2;     $('.logo').children('img').css('top', lopos); });  

the banner image has max-width:100% , using jquery's height() height , divide 2 can use position logo on top. sucessful far problem happens on time, , when rezize browser dont see values change.

is there way can make code work on window size change?

i not sure understand question correctly, may suggest 'javascript free' solution.

provided html looks this:

<div id='banner'>     <img src='http://placehold.it/1500x300'/>      <div id='logo'>logo</div> </div>  

you position logo on top of banner img this:

#banner {     position: absolute; } #banner img {     max-width: 100%;     display: block; } #logo {     height: 50px;     width: 50px;     background: #cff;     position: absolute;     left: 20px;     top: 50%;      z-index: 1;     margin-top: -25px; } 

this assuming logo has fixed size know in advance, suppose safe assumption...

the 'magic' happens in 2 last lines of css. setting top 50% make top of logo line center of banner. banner has same size image contains, means logo lined center of banner image. setting top margin on logo, negative half height, push 50%, , align center of logo center of banner image.

i hope makes sense. feel free ask if want further explanation, or post html/css if want me make more concrete use case.

i have set small fiddle demonstrate: http://jsfiddle.net/pepsz/


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -