55

I have a simple table with 1 TD with vertical-align:middle;. This TD contains an Image :

table
{
  border:solid 1px red;
  width:300px;
}

td
{
  height:100px;
  vertical-align:middle;
  width:100%;
  border:solid 1px green;
}
img
{
  height:43px;width:43px;
}

span
{
  vertical-align:middle;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8/>
    <title>JS Bin</title>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <img src='http://static.jsbin.com/images/favicon.png'/>
        </td>
      </tr>
    </table>
  </body>
</html>

Everything is Ok and the IMG is vertical-aligned.

But If I add another elements after that Image ( a span for example ) :

table
{
  border:solid 1px red;
  width:300px;
}

td
{
  height:100px;
  vertical-align:middle;
  width:100%;
  border:solid 1px green;
}
img
{
  height:43px;width:43px;
}

span
{
  vertical-align:middle;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8/>
    <title>JS Bin</title>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <img src='http://static.jsbin.com/images/favicon.png'/>
          <span>aaa</span>
        </td>
      </tr>
    </table>
  </body>
</html>

Question

Doesn't the vertical align of the TD should vertical align all its childs ?

How can I make the span to be centered as well ?

NB

I don't want to add another TD , nor using float with padding/margin. IE8+.

edit:

Desired result :

enter image description here

MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

11 Answers11

81

Question
Doesn't the vertical align of the TD should vertical align all its childs ?

NO.
When you apply vertical-align to td, it is only applied to td, and is not inherited by any of its children.

If i have a TD with only span in it - it will vertical align. If I had a TD with only IMG inside it - it will also align.

This is because of the way vertical-align for td works. The total height of the cell i.e td is calculated and the whole cell is aligned vertically.

If there is a single img, then the height of td is same as that of img, so it seems that vertical-align for img is also middle. But actually, the td is vertically aligned to the middle with the img as vertical-align : baseline

Same is the case when there is a single span.

but if i have both - it doesn't. why is that ?

Because now, the height of td is the combined height of both img + span. So, actually, td is vertically aligned in the middle, but not img and span.

How can I make the span to be centered as well ?

You need to apply this CSS :

td > * {
    vertical-align : middle;
}

This will apply the CSS to all the children.

Check the JSFiddle for a better picture.

Hope, this answers your question.

Nikhil Patel
  • 1,761
  • 12
  • 23
  • Are you saying that in my first picture , the img is aligned to baseline ? according to this , if i have 2 different jsfiddle , 1 for img and 1 for span , they should overlap in the alignment... – Royi Namir Sep 24 '13 at 10:51
  • Yes, the `img` is aligned to baseline. You can inspect the `img` element for the `vertical-align` property ;) – Nikhil Patel Sep 24 '13 at 10:53
  • Well it is **not** aligned both to the baseline : http://i.stack.imgur.com/E0LmR.png – Royi Namir Sep 24 '13 at 10:53
  • Check my [JSFiddle](http://jsfiddle.net/vXfWV/). You can see the CSS property fetched through jQuery. – Nikhil Patel Sep 24 '13 at 10:55
  • And as for your picture, make sure you've reset all the `margin` and `padding`. Looks good to me though. – Nikhil Patel Sep 24 '13 at 11:00
  • Im getting crazy....why does this text is not aligned ? http://jsbin.com/AlapaNI/24/edit – Royi Namir Sep 24 '13 at 11:01
  • That is some weird problem with `webkit` based browsers. Try that in Firefox. Check this [thread](http://stackoverflow.com/questions/5880459/vertical-alignment-text-in-container-webkit-vs-firefox). – Nikhil Patel Sep 24 '13 at 11:17
  • You can write `vertical-align: middle` property by using `bootstrap` class `align-middle` as well – Eatsam ul haq Feb 07 '23 at 06:53
8

You can just use vertical-align: middle; to your span

img
{
  height:43px;width:43px;
  display: inline;
  vertical-align: middle;
}

span
{
  vertical-align:middle;
  display: inline;
  
}

your jsbin


As per comment


You may think as if td is given vertical-align: middle; then it should align all the contents inside this but having an image and a span in which browser is understanding the image is what? : is this inline or inline-block, so you need to set display: inline or inline-block; Then you may see its working only applying display property for image. demo

Edit

img tag : source: display inline vs inline-block

They are "block" elements in that they have a width and a height.

It's true, they are both - or more precisely, they are "inline block" elements. This means that they flow inline like text, but also have a width and height like block elements.

Also check this:

Replaced Elements

A replaced element is any element whose appearance and dimensions are defined by an external resource. Examples include images ( tags), plugins ( tags), and form elements (, , , and tags). All other elements types can be referred to as non-replaced elements.

Replaced elements can have intrinsic dimensions—width and height values that are defined by the element itself, rather than by its surroundings in the document. For example, if an image element has a width set to auto, the width of the linked image file will be used. Intrinsic dimensions also define an intrinsic ratio that’s used to determine the computed dimensions of the element should only one dimension be specified. For example, if only the width is specified for an image element—at, say, 100px—and the actual image is 200 pixels wide and 100 pixels high, the height of the element will be scaled by the same amount, to 50px.

Replaced elements can also have visual formatting requirements imposed by the element, outside of the control of CSS; for example, the user interface controls rendered for form elements.

In an inline formatting context, you can also think of a replaced element as being one that acts as a single, big character for the purposes of wrapping and layout. A width and height can be specified for replaced inline elements, in which case the height of the line box in which the element is positioned is made tall enough to accommodate the replaced element, including any specified box properties.

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
4

Here's a JS fiddle solution

fiddle

Here's the css

  table
{
  border:solid 1px red;
  width:300px;
}

td
{
  height:100px;
  vertical-align:middle;
  width:100%;
  border:solid 1px green;
}
img
{
  display: inline;
height: 43px;
width: 43px;
  position: relative !important;
float: left;
}

span
{
  height: auto !important;
width: 80%;
float: right;
position: relative;
vertical-align: middle !important;
text-align: left;
}
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
Anobik
  • 4,841
  • 2
  • 17
  • 32
  • Thankyou for the down vote as I can see a lot of people have been down voted by -1 . please try and provide a reason as it is suggested by SO. – Anobik Sep 24 '13 at 12:48
  • Probably because `vertical-align: middle;` is all you need on the `` class. I didn't downvote you, but it looks like you did too much work for something so simple. – Millhorn Sep 24 '13 at 13:49
  • tried my level best to bring it to center. Was actually firebugging the thing. So did not give a second look on what to keep . May be this will help if just some extra styles might be present :) – Anobik Sep 24 '13 at 14:34
  • Some one down voted every one serially . maybe :) Who cares just need a proper explanation . :) cause the answer is correct as far as fiddle says :) – Anobik Sep 24 '13 at 14:35
