Align an element to bottom with flexbox

前端 未结 8 1892
陌清茗
陌清茗 2020-11-22 15:34

I have a div with some children:

heading 1

heading 2

Some

相关标签:
8条回答
  • 2020-11-22 16:35

    1. Style parent element: style="display:flex; flex-direction:column; flex:1;"

    2. Style the element you want to stay at bottom: style="margin-top: auto;"

    3. Done! Wow. That was easy.

    Example:

    <section style="display:flex; flex-wrap:wrap;"> // For demo, not necessary
        <div style="display:flex; flex-direction:column; flex:1;"> // Parent element
            <button style="margin-top: auto;"> I </button> // Target element
        </div>
    
        ... 5 more identical divs, for demo ...
    
    </section>
    

    Demo: https://codepen.io/ferittuncer/pen/YzygpWX

    0 讨论(0)
  • 2020-11-22 16:36

    When setting your display to flex, you could simply use the flex property to mark which content can grow and which content cannot.

    Flex property

    div.content {
     height: 300px;
     display: flex;
     flex-direction: column;
    }
    
    div.up {
      flex: 1;
    }
    
    div.down {
      flex: none;
    }
    <div class="content">
      <div class="up">
        <h1>heading 1</h1>
        <h2>heading 2</h2>
        <p>Some more or less text</p>
      </div>
    
      <div class="down">
        <a href="/" class="button">Click me</a>
      </div>
    </div>

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