如何垂直居中文字

goo*_*ing 3 html css

我希望文本在框中垂直居中.我不想使用padding-top,因为它对于每个长度的文本都不正确.我在这里发布了一个例子:http://jsfiddle.net/55Ruh/23/

rad*_*nys 5

您可以像这样使用table属性vertical-align(在您的示例中):

html:

<div class="bg_class">
  <div class="bg_class_inner">
    My text goes here...My text goes here...My text goes here...
    My text goes here...My text goes here...My text goes here...
    My text goes here...My text goes here...My text goes here...
    My text goes here...My text goes here...
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

css:

.bg_class
{
  background: green;
  width: 300px;
  height: 200px;
  text-align: center;
  margin: 0 auto;
  display: table; 
}
.bg_class_inner
{
  display: table-cell;
  text-align: center;
  vertical-align: middle
}
Run Code Online (Sandbox Code Playgroud)