Flexbox column align self to bottom

后端 未结 7 1416
逝去的感伤
逝去的感伤 2021-01-30 19:53

Im trying to use Flexbox. http://css-tricks.com/almanac/properties/a/align-content/ this shows nice alignment options; but i would actually want a Top-top-bottom situation.

相关标签:
7条回答
  • 2021-01-30 20:04

    Basically, the answer is to give to the last of the middle elements a flex grow 1 code as follows:

    .middle-last{
      flex-grow: 1; // assuming none of the other have flex-grow set
    }
    

    Thanks, T04435.

    0 讨论(0)
  • 2021-01-30 20:06

    align self property rely on the alignment of an item in respect of the cross axis, not the main axis. So this is not the way to go. You have several options to achieve that using flexbox, though:

    1) Use flex-grow:1 on your middle item. This will make it grow taking all remaining space in the container, thus pushing your last div to the bottom.

    2) Refactor your layout so that there is a main div with justify-content:space-between so that your last div will be sticked to the bottom:

    .container{
        display:flex;
        flex-direction:column;
        justify-content:space-between;
    }
    .body{
        display:flex;
        flex-direction:column;
    }
    .bottom{
        /* nothing needed for the outer layout */
    }
    
    <div class="container">
        <div class="body">
            <div>top content</div>
            <div>middle content</div>
        </div>
        <div class="bottom">
            bottom content
        </div>
    </div>
    

    3) This is a bit weird, but you could even do that using align-self but inverting the flex direction and allowing items to wrap:

    .container{
        display:flex;
        flex-direction:row;
        flex-wrap:wrap;
    }
    .body{
        display:flex;
        flex-direction:column;
        flex-basis:100%;
    }
    .bottom{
        align-self:flex-end
    }
    

    I've tested all this out using this free flexbox designer: http://algid.com/Flex-Designer

    0 讨论(0)
  • 2021-01-30 20:09

    I know this is a old post but the same problem, with Flexbox's single axis alignment, made me nuts for an hour.

    The auto margin is a nice trick but i wanted to share my solution with CSS Grid.

    The important part is the definition of the grid-template-rows. With auto the rows have the same height as the content and 1fr uses the remaining space for the middle row.

    Here a nice overview about CSS Grid: https://css-tricks.com/snippets/css/complete-guide-grid/

    .container {
      min-height: 100vh;
      display: grid;
      grid-template-rows: auto 1fr auto;
      grid-gap: 1em;
      justify-content: center;
    }
    
    .top {
      height: 2em;
      width: 10em;
      background-color: blue;
    }
    
    .middle {
      height: 3em;
      width: 10em;
      background-color: green;
    }
    
    .bottom {
      height: 2em;
      width: 10em;
      background-color: red;
    }
    <div class="container">
      <div class="top">
        <p>TOP</p>
      </div>
      <div class="middle">
        <p>MIDDLE</p>
      </div>
      <div class="bottom">
        <p>BOTTOM</p>
      </div>
    </div>

    0 讨论(0)
  • 2021-01-30 20:10
    .message-container {
        display: flex;
        flex-direction: column;
        place-content: flex-end;
        height: -webkit-fill-available;
    }
    
    .message {
        display: table-cell;
    }
    

    Try this. I use it for messenger.

    .container {
      height: 400px;
    }
    
    .message-container {
        display: flex;
        background: #eee;
        flex-direction: column;
        place-content: flex-end;
        height: -webkit-fill-available;
    }
    
    .user-message {
        align-self: flex-start;
        display: table-cell;
        padding: 5px;
        margin: 10px;
        border-radius: 10px;
        background-color: rgba(154, 247, 200, 0.692);
    }
    
    .friend-message {
        align-self: flex-end;
        display: table-cell;
        padding: 5px;
        margin: 10px;
        border-radius: 10px;
        background-color: rgba(169, 207, 250, 0.692);
    }
    <div class='container'>
      <div class='message-container'>
        <div class='user-message'>Hello!</div>
        <div class='friend-message'>Hi.</div>
      </div>
    </div>

    0 讨论(0)
  • 2021-01-30 20:11

    I'm a bit late to the party, but might be relevant for others trying to accomplish the same you should be able to do:

    margin-top: auto
    

    on the element in question and it should go to the bottom. Should do the trick for firefox, chrome and safari.

    0 讨论(0)
  • 2021-01-30 20:21

    I found my own solution, i will add it here for documentation value;

    If you give the middle block height: 100% it will take up al the room in the middle. So the bottom block will be at the actual bottom and top and middle are on top.

    UPDATE: This doesn't work for Chrome...

    UPDATE: Found a way that works for FF/Chrome: setting flex-grow on a higher number (1 is enough) for [middle] will make it take full middle size. more info: http://css-tricks.com/almanac/properties/f/flex-grow/

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