Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

centering text vertically and horizontally in same table cell with image floated left

Tags:

html

css

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/

Problem:

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.

Requirements:

  • One table cell
  • CSS only with the given html (or minimal changes to the html)
  • I would like to avoid a line-height solution because the stylesheet is applied to many existing pages some of which have multiple lines in the text
like image 550
Brian Zengel Avatar asked Jan 13 '12 20:01

Brian Zengel


People also ask

How do you center text vertically and horizontally in a table?

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.

How do you center an element both vertically and horizontally?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I align text and images side by side in HTML?

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.


2 Answers

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>
like image 94
Zoltan Toth Avatar answered Oct 03 '22 05:10

Zoltan Toth


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 */
}
like image 21
user1133972 Avatar answered Oct 03 '22 05:10

user1133972