I have a DIV where I want the text to center vertically within that div. I can't use fixed pixel heights, only percentages.
<div style="position:relative; width: 100%; height: 100%;">
<img src="images/thumb-member-placeholder.gif" alt="" class="myImage" />
<div class="myText" style="display: inline-block; position: absolute; z-index:500; text-align:center;">Image Text</div>
</div>
I have 100 of these divs, where "myText" can be 1 line or 3 lines.
So I am using jquery to detect the height and set the TOP value accordingly:
$(window).load(function() {
var imageHeight = $('.myImage').height();
$('.myText').each(function() {
var textHeight = $('.myText').height();
var vertAdj = ((imageHeight - textHeight) / 2);
$(this).css('top', vertAdj);
});
});
But it is not working. It outputs the same TOP value all the time, and does not detect the height of "myText" when the text goes to 2 or 3 lines.
Any suggestions?
var textHeight = $('.myText').height();get the value of the first element in the stack, hence why it is all the same.var textHeight= $(this).height();.