How to align content of a div to the bottom

后端 未结 26 2535
挽巷
挽巷 2020-11-22 01:13

Say I have the following CSS and HTML code:

相关标签:
26条回答
  • 2020-11-22 01:41

    I use these properties and it works!

    #header {
      display: table-cell;
      vertical-align: bottom;
    }
    
    0 讨论(0)
  • 2020-11-22 01:42

    Here is another solution using flexbox but without using flex-end for bottom alignment. The idea is to set margin-bottom on h1 to auto to push the remaining content to the bottom:

    #header {
      height: 350px;
      display:flex;
      flex-direction:column;
      border:1px solid;
    }
    
    #header h1 {
     margin-bottom:auto;
    }
    <div id="header">
      <h1>Header title</h1>
      Header content (one or multiple lines) Header content (one or multiple lines)Header content (one or multiple lines) Header content (one or multiple lines)
    </div>

    We can also do the same with margin-top:auto on the text but in this case we need to wrap it inside a div or span:

    #header {
      height: 350px;
      display:flex;
      flex-direction:column;
      border:1px solid;
    }
    
    #header span {
     margin-top:auto;
    }
    <div id="header">
      <h1>Header title</h1>
      <span>Header content (one or multiple lines)</span>
    </div>

    0 讨论(0)
  • 2020-11-22 01:43

    If you're not worried about legacy browsers use a flexbox.

    The parent element needs its display type set to flex

    div.parent {
      display: flex;
      height: 100%;
    }
    

    Then you set the child element's align-self to flex-end.

    span.child {
      display: inline-block;
      align-self: flex-end;
    }
    

    Here's the resource I used to learn: http://css-tricks.com/snippets/css/a-guide-to-flexbox/

    0 讨论(0)
  • 2020-11-22 01:43

    a very simple, one-line solution, is to add line-heigth to the div, having in mind that all the div's text will go bottom.

    CSS:

    #layer{width:198px;
           height:48px;
           line-height:72px;
           border:1px #000 solid}
    #layer a{text-decoration:none;}
    

    HTML:

    <div id="layer">
        <a href="#">text at div's bottom.</a>
    </div>
    

    keep in mind that this is a practical and fast solution when you just want text inside div to go down, if you need to combine images and stuff, you will have to code a bit more complex and responsive CSS

    0 讨论(0)
  • 2020-11-22 01:45

    Use CSS positioning:

    /* Creates a new stacking context on the header */
    #header {
      position: relative;
    }
    
    /* Positions header-content at the bottom of header's context */
    #header-content {
      position: absolute;
      bottom: 0;
    }
    

    As cletus noted, you need identify the header-content to make this work.

    <span id="header-content">some header content</span>
    
    <div style="height:100%; position:relative;">
        <div style="height:10%; position:absolute; bottom:0px;">bottom</div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 01:48

    try with:

    div.myclass { margin-top: 100%; }
    

    try changing the % to fix it. Example: 120% or 90% ...etc.

    0 讨论(0)
提交回复
热议问题