html - 如何在 div 中的同一基线上对齐 h2 和 p

标签 html css vertical-alignment

我有一个高度为 70px/4.375em 的帖子标题 div 其中有一个类别h2和一个p时间戳

我希望它对齐,如 this picture 中所示 (在 Photoshop 中制作)

这是我的 HTML:

<div class="post-header" style="background-color:#9377d8">
    <h2 class="category">Technology</h2>
    <p class="timestamp">Saturday, today</p>
</div>

还有我的 CSS:

.post-header{
    height:4.375em;
    width:25em;
    margin-bottom:0;
    float:left;
}
.category{
    color:white;
    display:inline;
    float:left;
    margin:0.625em;
}

.timestamp{
    color:white;
    display:inline;
    float:right;
    margin:0.625em;
}

请帮助我使用 CSS 以获得我想要的设计

最佳答案

您可以按如下方式更改您的 CSS:

.post-header {
    height:4.375em;
    width:25em;
    margin-bottom:0;
    display:block;
}
.category {
    color:white;
    display:inline-block;
    width:45%;
    margin:0.625em;
}
.timestamp {
    color:white;
    display:inline-block;
    text-align:right;
    margin:0.625em;
    width:40%;
}

这样,您可以更好地控制布局,因为您可以指定元素的宽度和垂直对齐方式。正如我们所说,我会使用百分比来表示利润率,但当然这取决于您

Visit fiddle看看它的实际效果

关于html - 如何在 div 中的同一基线上对齐 h2 和 p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033371/

相关文章:

javascript - onblur 不工作但 onclick 是

css - asp.net mvc4中如何设置背景图片

HTML 和 CSS。社交按钮如何移动它们?

html - 无法让我的导航栏与我的主体对齐

css - Bootstrap 容器/列/行错位

css - 导航栏图像不会垂直对齐 (CSS)

html - 使用 CSS 设置样式的按钮元素在 IE6 中不显示背景图像

javascript - 为什么调用 focus() 会破坏我的 CSS 转换?

html - 将元素的宽度设置为兄弟的宽度

html - 如何使用骨架网格垂直对齐图像旁边的文本