How can I position my div at the bottom of its container?

前端 未结 24 2428
广开言路
广开言路 2020-11-22 00:46

Given the following HTML:



        
相关标签:
24条回答
  • 2020-11-22 01:41

    The flexbox approach!

    In supported browsers, you can use the following:

    Example Here

    .parent {
      display: flex;
      flex-direction: column;
    }
    .child {
      margin-top: auto;
    }
    

    .parent {
      height: 100px;
      border: 5px solid #000;
      display: flex;
      flex-direction: column;
    }
    .child {
      height: 40px;
      width: 100%;
      background: #f00;
      margin-top: auto;
    }
    <div class="parent">
      <div class="child">Align to the bottom</div>
    </div>


    The solution above is probably more flexible, however, here is an alternative solution:

    Example Here

    .parent {
      display: flex;
    }
    .child {
      align-self: flex-end;
    }
    

    .parent {
      height: 100px;
      border: 5px solid #000;
      display: flex;
    }
    .child {
      height: 40px;
      width: 100%;
      background: #f00;
      align-self: flex-end;
    }
    <div class="parent">
      <div class="child">Align to the bottom</div>
    </div>


    As a side note, you may want to add vendor prefixes for additional support.

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

    Maybe this helps someone: You can always place the div outside the other div and then push it upwards using negative margin:

    <div id="container" style="background-color: #ccc; padding-bottom: 30px;">
      Hello!
    </div>
    <div id="copyright" style="margin-top: -20px;">
      Copyright Foo web designs
    </div>
    
    0 讨论(0)
  • 2020-11-22 01:43

    CodePen link here.

    html, body {
        width: 100%;
        height: 100%;
    }
    
    .overlay {
        min-height: 100%;
        position: relative;
    }
    
    .container {
        width: 900px;
        position: relative;
        padding-bottom: 50px;
    }
    
    .height {
        width: 900px;
        height: 50px;
    }
    
    .footer {
        width: 900px;
        height: 50px;
        position: absolute;
        bottom: 0;
    }
    <div class="overlay">
        <div class="container">
            <div class="height">
                content
            </div>
        </div>
    
        <div class="footer">
            footer
        </div>
    </div>

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

    If you want it to "stick" to the bottom, regardless of the height of container, then absolute positioning is the way to go. Of course, if the copyright element is the last in the container it'll always be at the bottom anyway.

    Can you expand on your question? Explain exactly what you're trying to do (and why you don't want to use absolute positioning)?

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

    Just because this hasn't been mentioned at all, what usually works well in situations like yours:

    Placing the copyright-div after the container-div

    You would only have to format the copyright-div in a similar way to the other container (same overall width, centering, etc.), and all is fine.

    CSS:

    #container, #copyright {
        width: 1000px;
        margin:0 auto;
    }
    

    HTML:

    <div id="container">
        <!-- Other elements here -->
    </div>
    
    <div id="copyright">
        Copyright Foo web designs
    </div>
    

    The only time this might not be ideal is when your container-div is declared with height:100%, and the user would need to scroll down to see the copyright. But even still you could work around (e.g. margin-top:-20px - when the height of your copyright element is 20px).

    • No absolute positioning
    • No table layout
    • No crazy css, that looks different in every other browser (well IE at least, you know)
    • Simple and clear formatting

    Aside: I know the OP asked for a solution that "... sticks to the bottom of the 'container' div ...", and not something under it, but come on, people are looking for good solutions here, and this is one!

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

    According: w3schools.com

    An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

    So you need to position the parent element with something either relative or absolute, etc and position the desired element to absolute and latter set bottom to 0.

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