Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align image and text vertically within TD element?

<td>
<img src="http://blog.garethjmsaunders.co.uk/wp-content/feed-icon-16x16.gif"/>
 My feed
 </td>

This is how it looks like:


(source: garethjmsaunders.co.uk)
My feed

The icon and the text is misalligned vertically. The icon is on the top of the table cell, the text is on the bottom. Both the text and the icon occupy 16 pixel but the cell still eats up 19. How can I align them to save those 3 pixels?

like image 419
Mr. Lame Avatar asked Jan 15 '09 16:01

Mr. Lame


People also ask

How do I vertically align text in TD tag?

HTML | <td> valign Attribute The HTML <td> valign Attribute is used to specify the vertical alignment of text content in a cell. Attribute Value: top: It sets the content to top-align. middle: It sets the content to middle-align.

How do I align text and image vertically in CSS?

Using flex property in css.To align text vertically center by using in flex using align-items:center; if you want to align text horizontally center by using in flex using justify-content:center; .

How do I align text and an image in the same line in HTML?

Using the float property of CSS will allow you to place an image and text on the same line without breaking the line break. Or Alternatively, you should use the flexbox method of CSS that uses the flex property to make sure that images and lines are aligned in the same line.


2 Answers

Well, if you choose the background image method, then it is very simple:

background: url(feed.png) left center no-repeat
like image 183
Joey Avatar answered Oct 17 '22 21:10

Joey


The image is aligning to the base line of the text, this does not include the descender height which is the 'tick' in letter like g or y.

If the height of the row/cell is to be fixed, you can add line-height to get it to vertically centre. So for instance, assuming your cell is 16px high:

td.feed {
    line-height:16px;
}

The other option would be to add the icon as a background image, adding padding-left to the cell:

td.feed {
    background: transparent url(/wp-content/feed-icon-16x16.gif) no-repeat left center;
    padding-left: 18px; /* width of feed icon plus 2px spacing */
}

The second one would mean you could remove the need for tables at all, now there's an idea...

like image 36
roryf Avatar answered Oct 17 '22 22:10

roryf