2013-12-18 44 views
0

我有一個頁面的標題,在它的左側是一個div,它位於左側,並使用引導程序3進行響應。我有一個圖像,然後一些文本彼此相鄰。因爲他們響應,他們並不總是在包含div的垂直中心。我試圖使用jquery來對齊它們,如下所示:垂直居中引導3容器與jquery

var userImg_pos = function() { 
    $(".lp-shell-head-user-img").css({ 
     marginTop: ($(".lp-shell-head-user-img").height() * ".5") * -1 
    }); 
    $(window).resize(function() { 
     $(".lp-shell-head-user-img").css({ 
      marginTop: ($(".lp-shell-head-user-img").height() * ".5") * -1 
     });     
    }); 
} 
userImg_pos(); 

但它似乎沒有工作。我做了一個小問題來展示它。你可以在左邊看到綠色的正方形和文字,並且他們兩人都在半容器的上方移動,這對我來說沒有意義,因爲在他們的CSS中他們都有頂尖:50%。

這裏是小提琴,在這個任何見解將不勝感激:http://jsfiddle.net/cDz79/1/

+0

https://github.com/PaulSpr/jQuery-Flex-Vertical-Center – Christina

回答

1

爲了定位與top: 50%元素(或任何其它位置),您必須設置position: absolute;在CSS中該元素本身和position: relative;在父母的CSS中。

#imgparent { 
    border:1px solid blue; 
    position: relative; 
} 
.lp-shell-head-user-img { 
    position: absolute; 
} 

http://jsfiddle.net/mblase75/7TXqg/