How to align content of a div to the bottom

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

Say I have the following CSS and HTML code:

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

    Seems to be working:

    #content {
        /* or just insert a number with "px" if you're fighting CSS without lesscss.org :) */
        vertical-align: -@header_height + @content_height;
    
        /* only need it if your content is <div>,
         * if it is inline (e.g., <a>) will work without it */
        display: inline-block;
    }
    

    Using less makes solving CSS puzzles much more like coding than like... I just love CSS. It's a real pleasure when you can change the whole layout (without breaking it :) just by changing one parameter.

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

    If you have multiple, dynamic height items, use the CSS display values of table and table-cell:

    HTML

    <html>
    <body>
    
      <div class="valign bottom">
        <div>
    
          <div>my bottom aligned div 1</div>
          <div>my bottom aligned div 2</div>
          <div>my bottom aligned div 3</div>
    
        </div>
      </div>
    
    </body>
    </html>
    

    CSS

    html,
    body {
      width: 100%;
      height: 100%;
    }
    .valign {
      display: table;
      width: 100%;
      height: 100%;
    }
    .valign > div {
      display: table-cell;
      width: 100%;
      height: 100%;
    }
    .valign.bottom > div {
      vertical-align: bottom;
    }
    

    I've created a JSBin demo here: http://jsbin.com/INOnAkuF/2/edit

    The demo also has an example how to vertically center align using the same technique.

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

    You don't need absolute+relative for this. It is very much possible using relative position for both container and data. This is how you do it.

    Assume height of your data is going to be x. Your container is relative and footer is also relative. All you have to do is add to your data

    bottom: -webkit-calc(-100% + x);
    

    Your data will always be at the bottom of your container. Works even if you have container with dynamic height.

    HTML will be like this

    <div class="container">
      <div class="data"></div>
    </div>
    

    CSS will be like this

    .container{
      height:400px;
      width:600px;
      border:1px solid red;
      margin-top:50px;
      margin-left:50px;
      display:block;
    }
    .data{
      width:100%;
      height:40px;
      position:relative;
       float:left;
      border:1px solid blue;
      bottom: -webkit-calc(-100% + 40px);
       bottom:calc(-100% + 40px);
    }
    

    Live example here

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 01:54
    display: flex;
    align-items: flex-end;
    
    0 讨论(0)
  • 2020-11-22 01:54

    You can simply achieved flex

    header {
      border: 1px solid blue;
      height: 150px;
      display: flex;                   /* defines flexbox */
      flex-direction: column;          /* top to bottom */
      justify-content: space-between;  /* first item at start, last at end */
    }
    h1 {
      margin: 0;
    }
    <header>
      <h1>Header title</h1>
      Some text aligns to the bottom
    </header>

    0 讨论(0)
  • 2020-11-22 01:56
    #header {
        height: 150px;
        display:flex;
        flex-direction:column;
    }
    
    .top{
        flex: 1;
    }   
    
    <div id="header">
        <h1 class="top">Header title</h1>
        Header content (one or multiple lines)
    </div>
    

    #header {
        height: 250px;
        display:flex;
        flex-direction:column;
        background-color:yellow;
    }
    
    .top{
        flex: 1;
    }
    <div id="header">
        <h1 class="top">Header title</h1>
        Header content (one or multiple lines)
    </div>

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