将<button>中的项目垂直对齐为弹性项目

时间:2017-10-12 13:33:13

标签: css flexbox

我有许多按钮,里面有不同大小的图像:

<div>
  <button><img src="http://via.placeholder.com/200x200" alt=""></button>
  <button><img src="http://via.placeholder.com/200x100" alt=""></button>
  <button><img src="http://via.placeholder.com/200x50" alt=""></button>
  <button><img src="http://via.placeholder.com/200x200" alt=""></button>
  <button><img src="http://via.placeholder.com/200x150" alt=""></button>
</div>

所以,我想将这些图像垂直对齐到底部,如下图所示:

desired layout

但无论我尝试什么,图像总是垂直居中:

layout with buttons

看看这个jsFiddle:https://jsfiddle.net/y7efebwg/

有趣的是,如果我使用div而不是button s,一切都按预期工作:https://jsfiddle.net/Lkycyvvf/1/

2 个答案:

答案 0 :(得分:4)

将按钮的display属性设置为flex并使用flex-direction:columnjustify-content:flex-end似乎有效。

div {
  display: flex;
}

button {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
<div>
  <button><img src="http://via.placeholder.com/200x200" alt=""></button>
  <button><img src="http://via.placeholder.com/200x100" alt=""></button>

</div>

答案 1 :(得分:1)

将按钮本身设置为弹性容器并为margin-top auto提供图像似乎有效:

button {
    display: flex;
    flex-direction: column;
}

button img {
    margin-top: auto;
}

https://jsfiddle.net/y7efebwg/1/

相关问题