Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical-align doesn't work properly on not capitalized text

Tags:

html

css

I'm trying to vertical align, with pixel perfection in mind, two texts.

One is completely capitalized, the other is not.

http://codepen.io/FezVrasta/pen/EjxJoz

div {
  outline: 1px solid red;
  height: 31px;
  width: 400px;
  float: left;
  line-height: 31px;
}
span {
  display: inline-block;
  vertical-align: middle;
}

As you can see, the capitalized text is perfectly centered (20px above and 20px below).

The other one, has 26px above, and 19px below.

I think it always centers the text assuming the higher possible character as center... but I'd like to center the current text considering only the used characters.

Is there a way?

NB: Using CSS tables will not fix the problem.

like image 737
Fez Vrasta Avatar asked Apr 24 '15 11:04

Fez Vrasta


People also ask

Does vertical-align work for text?

The vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.

Why text-Align Center doesn't work?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

How do you vertically align text in a block element?

Use the CSS vertical-align property The vertical-align property is used to vertically center inline elements. The values of the vertical-align property align the element relative to its parent element: Line-relative values vertically align an element relative to the entire line.


1 Answers

Each browser only sets one baseline for the font. It doesn't care if you're using capitals or not. So naturally not every letter can be exactly centered because they have different heights and maybe descenders.

Vertical alignment is more a "close to"-value. When you set vertical-align: top; not all the letters will align to the top and bump the top-border, just the baseline of the font does.

like image 167
zork media Avatar answered Oct 04 '22 18:10

zork media