简体   繁体   English

如何在段落块中垂直居中文本?

[英]How to vertically center the text in a paragraph block?

How can I get from that : 我怎样才能从中得到:

从那以后

to that : 对此:

那个

?

Is there a css property to get this result ? 是否有css属性来获得此结果? I'm aware of vertical-align But that is only for inline elements when they are altogether with more height-wide element ones. 我知道vertical-align但是这只适用于内联元素,当它们与更高的高度元素一起时。 Any idea ? 任何想法 ?

You can use margin / padding / line height 您可以使用margin / padding / line height

p #copyright {margin-top: 10px;}

or 要么

p #copyright {padding-top: 10px;}

or 要么

p #copyright {line-height: 25px;}

Here is an article that can help you understand it better: 这篇文章可以帮助您更好地理解它:

http://www.w3.org/TR/CSS2/box.html http://www.w3.org/TR/CSS2/box.html

If the content is fixed (The actual words that make up "Copyright" will never change), then you should go with Nave Tseva's solution. 如果内容是固定的(构成“版权”的实际单词永远不会改变),那么你应该使用Nave Tseva的解决方案。

For vertically aligning dynamic content though, you can use display:table-cell; vertical-align:middle 但是,为了垂直对齐动态内容,您可以使用display:table-cell; vertical-align:middle display:table-cell; vertical-align:middle to accomplish the vertical centering. display:table-cell; vertical-align:middle以完成垂直居中。 Although, a more cross-browser compatible solution is to actually wrap that content in a single table cell, and set valign="middle" . 虽然,更多跨浏览器兼容的解决方案是将该内容实际包装在单个表格单元格中,并设置valign="middle"

<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td valign="middle">
      <p>Copyright</p>
    </td>
  </tr>
</table>

I know most people would frown on using a table for non-delinated single entry content like this as that's not actually a situation where tables are the appropriate element... but then again, in a perfect world we wouldn't be taking IE shittiness into consideration either. 我知道大多数人会不喜欢使用表格来处理非删除的单个条目内容,因为这实际上并不是表格是适当元素的情况......但是在一个完美的世界中,我们不会对IE感到羞怯考虑到。

If you can use flex box layout, that is, you don't need support for older browsers, you can use something like the following. 如果您可以使用弹性框布局,也就是说,您不需要支持旧版浏览器,则可以使用以下内容。 The CSS is kind of jacked to support different browsers, but they are converging... CSS有点支持不同的浏览器,但它们正在融合......

#content {
   display: -webkit-box;   /* OLD: Safari,  iOS, Android browser, older WebKit browsers.  */
   display: -moz-box;      /* OLD: Firefox (buggy) */ 
   display: -ms-flexbox;   /* MID: IE 10 */
   display: -webkit-flex;  /* NEW, Chrome 21+ */
   display: flex;          /* NEW: Opera 12.1, Firefox 22+ */

   -webkit-box-align: center; -moz-box-align: center; /* OLD… */
   -ms-flex-align: center; /* You know the drill now… */
   -webkit-align-items: center;
   align-items: center;

   -webkit-box-pack: center; -moz-box-pack: center; 
   -ms-flex-pack: center; 
   -webkit-justify-content: center;
   justify-content: center;

   margin: 0;
   height: 500px;
   border: 1px solid red;
}

#content p{
    border: 1px blue solid;
    box-flex:1
}

HTML HTML

<div id="content">
    <p>Here's everything that I can think of to put into one line. Have to add more fillter to make sure it works for multiple lines too, yay-ah</p>
</div>

See http://jsfiddle.net/C4WTE/ and the flexbox playground 请参阅http://jsfiddle.net/C4WTE/flexbox playground

If you really need this to work in older browsers, I'd just give up and use tables like relic180 suggested http://ajaxian.com/wp-content/images/giveupandusetables.png 如果你真的需要这个在旧浏览器中工作,我只是放弃并使用像relic180这样的表建议 http://ajaxian.com/wp-content/images/giveupandusetables.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM