【问题标题】:Align position absolute element content to bottom with only one element仅使用一个元素将位置绝对元素内容与底部对齐
【发布时间】:2016-04-18 17:04:59
【问题描述】:

我有一个绝对 div 元素,里面有文本内容,没有任何标签,像这样:

<div class="element">
    This is some content...
</div>


.element{
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
    overflow: hidden;
    padding: 10px;
    color: white;
}

我想将该内容与 div 的底部对齐,但不使用更多的 html 标签(如this answer on a similiar question),以保持简单。

我怎样才能仅使用 css 实现这一点?

【问题讨论】:

    标签: html css alignment element vertical-alignment


    【解决方案1】:

    您可以像这样使用 CSS flexbox 来实现:

    .element {
      position: absolute;
      top: 10px;
      left: 10px;
      right: 10px;
      bottom: 10px;
      overflow: hidden;
      padding: 10px;
      color: red;
      background: green;
      display: flex;
      align-items: flex-end;
    }
    <div class="element">
      This is some content...
    </div>

    【讨论】:

    • 对,我完全忘记了 flex。这些天浏览器支持怎么样?
    • 微软已经结束了对古代 IE 浏览器的支持,所以这些天真的很好:) - winbeta.org/news/…
    • 感谢您的回答 :) 赞赏
    • 如果你想要更多的兼容性可以看看这个githubgithub.com/10up/flexibility:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2015-09-09
    • 1970-01-01
    • 2014-09-04
    • 2014-12-03
    相关资源
    最近更新 更多