I know this probably seems like a little problem. It just feels like there should be an easy solution. I have a feeling I'm SOL but I figured I'd ask first.
http://jsfiddle.net/bczengel/FSDz9/5/
When the image is not floated left it has the desired end result where the image and the text are both vertically centered. When you float the image left the image is vertically centered and the text is aligned with the top of the image.
Select the text that you want to center, and then click Paragraph on the Format menu. On the Indents and Spacing tab, change the setting in the Alignment box to Centered, and then click OK.
To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.
Use display: inline-block and vertical-align: top to Place the Text Next to an Image in HTML. We can use the display and vertical-align properties to place a text next to an image in HTML.
with a minimal html change (wrapping your title text in a <p>
)
table {
width: 500px;
border: solid 1px black;
}
table td {
text-align: center;
vertical-align: middle;
padding: 5px;
position: relative;
}
table td img {
width: 50px;
vertical-align: middle;
display: inline-block;
}
table td p {
display: inline-block;
width: 430px;
background: #ccc;
vertical-align: middle
}
<table>
<tr>
<td>
<img alt="test" src="http://t1.gstatic.com/images?q=tbn:ANd9GcQ-nkjB-srUT_dc7brfRDiDSVu8BQa_6TEbQYevP1gy2UkTdcXWzg" />
<p>Title Text</p>
</td>
</tr>
</table>
<br /><br />
<table>
<tr>
<td>
<img alt="test" src="http://t1.gstatic.com/images?q=tbn:ANd9GcQ-nkjB-srUT_dc7brfRDiDSVu8BQa_6TEbQYevP1gy2UkTdcXWzg" />
<p>Title Text <br /> on the next line</p>
</td>
</tr>
</table>
You could use the negative margin method to vertically center the text. To do that you could put your text in a <span>
and have the following css:
table
{
width: 500px;
border: solid 1px black;
}
table td
{
text-align: center;
vertical-align: middle;
padding: 5px;
position: relative;
}
table td img
{
width: 50px;
vertical-align: middle;
}
table.float td img
{
float: left;
}
table.float td span
{
position: absolute;
top: 50%;
margin-top: -10px; /* or half of the height of your text */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With