How to align content of a div to the bottom

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

Say I have the following CSS and HTML code:

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

    if you could set the height of the wrapping div of the content (#header-content as shown in other's reply), instead of the entire #header, maybe you can also try this approach:

    HTML

    <div id="header">
        <h1>some title</h1>
        <div id="header-content">
            <span>
                first line of header text<br>
                second line of header text<br>
                third, last line of header text
            </span>
        </div>
    </div>
    

    CSS

    #header-content{
        height:100px;
    }
    
    #header-content::before{
      display:inline-block;
      content:'';
      height:100%;
      vertical-align:bottom;
    }
    
    #header-content span{
        display:inline-block;
    }
    

    show on codepen

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

    I have devised a way which is a lot simpler than what's been mentioned.

    Set the height of the header div. Then inside that, style your H1 tag as follows:

    float: left;
    padding: 90px 10px 11px
    

    I'm working on a site for a client, and the design requires the text to be at the bottom of a certain div. I've achieved the result using these two lines, and it works fine. Also, if the text does expand, the padding will still remain the same.

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

    You can use following approach:

    .header-parent {
      height: 150px;
      display: grid;
    }
    
    .header-content {
      align-self: end;
    }
    <div class="header-parent">
      <h1>Header title</h1>
      <div class="header-content">
        Header content
      </div>
    </div>

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

    I found this solution bassed on a default bootstrap start template

    /* HTML */
    
    <div class="content_wrapper">
      <div class="content_floating">
        <h2>HIS This is the header<br>
          In Two Rows</h2>
        <p>This is a description at the bottom too</p> 
      </div>
    </div>
    
    /* css */
    
    .content_wrapper{
          display: table;
          width: 100%;
          height: 100%; /* For at least Firefox */
          min-height: 100%;
        }
    
    .content_floating{
      display: table-cell;
      vertical-align: bottom;
      padding-bottom:80px;
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:59

    Here's the flexy way to do it. Of course, it's not supported by IE8, as the user needed 7 years ago. Depending on what you need to support, some of these can be done away with.

    Still, it would be nice if there was a way to do this without an outer container, just have the text align itself within it's own self.

    #header {
        -webkit-box-align: end;
        -webkit-align-items: flex-end;
        -ms-flex-align: end;
        align-items: flex-end;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        height: 150px;
    }
    
    0 讨论(0)
  • 2020-11-22 02:00

    After struggling with this same issue for some time, I finally figured out a solution that meets all of my requirements:

    • Does not require that I know the container's height.
    • Unlike relative+absolute solutions, the content doesn't float in its own layer (i.e., it embeds normally in the container div).
    • Works across browsers (IE8+).
    • Simple to implement.

    The solution just takes one <div>, which I call the "aligner":

    CSS

    .bottom_aligner {
        display: inline-block;
        height: 100%;
        vertical-align: bottom;
        width: 0px;
    }
    

    html

    <div class="bottom_aligner"></div>
    ... Your content here ...
    

    This trick works by creating a tall, skinny div, which pushes the text baseline to the bottom of the container.

    Here is a complete example that achieves what the OP was asking for. I've made the "bottom_aligner" thick and red for demonstration purposes only.

    CSS:

    .outer-container {
      border: 2px solid black;
      height: 175px;
      width: 300px;
    }
    
    .top-section {
      background: lightgreen;
      height: 50%;
    }
    
    .bottom-section {
      background: lightblue;
      height: 50%;
      margin: 8px;
    }
    
    .bottom-aligner {
      display: inline-block;
      height: 100%;
      vertical-align: bottom;
      width: 3px;
      background: red;
    }
    
    .bottom-content {
      display: inline-block;
    }
    
    .top-content {
      padding: 8px;
    }
    

    HTML:

    <body>
      <div class="outer-container">
        <div class="top-section">
          This text
          <br> is on top.
        </div>
        <div class="bottom-section">
          <div class="bottom-aligner"></div>
          <div class="bottom-content">
            I like it here
            <br> at the bottom.
          </div>
        </div>
      </div>
    </body>
    

    Align bottom content

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