How to align a label to the “BOTTOM” of a div in CSS?

后端 未结 8 2180
太阳男子
太阳男子 2020-12-03 06:08

DEMO can be found at:

http://www.bootply.com/VZ7gvA7ndE#

I set the height of div to 100px and want to show the label at the bottom

相关标签:
8条回答
  • 2020-12-03 06:37

    vertical-align only applies to table cells. If you want to position an element at the bottom of its parent, you need to give it position: absolute; and bottom: 0;.

    0 讨论(0)
  • 2020-12-03 06:41

    set it as position:absolute;

    #contain-word-lab {
      position:absolute;
      bottom:5px;
    }
    
    0 讨论(0)
  • 2020-12-03 06:46

    Apply position:relative to the parent div and make your label as position:absolute.

     #contain-word-div {
     height: 100px;
     position:relative;
     }
     #contain-word-lab {
      position:absolute;
      bottom:5px;
     }
    

    DEMO

    0 讨论(0)
  • 2020-12-03 06:49

    quick example

    http://jsfiddle.net/oa2gmuq3/

    if you add

    position:absolute;
    bottom:0px;
    

    to the label it likes to keep it at the bottom

    0 讨论(0)
  • 2020-12-03 06:56

    Why vertical-align: bottom is not working alone

    Since the height of the parent element is greater than the computed height of the label, Using vertical-align: bottom won't move that (inline) element to the bottom of the parent.

    Because in an inline flow, vertical-align determines how the elements are positioned based on their parent's baseline; And using that property on the label won't alter the position of baseline of its parent.

    Inline level elements (inline, inline-block) are sitting in their baseline by default. And if they have different heights, the tallest element will determine where the others whould be placed.

    I.e. In an inline flow, the tallest element will affect/move the baseline of the parent:

    illustration of baseline

    Looking for a solution

    Hence in cases where the parent has an explicit height, if we could have an inline child which has the exact same height as the parent (a full-height child), it would affect the inline flow and move the baseline down:

    A full-height element which affects the baseline

    And in order to keep elements (including letters having descenders) within the parent, we should align them vertically by vertical-align: bottom; declaration.

    applying vertical align property

    10.8 Line height calculations: 'vertical-align' property

    baseline
    Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.

    bottom
    Align the bottom of the aligned subtree with the bottom of the line box.

    Putting it all together

    Therefore you could create a full-height element (Personally I'd rather go with pseudo-elements) within the parent to align the label at the bottom.

    EXAMPLE HERE

    #contain-word-div:after {
      content: "";
      display: inline-block;
      height: 100%;            /* Let it be as height as the parent */
      vertical-align: bottom;  /* Align the element at the bottom   */
    }
    
    #contain-word-lab {
      display: inline-block;
      vertical-align: bottom;  /* Align the element at the bottom   */
    }
    
    0 讨论(0)
  • 2020-12-03 06:57

    Try to do following

    FIDDLE DEMO

    #contain-word-lab {
        vertical-align='bottom';/***Remove This***/
        bottom:2px;
        position:absolute;
    }
    
    0 讨论(0)
提交回复
热议问题