How to align an image to the bottom of the div?

  • Trying to align an image to the bottom of the out div element using vertical-align,but this vertical-align doesn’t seem to work,the image is not moving at all.
[pastacode lang=”markup” manual=”%3Cdiv%20class%3D%22row%22%3E%0A%20%20%20%20%20%09%3Cdiv%20class%3D%22col-md-1%22%3E%0A%20%20%20%20%20%20%20%09%3Cimg%20src%3D%22..%2Fimages%2Fimage.png%22%20style%3D%22height%3A%2020px%3B%20width%3A%2020px%3Bcursor%3Apointer%22%2F%3E%0A%20%20%20%20%20%09%3C%2Fdiv%3E%0A%20%20%20%20%20%09%3Cdiv%20class%3Dcol-md-11%20%3E%0A%20%20%20%20%20%20%20%20%09%3Cdiv%20%20style%3D%22overflow%3Aauto%3Bheight%3A300px%22%3E%3C%2Fdiv%3E%0A%20%20%20%20%20%09%3C%2Fdiv%3E%0A%09%3C%2Fdiv%3E%0A” message=”html” highlight=”” provider=”manual”/] [ad type=”banner”]
  • We are using bootstrap classes for alignments. Is there anything that can make the image div align to the bottom of the outer div?Which is taking the height of second div which is 300px;.

  • something like this should do the trick:
[pastacode lang=”css” manual=”.parent%20%0A%7B%0A%20%09%20position%3A%20relative%3B%0A%20%20%09height%3A%20300px%3B%0A%7D%0A%09.child%20%0A%7B%0A%20%09%20position%3A%20absolute%3B%0A%09%20bottom%3A%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
  • You could add those classes to the DIV and the IMG respectively.
  • It looks like the parent div will be 300px high anyway because of the height set on the child in the adjacent div. If you specify the height of the parent instead, then you can absolutely position the child relative to the size of the parent.
  • If you don’t set the parent as position:relative, then the child will be position relative to something else (like the page).
  • Vertical-align won’t work because the IMG is an inline element, and the behavior you’re expecting is table-cell dependent.
  • With inline elements, vertical alignment is relative to the line and not to the parent container, so that an image aligned with text would sit in various positions relative to a given line of text.

  • Using display: table-cell is fine, provided that you’re aware that it won’t work in IE6/7
  • To fix the space at the bottom, add vertical-align: bottom to the actual imgs:
[pastacode lang=”markup” manual=”%3Cdiv%20id%3D%22randomContainer%22%3E%0A%20%20%20%20%3Cdiv%20id%3D%22imageContainer%22%3E%0A%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22http%3A%2F%2Fdummyimage.com%2F50x50%2Ff0f%2Ffff%22%20alt%3D%22%22%2F%3E%0A%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22http%3A%2F%2Fdummyimage.com%2F50x60%2Ff0f%2Ffff%22%20alt%3D%22%22%2F%3E%0A%20%20%20%20%20%20%20%20%3Cim%0A” message=”html code” highlight=”” provider=”manual”/]
  • There’s always the most obvious fix, which is to simply remove the whitespace in the HTML:
[pastacode lang=”markup” manual=”%3Cdiv%3Ea%3C%2Fdiv%3E%0A%3Cdiv%3Ea%3C%2Fdiv%3E%0A” message=”html code” highlight=”” provider=”manual”/]
  • To this:
[pastacode lang=”markup” manual=”%3Cdiv%3Ea%3C%2Fdiv%3E%3Cdiv%3Ea%3C%2Fdiv%3E%0A” message=”html code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Flexboxes can accomplish this by using align-items: flex-end; flex-direction: column; with display: flex; or display: inline-flex;
[pastacode lang=”css” manual=”div%23imageContainer%20%0A%7B%0A%20%20%20%20height%3A%20160px%3B%20%20%0A%20%20%20%20align-items%3A%20flex-end%3B%0A%20%20%20%20display%3A%20flex%3B%0A%20%20%20%20flex-direction%3A%20column%3B%0A%7D%0A” message=”css code” highlight=”” provider=”manual”/]

< div > with some proportions :

[pastacode lang=”css” manual=”div%20%0A%7B%0A%20%20position%20relative%0A%20%20width%3A%20100%25%3B%0A%20%20height%3A%20100%25%3B%0A%7D%0A” message=”css code” highlight=”” provider=”manual”/]

< img >’s with their own proportions :

[pastacode lang=”css” manual=”img%20%0A%7B%0A%20%20position%3A%20absolute%3B%0A%20%20top%3A%200%3B%0A%20%20left%3A%200%3B%0A%20%20bottom%3A%200%3B%0A%20%20right%3A%200%3B%0A%20%20width%3A%20auto%3B%20%2F*%20to%20keep%20proportions%20*%2F%0A%20%20height%3A%20auto%3B%20%2F*%20to%20keep%20proportions%20*%2F%0A%20%20max-width%3A%20100%25%3B%20%2F*%20not%20to%20stand%20out%20from%20div%20*%2F%0A%20%20max-height%3A%20100%25%3B%20%2F*%20not%20to%20stand%20out%20from%20div%20*%2F%0A%20%20margin%3A%20auto%20auto%200%3B%20%2F*%20position%20to%20bottom%20and%20center%20*%2F%0A%7D%0A” message=”css code” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: