如何将div与其父级的顶部对齐但保持其内联块行为?

时间:2011-04-16 11:53:48

标签: css html

请参阅:http://jsfiddle.net/b2BpB/1/

问:如何让box1和box3与父div boxContainer的顶部对齐?

#boxContainerContainer {
  background: #fdd;
  text-align: center;
}

#boxContainer {
  display: inline-block;
  border: thick dotted #060;
  margin: 0px auto 10px auto;
  text-align: left;
}

#box1 {
  width: 50px;
  height: 50px;
  background: #999;
  display: inline-block;
}

#box2 {
  width: 50px;
  height: 100px;
  background: #999;
  display: inline-block;
}

#box3 {
  width: 50px;
  height: 50px;
  background: #999;
  display: inline-block;
}

非常感谢...

致谢:此问题源于https://stackoverflow.com/users/20578/paul-d-waite先前给出的答案:Getting a CSS element to automatically resize to content width, and at the same time be centered

5 个答案:

答案 0 :(得分:351)

尝试vertical-align CSS属性。

#box1 {
    width: 50px;
    height: 50px;
    background: #999;
    display: inline-block;
    vertical-align: top; /* here */
}

也适用于#box3

答案 1 :(得分:21)

正如其他人所说,vertical-align: top是你的朋友。

这里的奖励是一个分叉的小提琴,增加了一些增强功能,可以在Internet Explorer 6和Internet Explorer 7中使用;)

示例:here

答案 2 :(得分:6)

使用vertical-align:top;对于你想要的元素,正如我在你的jsfiddle上所展示的那样。

http://www.brunildo.org/test/inline-block.html

答案 3 :(得分:6)

你可以添加float:left;对于每个方框(box1,box2,box3)。

http://jsfiddle.net/Wa4ma/

答案 4 :(得分:0)

或者你可以只在div中添加一些内容并使用inline-table

相关问题