4

In all the cases, the vertical-align: middle; on the td does what is expected of it. That is, align the td to the center of that row and the entire contents of the td to the vertical middle (by default) leaving equal spaces at the top and the bottom.

Here is what the W3 Spec says about vertical-align: middle:

The center of the cell is aligned with the center of the rows it spans.

enter image description here

Row height calculation:

The height of a 'table-row' element's box is calculated once the user agent has all the cells in the row available: it is the maximum of the row's computed 'height', the computed 'height' of each cell in the row, and the minimum height (MIN) required by the cells.

In CSS 2.1, the height of a cell box is the minimum height required by the content. The table cell's 'height' property can influence the height of the row (see above), but it does not increase the height of the cell box.

Cell boxes that are smaller than the height of the row receive extra top or bottom padding.

As a result of the above, the height of the tr and the td becomes 100px but the cell box takes up only the amount of height required by the contents (img height = 43px). Now since the Cell box is smaller than the row height, extra padding is added like shown in Box 5 of the image above and thus makes the contents also get aligned to the middle.


TD has only image:

When there is only an img, the content height is equal to the height of the img. So it gets positioned to the middle.

enter image description here

As can be seen in the above image, this does not require a vertical-align: middle on the img explicitly because the td aligns its contents to the middle.


TD has only inline data:

