css - 垂直对齐 float div 中的多行文本

标签 css vertical-alignment

我一直在尝试不同的方法来垂直对齐多行文本。基本上我有类似这个 fiddle 中的东西 - http://jsfiddle.net/xPxwn/

<div class = "box">  <h2>multiline text goes here</h2> 
</div>

CSS:

.box
    {

width:100px;  
height:40px;
    border:1px solid;
    background:blue;
    color:yellow;
    -moz-border-radius: 5px;
    border-radius: 5px;
    margin:5px;
    text-align:center;
    font : 50% "Trebuchet MS", verdana, arial, tahoma, sans-serif;
    float:left;
    }       

我需要文本在框中垂直对齐,但我没有想出解决方案。有什么想法吗?

提前致谢 克里斯

最佳答案

如果您不需要 float 框并且可以稍微修改您的布局,您可以试试这个。 display:table-cellvertical-align:middle 添加到 .box 并删除了 float:left

 .box
    {
    display:table-cell;
    vertical-align:middle;
    width:100px;  
    height:40px;
    border:1px solid;
    background:blue;
    color:yellow;
    -moz-border-radius: 5px;
    border-radius: 5px;
    padding:0;margin:0;    
    text-align:center;
    font : 50% "Trebuchet MS", verdana, arial, tahoma, sans-serif;
    }        
.box>h2{padding:0;margin:0;}

关于css - 垂直对齐 float div 中的多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458854/

相关文章:

javascript - 在窗口调整大小时隐藏元素

javascript - Font Awesome 5 伪元素与 Webpack

css - 为整个页面使用多种背景颜色的最有效方法是什么?

html - 如何在 bootstrap 4 中使子内容相对于父容器垂直居中

html - 文本的垂直对齐方式和另一个内部的 div

html - 行高推开 <p> 文本而不是 <h2> 文本

css - 像我想要的那样对齐图像时遇到问题

javascript - 如何使用 javascript 选择 iframe 标签?

html - 使用 CSS 使页面内容居中

css - 垂直对齐 Bootstrap 3 列的内容