Align an element to bottom with flexbox

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

I have a div with some children:

heading 1

heading 2

Some

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

    You can use auto margins

    Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.

    So you can use one of these (or both):

    p { margin-bottom: auto; } /* Push following elements to the bottom */
    a { margin-top: auto; } /* Push it and following elements to the bottom */
    

    .content {
      height: 200px;
      border: 1px solid;
      display: flex;
      flex-direction: column;
    }
    h1, h2 {
      margin: 0;
    }
    a {
      margin-top: auto;
    }
    <div class="content">
      <h1>heading 1</h1>
      <h2>heading 2</h2>
      <p>Some text more or less</p>
      <a href="/" class="button">Click me</a>
    </div>

    Alternatively, you can make the element before the a grow to fill the available space:

    p { flex-grow: 1; } /* Grow to fill available space */
    

    .content {
      height: 200px;
      border: 1px solid;
      display: flex;
      flex-direction: column;
    }
    h1, h2 {
      margin: 0;
    }
    p {
      flex-grow: 1;
    }
    <div class="content">
      <h1>heading 1</h1>
      <h2>heading 2</h2>
      <p>Some text more or less</p>
      <a href="/" class="button">Click me</a>
    </div>

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

    You can use display: flex to position an element to the bottom, but I do not think you want to use flex in this case, as it will affect all of your elements.

    To position an element to the bottom using flex try this:

    .container {
      display: flex;
    }
    
    .button {
      align-self: flex-end;
    }
    

    Your best bet is to set position: absolute to the button and set it to bottom: 0, or you can place the button outside the container and use negative transform: translateY(-100%) to bring it in the container like this:

    .content {
        height: 400px;
        background: #000;
        color: #fff;
    }
    .button {
        transform: translateY(-100%);
        display: inline-block;
    }
    

    Check this JSFiddle

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

    Try This

    .content {
          display: flex;
          flex-direction: column;
          height: 250px;
          width: 200px;
          border: solid;
          word-wrap: break-word;
        }
    
       .content h1 , .content h2 {
         margin-bottom: 0px;
        }
    
       .content p {
         flex: 1;
        }
       <div class="content">
      <h1>heading 1</h1>
      <h2>heading 2</h2>
      <p>Some more or less text</p>
      <a href="/" class="button">Click me</a>
    </div>

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

    Not sure about flexbox but you can do using the position property.

    set parent div position: relative and child element which might be an <p> or <h1> etc.. set position: absolute and bottom: 0.

    Example:

    index.html

    <div class="parent">
      <p>Child</p>
    </div>
    

    style.css

    .parent {
      background: gray;
      width: 10%;
      height: 100px;    
      position: relative;
    }
    p {
      position: absolute;
      bottom: 0;
    }
    

    Code pen here.

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

    I just found a solution for this.

    for those who are not satisfied with the given answers can try this approach with flexbox

    CSS

        .myFlexContainer {
            display: flex;
            width: 100%;
        }
    
        .myFlexChild {
            width: 100%;
            display: flex;
    
            /* 
             * set this property if this is set to column by other css class 
             *  that is used by your target element 
             */
            flex-direction: row;
    
            /* 
             * necessary for our goal 
             */
            flex-wrap: wrap;
            height: 500px;
        }
    
        /* the element you want to put at the bottom */
        .myTargetElement {
            /*
             * will not work unless flex-wrap is set to wrap and 
             * flex-direction is set to row
             */
            align-self: flex-end;
        }
    

    HTML

        <div class="myFlexContainer">
            <div class="myFlexChild">
                <p>this is just sample</p>
                <a class="myTargetElement" href="#">set this to bottom</a>
            </div>
        </div>
    
    0 讨论(0)
  • 2020-11-22 16:32

    The solution with align-self: flex-end; didn't work for me but this one did in case you want to use flex:

    Result

     -------------------
    |heading 1          |
    |heading 2          | 
    |paragraph text     |
    |                   |
    |                   |
    |                   | 
    |link button        |
     -------------------
    

    Code

    Note: When "running the code snippet" you have to scroll down to see the link at the bottom.

    .content {
      display: flex;
      justify-content: space-between;
      flex-direction: column;
      height: 300px;
    }
    
    .content .upper {
      justify-content: normal;
    }
    
    /* Just to show container boundaries */
    .content .upper, .content .bottom, .content .upper > * {
      border: 1px solid #ccc;
    }
    <div class="content">
      <div class="upper">
    	  <h1>heading 1</h1>
    	  <h2>heading 2</h2>
    	  <p>paragraph text</p>
      </div>
    
      <div class="bottom">
    	  <a href="/" class="button">link button</a>
      </div>
    </div>

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