html - 将元素与容器底部对齐

标签 html css

我有一些框,每个框都显示博客文章的标题和日期。博客标题最多占框的三行。

enter image description here

HTML

<article class="box" style="height: 144px">
    <header class="entry-header" style="padding-top: 15%;">
        <?php the_title(); ?>
    </header>

    <footer class="entry-meta" style="font-size: 12px;">
        <?php echo get_the_date(); ?>
    </footer>
</article>

CSS

.box {
    background-color: #333333;      
    color: white;       
}

.box header {
    text-align: center;
    word-wrap: break-word;
    margin: 0 10px 0 10px;
}

.box footer {
    text-align: center;
}

由于标题的长度可能不同,我无法将日期与框的底部对齐。

预期结果

enter image description here

我已经尝试将 margin-top 设置为 footer,但这并不能正确对齐日期,因为它们都显示在不同的行上。有没有其他方法可以解决这个问题?

最佳答案

在盒子上设置position:relative;

在页脚(日期)上设置position:absolute;

.box {

   position: relative;      
}

.box footer {
    position: absolute;
    bottom: 10px; /* however much you need */
}

或者你也可以使用 css 表格:

FIDDLE

CSS

.box
{
    display: table;
}
header
{
    display: table-row;
    height: 100%; /* fills remaining height */
}
footer
{
    display: table-row;
    height: 20px;
}

关于html - 将元素与容器底部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830878/

相关文章:

html - 在 Div 上叠加 Div

javascript - HTML5 - 启动共享相同变量的游戏对象

php - HTML/PHP 表单不会将数据插入 MySQL 数据库

javascript - HTML <脚本语言 ="$var",接受什么 $var?

CSS z-index 不适用于相对位置

javascript - 获取点击的div中文本字段的值

jquery - 我需要在 DIV 中插入 append jquery 的结果,只是 css 不能正常工作

javascript - 如何延迟在我的主页上加载 Javascript 和 CSS。我可以像 Google 对 Analytics 那样做吗?

html - @media 标签不适用于 phtml 文件

javascript - 等待CSS中的背景图片完全加载