Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

  • Related entries

    No related content found.

    • Archives

    • Recent comments

    Vertically align text to bottom within div

    19th February 2007

    I was looking to solve this extremely common problem – when you need to vertical-align:top/middle/bottom something within the DIV element. So far not a single solution fits my exact case, but I collected different solutions here for convenient reference.

    • vertical-align CSS property should be applied to the inline element within your DIV (which is a block element), and NOT to the DIV itself. For this to work, you may need to set line-height and font-size to the height of the enveloping DIV element:
      1. <div>
      2. <img src="/images/demoBoxH.gif" alt="" />
      3. </div>
      1. div{
      2.     height:200px;
      3.     width:200px;
      4.     background:blue;
      5.     text-align:center;
      6.     line-height:200px;
      7.     font-size:200px;
      8. }
      9. *>div{
      10.     font-size:12px
      11. }
      12. img{
      13.     vertical-align:middle
      14. }
    • there is a different approach: for the element within your DIV, set display:absolute and bottom:0px (or top:0px) to vertically align to bottom or top, respectively. Note that this approach doesn’t let aligning to “middle”, and that it works best when only one block-level element is aligned – otherwise elements will overlap at the bottom/top of container DIV. Container DIV should be set to position:relative.
    • another approach: we can set the container DIV to
      1. div {display:table-cell;vertical-align:middle}

      because for table cells vertical-align property works as one might expect from common sense and the text-align property. Basically, by doing so we ask the browser to treat our DIV as a table-cell, where vertical-align works just perfectly. However, this approach appears not to work on most IE browsers (probably except for IE7 only, but I didn’t try, as I need IE6 support as well).

    • one more table-cell-display-based solution by Nikola:
      1. .nav_next a {
      2. display: table-cell;
      3. width: 85px;
      4. text-align: center;
      5. padding-top: 60px;
      6. position: relative;
      7. top: 60px;
      8. margin-top: -60px;
      9. }

      This time, display:table-cell is applied to the immediate text container (anchor in the example), and then padding-top is added to push text to the bottom. Should work in FF3 and IE6; in FF3, this solution worked in a test setup without the last 3 CSS attributes, but YMMV.

    • vertical alignment can be simulated with margins, applied to the element(s) within the container DIV. This is a simple approach and I would recommend it, if the heights of the container and elements within it are known (otherwise, this method is of no help). Example application of this method: if you have a DIV container 200px high, and need to position some SPANed text within it; first, set SPAN to
      1. span {display:block; height:20px}

      (or whatever height value fits your requirements), and then set margins to vertically align the SPAN. To align your SPAN to top, you would apply

      1. span {display:block; height:20px;margin-bottom:180px}

      to your SPAN; to align to bottom –

      1. span {display:block; height:20px;margin-top:180px}

      to align in the middle –

      1. span {display:block; height:20px;margin:90px 0px}


    (The problem I have involves vertically aligning two DIVs within a container DIV so that DIV1 is aligned to top, and DIV2 is aligned to bottom, and their content is aligned appropriately to top/bottom, and that it works in at least IE6, IE7, FF2. If anyone has a ready solution – please comment. When I find mine, I’ll add it to the list above.)


    5 Responses to “Vertically align text to bottom within div”

    1. Nikola Says:

      try this one and be free to simplify it and publish on your site appropriate version as I’m in a hurry. Works in ff3 ie6. I didn’t tested other browsers.
      .nav_next a {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 10px;
      color: #666;
      text-decoration: none;
      display: table-cell;
      height: 16px;
      width: 85px;
      border-right-width: 1px;
      border-left-width: 1px;
      border-right-style: solid;
      border-left-style: solid;
      border-right-color: #333333;
      border-left-color: #333333;
      position: relative;
      text-align: center;
      float: right;
      vertical-align: bottom;
      margin-top: -60px;
      padding-top: 60px;
      top: 60px;

    2. Bogdan Says:


      Thank you for the grant of rights for publishing.

      If we remove everything unrelated to positioning (in FF3 only), we are left with the following CSS code:

      .nav_next a {
      display: table-cell;
      width: 85px;
      text-align: center;
      margin-top: -60px;
      padding-top: 60px;
      top: 60px;

      { “height” is omitted because of the margin/padding/top attributes of 60px, which renders height=16px irrelevant (at least in FF3); “width”, however, influences the display, so it is left in. “vertical align” also had no effect in FF3, and should have no effect in IE6, so I removed it. }

      Let’s have a look at what is going on.

      By applying display:table-cell, you make the anchor element behave like a table-cell. This, in turn, enforces proper width of the element, and makes all of margin/padding/top attributes exhibit desirable effects in FF3.

      An equivalent, which works just the same in FF3, is to set display:block and add position:relative (which isn’t necessary for FF3 with display:table-cell).

      Applying padding-top:60px pushes the anchor’s text to the bottom, and text-align:center centers text. Negative margin-top:-60px looks like elements flow fix (had no effect in my test file with only two elements; might also be IE6-specific).

      So the minimal working version might be:

      .nav_next a {
      display: table-cell;
      width: 85px;
      text-align: center;
      padding-top: 60px;
      position: relative;
      top: 60px;
      margin-top: -60px;

      with the last three attributes unnecessary for FF3.

      Let’s label this solution as “display:table-cell extended” :)

    3. itilv3 Says:

      Try this as it may do the trick. You can apply css styling as appropriate. Only apply colours, etc. to the DIV. Only tested and confirmed that is works on IE7 & FF3. Also solves another problem in that if your text size is increased the table does not overflow as it would if it were a table alone.

      1. <DIV id="adiv">
      2. <TABLE WIDTH="100%" HEIGHT="100%" cellpadding="0" cellspacing="0">
      3. <TR>
      4. <TD valign="center">
      5. YOUR TEXT
      6. </TD>
      7. </TR>
      8. </TABLE>
      9. </DIV>
    4. Sky Says:

      div {display:table-cell;vertical-align:middle}
      trick often doesn’t work for some unknown reason

    5. Bogdan Says:


      I believe some contexts and/or CSS attribute combinations make “table-cell” display type unusable; I might have seen something about it at the time of posting.
      If you have a fragment of code where “table-cell” doesn’t work – please post it here.

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>