When the td has only a span or span plus an inline div, the height of the content is equal to the default line-height for text (or any specified line-height). In this case also, the td aligns it correctly.

enter image description here

When the text content goes beyond the first line (refer to the demo), you can see that the td automatically pushes the first-line (marked in cyan background) upwards to ensure that the contents on the whole is aligned to the middle (not just a single line).


TD has an image and a span:

When we put an img and a span (inline text) within the td, the content height becomes equal to the height of the img plus the line-height of the second and subsequent lines.

In this situation, there are two possible cases as described below:

Case 1 - img tag has no vertical-align specified

In this case, the img is aligned to the baseline (default). Then the td aligns the entire content to the middle. This means the td leaves around 28.5px (= (100-43)/2) gap at the top and the bottom of the content. Again, the vertical-align on td does the job, it puts the contents in the middle (that is, leave equal gap on top and bottom). But the text gets pushed down because img height is more.

enter image description here

If we reduce the img height to less than the line height (say 10px), we can see that even with img + span it gets aligned to the middle.


Case 2 - img tag has vertical-align: middle

In this case also vertical-align on the td does the same as what it did for Case 1. However, the text in this case is near the middle because the img is also aligned to the middle of the line.

enter image description here

table {
  border: solid 1px red;
}
td {
  height: 100px;
  width: 200px;
  vertical-align: middle;
  border: solid 1px green;
}
img {
  height: 43px;
  width: 43px;
  border: solid 1px green;
}
.one td + td img {
  vertical-align: middle;
}
.three td + td img {
  height: 10px;
  width: 10px;
}
.four img {
  vertical-align: middle;
}
.five img + img{
  height: 50px;
  width: 50px;
}
td:first-line {
  background-color: cyan;
}
div {
  display: inline;
}
<table>
  <tr class='one'>
    <td>
      <img src='http://static.jsbin.com/images/favicon.png' />
    </td>
    <td>
      <img src='http://static.jsbin.com/images/favicon.png' />
    </td>
  </tr>
  <tr class='two'>
    <td>
      <div>aaa</div>
      <span>aaa</span>
    </td>
    <td>
      <div>aaa</div>
      <span>aaa aaaaaaa aaaaaaaa aaaaaaaa</span>
    </td>
  </tr>
  <tr class='three'>
    <td>
      Case 1
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span</span> 
    </td>
    <td>
      Case 1
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span</span> 
    </td>
  </tr>
  <tr class='four'>
    <td>
      Case 2
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span</span> 
    </td>
    <td>
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span + more text.......</span> 
    </td>
  </tr>
  <tr class='five'>
    <td>
      Case 3
      <img src='http://static.jsbin.com/images/favicon.png' />
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span text...</span> 
    </td>
    <td>
      <img src='http://static.jsbin.com/images/favicon.png' />
      <span>Image + Span + more text.......</span> 
    </td>
  </tr>  
</table>
Community
  • 1
  • 1
Harry
  • 87,580
  • 25
  • 202
  • 214
  • What about nikhil's answer which states that it is the td which is aligned and not its content – Royi Namir Sep 25 '13 at 18:38
  • Harry , your first long comment has a contradiction . There is td and content . No cell . Leave it alone it's just confusing .... Now - as far as i understood - the img stays still without any align ( default which is baseline) and the td itself is verticaly aligned to the max content (!!!)height of the td . Now am i right ? – Royi Namir Sep 26 '13 at 04:58
  • @RoyiNamir: The way I always use it is `td` = cell. I assume that is the same way Nikhil used it also judging from *height of the cell i.e td*. Coming to the second part. Yes, you are right. `vertical-align: middle` on `td` aligns contents of the `td` to the middle. So if for example `td` height is 100px, we have two lines of `20px` height each, we would have a 30px ((100-40)/2) space on top and bottom. The `img` or any other element within the `td` doesn't get aligned to the middle of the line that holds it unless `vertical-align` is set for it also. (Btw, thanks for the bounty :)) – Harry Sep 26 '13 at 05:33
  • You are doing it again :-). Settings vertical align middle to the td - aligns the td(!! Not its content!) to the middle of its max content height .Please confirm. What im saying is that there is a difference if im standing and the room aligns to my stumach ... Vs ... Im aligning to the middle height of my room – Royi Namir Sep 26 '13 at 05:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38070/discussion-between-harry-and-royi-namir) – Harry Sep 26 '13 at 05:57
