html - 垂直对齐 : middle doesn't work

标签 html css

css 属性 vertical-align: middle 在此示例中不起作用。

HTML:

<div>
  <span class='twoline'>Two line text</span>
  <span class='float'>  Float right  </span>
</div>

CSS:

.float {
    float: right;
}

.twoline {
    width: 50px;
    display: inline-block;
}

div {
    border: solid 1px blue;
    vertical-align: middle;
}

右侧 float 的 span 相对于包含它的 div 没有垂直居中。我怎样才能让它垂直居中?

以上代码在this fiddle中.

最佳答案

您必须使用 display 将您的元素包装在 table-cell 中,在 table 中。

像这样:

<div>
  <span class='twoline'>Two line text</span>
  <span class='float'>Float right</span>
</div>

.float {
    display: table-cell;
    vertical-align: middle;
    text-align: right;
}
.twoline {
    width: 50px;
    display: table-cell;
}
div {
    display: table;
    border: solid 1px blue;
    width: 500px;
    height: 100px;
}

此处显示:http://jsfiddle.net/e8ESb/7/

关于html - 垂直对齐 : middle doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7967217/

相关文章:

html - 为什么水平边距不会像垂直边距一样折叠?

html - 从组底部使用 CSS 旋转 SVG

javascript - 通过主题设置改变主题颜色

HTMLParser 只转换文件的第一行

javascript - 如何在文本框中传输传单标记纬度/经度

css - 使用多个内联标签创建 Bootstrap 表单并选择

css - jquery.ui 隐藏固定元素

css - 条件注释或 IE 特定 hack

html - 按钮 HTML/CSS 后面的水平线

javascript - 如何在 python 中制作 HTML5 Geolocation 对象?