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

相关文章:

html - 如何使用 CSS 根据任意数字倍数选择特定类之后的第 n 个兄弟?

w3c - CSS 折叠边距有什么意义?

css - 如何在 Stylus 函数中创建多个框阴影

html - 溢出 :hidden not working on any Android Device Mobile App

javascript - X-UA-兼容 JavaScript 和 CSS 文件

css - 通过 CSS 导入字体

CSS form[action] 不适用于查询字符串

javascript - JS 在 iOS 设备上无法正常工作 - 仅有时

css - :hover element change::after style another component

html - 表格单元格的间距