Vertically align text to the bottom of the box?
Asked Answered
B

4

11

I made box and I set line-height, the text is automatically vertically center. Is there a way or any kind of trick to set the text on the bottom of the box?

div {
    width: 100px;
    height: 100px;
    background: #eee;
    color: #333;
    text-align: center;
    line-height: 100px;
    vertical-align: text-bottom;
 }

<div>FoxRox</div>
Bloodcurdling answered 25/5, 2012 at 19:8 Comment(1)
You could change the line height to something around 180px. Vertical-align is only for inline elements.Madelynmademoiselle
I
21

2020 Update

You can use CSS grid, flexbox or the original method with line-height:

body { display: flex } /* just to prettify */

div {
  margin: .5em;
  width: 6.25em; height: 6.25em;
  background: #eee;
  color: #333;
  text-align: center
}

.grid {
  display: grid;
  align-content: end;
}

.flex {
  display: flex;
  align-items: flex-end;
  justify-content: center
}

.lh { line-height: 11.5 /* 6.25*2 - 1.5 */ }
<div class='grid'>Hello</div>

<div class='flex'>Hello</div>

<div class='lh'>Hello</div>

Setting the height of the div and the line-height of the text to the same value, 100px in your case, is a method of vertically centering the text within the div. That's the problem.

The solution is to change line-height to twice the height minus the size of the text and remove useless vertical-align.

Inbreathe answered 25/5, 2012 at 19:13 Comment(4)
That's part of the problem. The content has to be inside an inline-block for vertical-align to work.London
I liked how you solved it by just using line-height. Smart! thnx a lot, saved me a lot of markup!Bloodcurdling
Can only imagine what wisdom that broken link once might have containedDeepseated
Hi @Ana, Can you please retrieve you thoughts that you pasted into the broken link. An up-voted answer going to waste right now.Jetta
C
9

Enclose the text in a p tag with display:inline-block. Set vertical-align to the p element.

<div>
    <p>FoxRox</p>
</div>​

div {
    width: 100px;
    height: 100px;
    background: #eee;
    color: #333;
    text-align: center;
 }
p {
    display: inline-block;
    vertical-align: -80px;
}
​

Demo

Crosscrosslet answered 25/5, 2012 at 19:15 Comment(0)
P
5

You could set display to table-cell, try this CSS for example.

width: 100px; 
height: 100px; 
display: table-cell; 
vertical-align: bottom;

Demo: http://jsfiddle.net/Kawwr/

Pompadour answered 25/5, 2012 at 19:17 Comment(2)
Spot on, and not as complicated as the other answers would lead you to believeDigiacomo
This won't work if the position is defined. It works only with position relative.Planogamete
E
2

You could check out my answer to https://mcmap.net/q/348113/-how-to-vertically-align-floating-divs-to-the-bottom.

Here is the demo for the above answer.


The trick is to use display: table-cell on the outer container. That way you can use the vertical-align: bottom and display: inline-block; on the div.

Escalate answered 25/5, 2012 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.