html - 具有可变填充的 CSS 底部对齐

标签 html css

我想弄清楚如何使用 CSS 将 div 对齐到父 div 的底部。但是,要注意的是,如果另一个同级 div 遇到它,我需要底部对齐的 div 向下移动。下图显示了可能发生的各种对齐的一些示例。前两个是理想的,我可以轻松地使用绝对值并将 bottom 设置为 0。但是,该解决方案在第三个示例中失效,因为文本会与底部 div 重叠。

Image showing what I am trying to achieve

最佳答案

假设所有图片大小相同

HTML

<div class="item">
  <div>
    <div class="image"></div>
  </div>
  <div class="text">
    <div>
      <div class="top">
        <div>
          Top
        </div>
      </div>
      <div class="bottom">
        <div>
          Bottom
        </div>
      </div>
    </div>
  </div>
</div>

CSS

  .item {display: table;}
  .item > div {display: table-cell; vertical-align: top;}
  .text {width: 100%;}
  .text > div {height: 144px; width: 100%; display: table;}
  .text > div > div {display: table-row;}
  .text > div > div > div {display: table-cell;}
  .top {height: 100%;}

Demo

关于html - 具有可变填充的 CSS 底部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360028/

相关文章:

Javascript隐藏图片

javascript - jQuery 动画滚动不起作用

html - 相对父级的 CSS 相对定位高度

html - 如何为 Bootstrap 表格行添加边距?

javascript - 浏览器刷新时的随机全屏背景图片

javascript - html 移动菜单大小调整

html - 在 div 顶部居中图像

php - 从表中检索数据

css - Font Awesome 仅适用于移动浏览器

css - 页眉 Logo 已偏离中心