HTML image bottom alignment inside DIV container

后端 未结 4 1719
执笔经年
执笔经年 2020-12-13 17:46

I have a div tag with a fixed height. Most of the images have the same height and width.

I want to align the images at the bottom of the div so that they are nicely

相关标签:
4条回答
  • 2020-12-13 18:05

    <div> with some proportions

    div {
      position: relative;
      width: 100%;
      height: 100%;
    }
    

    <img>'s with their own proportions

    img {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      width: auto; /* to keep proportions */
      height: auto; /* to keep proportions */
      max-width: 100%; /* not to stand out from div */
      max-height: 100%; /* not to stand out from div */
      margin: auto auto 0; /* position to bottom and center */
    }
    
    0 讨论(0)
  • 2020-12-13 18:14

    This is your code: http://jsfiddle.net/WSFnX/

    Using display: table-cell is fine, provided that you're aware that it won't work in IE6/7. Other than that, it's safe: Is there a disadvantage of using `display:table-cell`on divs?

    To fix the space at the bottom, add vertical-align: bottom to the actual imgs:

    http://jsfiddle.net/WSFnX/1/

    Removing the space between the images boils down to this: bikeshedding CSS3 property alternative?

    So, here's a demo with the whitespace removed in your HTML: http://jsfiddle.net/WSFnX/4/

    0 讨论(0)
  • 2020-12-13 18:16

    Flexboxes can accomplish this by using align-items: flex-end; with display: flex; or display: inline-flex;

    div#imageContainer {
        height: 160px;  
        align-items: flex-end;
        display: flex;
    
        /* This is the default value, so you only need to explicitly set it if it's already being set to something else elsewhere. */
        /*flex-direction: row;*/
    }
    

    JSFiddle example

    CSS-Tricks has a good guide for flexboxes

    0 讨论(0)
  • 2020-12-13 18:17

    Set the parent div as position:relative and the inner element to position:absolute; bottom:0

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