css - 将行内 block DIV 对齐到容器元素的顶部

标签 css

当两个 inline-block div 具有不同的高度时,为什么两者中较短的一个不与容器顶部对齐? ( DEMO ):

.container { 
    border: 1px black solid;
    width: 320px;
    height: 120px;    
}

.small {
    display: inline-block;
    width: 40%;
    height: 30%;
    border: 1px black solid;
    background: aliceblue;    
}

.big {
    display: inline-block;
    border: 1px black solid;
    width: 40%;
    height: 50%;
    background: beige;    
}
<div class="container">
    <div class="small"></div>
    <div class="big"></div>
</div>

如何将小 div 对齐到其容器的顶部?

最佳答案

因为 vertical-align 默认设置为 baseline

改用vertical-align:top:

.small{
    display: inline-block;
    width: 40%;
    height: 30%;
    border: 1px black solid;
    background: aliceblue;   
    vertical-align:top; /* <---- this */
}

http://jsfiddle.net/Lighty_46/RHM5L/9/

或作为 @f00644说您也可以将 float 应用于子元素。

关于css - 将行内 block DIV 对齐到容器元素的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970384/

相关文章:

html - 将菱形放置在两列之间的中心线上

css - 修复 css 中的列高度

css - 响应式字体大小 - 适合容器

javascript - 当被溢出的父元素隐藏时,防止使用 Tab 键进行链接 :hidden

javascript - 如何从中心开始jquery .animation

html - Chrome 突出显示带有 tabindex ="-1"的 div

html - 子 Div 的底部超出父 Div

css - 在图像的 css 中设置对齐属性的样式

css - 通过 CSS 更改内容 URL 在 Firefox 中不起作用

html - 如何在像网格一样显示的 div 中垂直对齐文本?