Reputation: 4043
I'm displaying an icon in a cell below some text, the exact length/height of which is not known in advance (based on user input, or translations), and want it to be vertically centered between the text and the bottom of the cell.
Getting it aligned to the center of the cell is easy (vertical-align: center
, absolute positioning, etc.), but getting it to center below the text, not so much. To visualize what I mean:
The red lines are to find the center of the cell, which is where we can get it. The blue line is the bottom of the text, and where the green lines cross is where we want it to be.
Sample of what I have so far: http://plnkr.co/edit/RIaXLzPtQiqLK9XBcmD0
Things that aren't making this easy:
Is there a way to get the icon vertically centered between the bottom of the text, and the bottom of the table cell, given the lack of knowledge of the size of elements around it?
Upvotes: 2
Views: 163
Reputation: 1410
Using jQuery and some math, this may work for you: jsFiddle (I'm much more used to fiddle than the other site).
jQuery:
$(document).ready(function(){
//get total height of cell
var padding = $('.cell').height();
//subtract height of title text and top 20px
padding = padding - $('.titleText').height() - 20;
//split in half for middle
padding = padding/2;
$('.iconContainer').css('padding-top', padding + 'px');
});
and the HTML:
<table>
<tr>
<td>
This is a<br />
tall cell<br />
to<br />
make<br />
the<br />
other<br />
tall
</td>
<td class="cell">
<div class="titleText">Title Text</div>
<div class="iconContainer"><div class="icon">
</div></div>
</td>
</tr>
</table>
and CSS:
table {
width: 400px;
}
td {
width: 50%;
border: thin solid black;
position: relative;
}
.titleText {
position: relative;
top: 20px;
width: 100%;
text-align: center;
text-transform: uppercase;
font-size: 20px;
}
.cell
{
vertical-align: top;
}
.icon {
background-image: url( 'http://icons.iconarchive.com/icons/tinylab/android-lollipop-apps/32/Skala-icon.png' );
width: 32px;
height: 32px;
margin: auto;
}
EDIT: Using only Javascript, here is another solution (same principle): http://plnkr.co/edit/i1m1lulL7OgAe0ykY6eX?p=preview
Upvotes: 1