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/59264392/

相关文章:

css - 如何使用 SVG defs.svg 中的符号作为 CSS 中的背景图像?

css - 显示图像和文本相互内联

html - 将 ul 在 div 中居中(已经在页面居中)

javascript - 具有不同颜色选项的浮线菜单/导航

css - 如何让一个 div 填满整个页面的宽度?

jquery - onClick 从 div 中删除类

javascript - 为什么我会收到 TypeError : L. Control.Draw is not a constructor 错误?

html - 如何在中间对齐文本和图像

css - 如何排除一个以上的 child

html - 引导和定位问题(输入组)