2

Please run the demo:

* {
  margin:0;
  padding:0;
}
.body {
  font-family:Microsoft Yahei;
  font-size:16px;
  background-color:lightblue;
  height: 200px;
  width:200px;
  line-height:2;
}
.body span {
  background-color:pink;  
}
.body img {
  width:50px;
  height:50px;
}
.body .img-wrapper {
  background-color:orange;
}
.body .img-wrapper {
  vertical-align:middle;
} 
<div class="body">
  <span class="wrapper">
      words-g words-g words-g
    <span class="img-wrapper">
      <img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
        s
    </span>
    words-g words-g words-g
  </span>
</div>

The point is that I set

.body .img-wrapper {
      vertical-align:middle;
} 

I was expecting the red lines in below picture is in the same line: enter image description here

According to the specification,

Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.

So,I think:

  • the vertical midpoint of the box is the first red line in the above picture and
  • the baseline of the parent box plus half the x-height of the parent = the second red line

But it turns out I am wrong and I guess the key is x-height of the parent.So,I found this: enter image description here

So, I thought x-height of the parent in this case is not the second red line because of the existence of the image.

So,my question is :

  • how much is the x-height of the parent in this case? Is it changed because of the existence of the image?

  • Or something else is wrong?

Please notice:

  • I just want to get the x-height value in this case,so I can understand the vertical-align better.

  • I am not asking for a specific solution.

Whatever thanks for your help!

Community
  • 1
  • 1
xianshenglu
  • 4,943
  • 3
  • 17
  • 34
  • what do you want? I am clear about this. – patelarpan May 18 '18 at 09:01
  • @patelarpan,how much is the x-height of the parent in this case? Is it changed because of the existence of the image? – xianshenglu May 18 '18 at 09:01
  • possible that I am totally wrong but give `.body img { width: 50px; height: 50px; vertical-align: middle; }` – patelarpan May 18 '18 at 09:05
  • @patelarpan,I just want to get the `x-height` value in this case,so I can understand the `vertical-align` better – xianshenglu May 18 '18 at 09:08
  • To know the x-height you need to look at the font metrics of the Microsoft Yahei font. The img element does not affect it. – Alohci May 18 '18 at 09:21
  • check this : https://stackoverflow.com/a/34952703/8620333 and no need to know the x-height to understand alignment – Temani Afif May 18 '18 at 09:37
  • @TemaniAfif - I don't see that as relevant to the question at all. If the OP wants to fully understand vertical-align:middle with a non-replaced inline element, then they have to understand what the x-height is. – Alohci May 18 '18 at 09:46
  • @Alohci it's not about the question or the answer I shared, it's more about the figure in that answer that explain very well the different values of vertical alignment visually ... then he will maybe notice that the x-height is the ex unit, etc ... and *yes* they have to understand what is x-height but no need its value [again not using the correct wording] – Temani Afif May 18 '18 at 09:51
  • @TemaniAfif. I see. Fair enough. – Alohci May 18 '18 at 09:55
  • @Alohci I guess it worth an answer so my point is better explained than this bad comment :p – Temani Afif May 18 '18 at 10:05

1 Answers1

2

First the x-height of the element is not affected by the the image and is only defined by font-size and of course the font-family used. Then in order to get the value of the x-height you need to consider the ex unit.

Here is a better illustration taken for this answer

Location of the baseline on text

You may clearly see the difference between each value of vertical alignment and also notice the illustration of em and ex unit. Now in order to have the exact value of x-height, you simply need to use the ex unit.

Here is an example:

* {
  margin:0;
  padding:0;
}
body {
  font-family:Microsoft Yahei;
  font-size:16px;
  background-color:lightblue;
  line-height:2;
}
span {
  background-color:pink;  
  border-right:1ex solid red;
  border-left:1em solid red; 
}
img {
  width:50px;
  height:50px;
}
<span>
    words-g words-g words-g
</span>
<br>
<span>
    words-g words-g words-g <img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
</span>

As you can see I added a right and left border using ex and em units then if I check the computed value I can get the exact value. You will aslo notice that both span have the same value which indicate that the image has no impact on it.

enter image description here

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Thanks for let me know about `ex` unit.Well,it turns out,`vertical midpoint of the box is not the first red line`?It seems that the calculation of `vertical midpoint of .img-wrapper` doesn't include the image? – xianshenglu May 18 '18 at 11:24
  • @xianshenglu I would say yes and the hint is the background. As you can see the orange background is not covering the height of the image because the height of the wrapper (the span element) is defined by considering line-height and font properties. To better see this, adjust the line-height and see ... and you will conclude that the image has absolutely no impact all these stuffs ;) – Temani Afif May 18 '18 at 11:31
  • @xianshenglu you may have a look at this to better understand how it's done : https://stackoverflow.com/a/50263412/8620333 – Temani Afif May 18 '18 at 11:33