3

Just add vertical-align:middle; to your img style?

Demo

Jazcash
  • 3,145
  • 2
  • 31
  • 46
2
<style>
table
{
  border:solid 1px red;
  width:300px;
}

td
{
  height:100px;  
  width:100%;
  border:solid 1px green;
  vertical-align:middle;
  line-height:100px;
}
img
{
  height:43px;width:43px;
  vertical-align:middle;
}
</style>
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
1

DEMO

just add this class:

td *{
  vertical-align:middle
}

Edit:

question is why when you add a pic to the td text goes bottom of pic and not any more middle of td.

this is my answer:

when you set td vertical-align to middle it should not set all content vertical-align to middle, they are still baseline. and when you add a pic to the text, the line height rise to the height of image and text is bottom of this height, so you need to set vertical-align to middle for fix this problem.

here you can see what i said: DEMO

and sorry about my bad english

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
  • you dont explain nothing and you answer to what i already know. read the question again. – Royi Namir Sep 24 '13 at 07:49
  • @RoyiNamir: the image's `vertical-align` default value is `baseline`. – Mohsen Safari Sep 24 '13 at 07:58
  • but i set vertical align to the td which should vertical aliugn all its content ! read my bounty description. – Royi Namir Sep 24 '13 at 07:59
  • @RoyiNamir no it is not true. a child just get the parent attribute value when default value is `inherit`. look at this: [DEMO](http://jsfiddle.net/AZGc7/5/) – Mohsen Safari Sep 24 '13 at 08:04
  • You dont understand. look at [this](http://jsfiddle.net/AZGc7/6/) this is pure img which is aligned. now look at [this](http://jsfiddle.net/AZGc7/7/) : this is 2 labels which also aligned. **together they are not**. – Royi Namir Sep 24 '13 at 08:07
  • You don't have to add an extra class to the stylesheet. It's not much work to do so, but it's not necessary. You simply need to add `vertical-align: middle;` to the '' class. – Millhorn Sep 24 '13 at 13:42
  • @webfrogs: if you want to set all content in the middle so you have to add extra class, for example two word with different `font-size`. look at this [link](http://jsfiddle.net/mohsen4887/AZGc7/10/) – Mohsen Safari Sep 24 '13 at 15:06
0

Is this what you mean? http://jsfiddle.net/JFVNq/

The reason is spans are treated as inline so you need to make them block.

CSS for the span:

td.vert span
{
    vertical-align: middle;
    display: block;
}
andrewb
  • 2,995
  • 7
  • 54
  • 95
0

I think we are all nearly there but

td img,
td span {
display:inline-block;
vertical-align:middle;
}

seems to work.

Codepen Example

Or am I missing something?

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

This CSS attribute doesn't go on any other kinds of elements. When the novice developer applies vertical-align to normal block elements (like a standard ) most browsers set the value to inherit to all inline children of that element.

You simply need to add vertical-align: middle to the <img> class.

Your CSS should look like this...

table
{
  border:solid 1px red;
  width:300px;
}

td
{
  height:100px;
  vertical-align:middle;
  width:100%;
  border:solid 1px green;
}
img
{
  height:43px;width:43px;
  vertical-align: middle;
}

You can view the Sample Fiddle...

Millhorn
  • 2,953
  • 7
  • 39
  • 77
-1

Just move your vertical-align: middle from the span to the image.

The image will align itself on the text; works better than the opposite ;)

img
{
  height:43px;width:43px;
  vertical-align:middle;
}

http://jsfiddle.net/ZnpNF/1/

hvgeertruy
  • 198
  • 6