Put text at bottom of div

前端 未结 5 1616
灰色年华
灰色年华 2020-12-02 07:10

I have 4 DIV\'s right next to each other, and they\'re all centered in the middle of the screen. I have 2 words in each div, but I don\'t want them at the top, I want them t

相关标签:
5条回答
  • 2020-12-02 07:38

    I think that's better to use flex boxes (compatibility) than the absolute position. Here's example from me in pure css.

    .container{
      background-color:green;
      height:500px;
      
      /*FLEX BOX */
      display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-flex-direction: column;
        -ms-flex-direction: column;
        flex-direction: column;
        -webkit-flex-wrap: nowrap;
        -ms-flex-wrap: nowrap;
        flex-wrap: nowrap;
        -webkit-justify-content: flex-start;
        -ms-flex-pack: start;
        justify-content: flex-start;
        -webkit-align-content: stretch;
        -ms-flex-line-pack: stretch;
        align-content: stretch;
        -webkit-align-items: flex-start;
        -ms-flex-align: start;
        align-items: flex-start;
    }
    
    .elem1{
      background-color:red;
      padding:20px;
      
       /*FLEX BOX CHILD */
     -webkit-order: 0;
        -ms-flex-order: 0;
        order: 0;
        -webkit-flex: 0 1 auto;
        -ms-flex: 0 1 auto;
        flex: 0 1 auto;
        -webkit-align-self: flex-end;
        -ms-flex-item-align: end;
        align-self: flex-end;
     
    }
    <div class="container">
     TOP OF CONTAINER 
    <div class="elem1">
      Nam pretium turpis et arcu. Sed a libero. Sed mollis, eros et ultrices tempus, mauris ipsum aliquam libero, non adipiscing dolor urna a orci.
    
    Mauris sollicitudin fermentum libero. Pellentesque libero tortor, tincidunt et, tincidunt eget, semper nec, quam. Quisque id mi.
    
    Donec venenatis vulputate lorem. Maecenas ullamcorper, dui et placerat feugiat, eros pede varius nisi, condimentum viverra felis nunc et lorem. Curabitur vestibulum aliquam leo.
    </div>
    
    </div>

    0 讨论(0)
  • 2020-12-02 07:39
    <div id="container">
        <div><span>Two Words</span></div>
        <div><span>Two Words</span></div>
        <div><span>Two Words</span></div>
        <div><span>Two Words</span></div>
    </div>
    
    #container{
        width:450px;
        height:200px;
        margin:0px auto;
        border:1px solid red;
    }
    
    #container div{
        position:relative;
        width:100px;
        height:100px;
        border:1px solid #ccc;
        float:left;
        margin-right:5px;
    }
    #container div span{
        position:absolute;
        bottom:0;
        right:0;
    }
    

    Check working example at http://jsfiddle.net/7YTYu/2/

    0 讨论(0)
  • 2020-12-02 07:49

    If you only have one line of text and your div has a fixed height, you can do this:

    div {
        line-height: (2*height - font-size);
        text-align: right;
    }
    

    See fiddle.

    0 讨论(0)
  • 2020-12-02 07:54

    Thanks @Harry the following code works for me:

    .your-div{
       vertical-align: bottom;
       display: table-cell;
    }
    
    0 讨论(0)
  • 2020-12-02 08:00

    Wrap the text in a span or similar and use the following CSS:

    .your-div {
        position: relative;
    }
    
    .your-div span {
        position: absolute;
        bottom: 0;
        right: 0;
    }
    
    0 讨论(0)
提交回复
热议问题