Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align 2 different sizes of text?

I know to vertically align text to the middle of a block, you set the line-height to the same height of the block.

However, if I have a sentence with a word in the middle, that is 2em. If the entire sentence has a line-height the same as the containing block, then the larger text is vertically aligned but the smaller text is on the same baseline as the larger text.

How can I set it so both sizes of text are vertically aligned, so the larger text will be on a baseline lower than the smaller text?

like image 248
JD Isaacks Avatar asked Oct 22 '10 14:10

JD Isaacks


People also ask

How do you align text vertically?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How do you vertically align elements?

If the inner element can have a fixed height, you can make its position absolute and specify its height , margin-top and top position. jsfiddle example. If the centered element consists of a single line and its parent height is fixed you can simply set the container's line-height to fill its height.

How do I align multiple images vertically?

Answer: Use the CSS vertical-align Property You can align an image vertically center inside a <div> by using the CSS vertical-align property in combination with the display: table-cell; on the containing div element.


2 Answers

Try vertical-align:middle; on inline containers?

EDIT : it works but all your text must be in an inline container, like this :

    <div style="height:100px; line-height:100px; background:#EEE;">          <span style="vertical-align:middle;">test</span>          <span style="font-size:2em; vertical-align:middle;">test</span>      </div>
like image 158
MatTheCat Avatar answered Sep 23 '22 19:09

MatTheCat


The functionality you are seeing is correct because the default for "vertical-align" is baseline. It appears that you want vertical-align:top. There are other options. See here at W3Schools.

Edit W3Schools has not cleaned up their act and still, appear, to be a shoddy (at best) source of information. I now use sitepoint. Scroll to the bottom of the sitepoint front page to access their reference sections.

like image 34
DwB Avatar answered Sep 25 '22 19:09

DwB