中心文本在div [英] center text in div

查看:67
本文介绍了中心文本在div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div> 
< div class =left>
< div align =centerclass =node>
< div class =nodeText>
< h2>测试< / h2>

< / div>
< div class =node>
< h2>测试< / h2>
< / div>
< div class =node>
< h2>测试< / h2>
< / div>
< div>





div似乎无效

  .left {
float:left;
width:200px;
border:solid 1px black;
text-align:center;
}
.node {
height:200px;
border:solid 1px black;
margin:0 auto;
}
.nodeText {
vertical-align:middle;
}
h2 {
text-align:center;
}

Tnxs



内嵌内容的水平居中很容易。只需将 text-align:center 应用到包含的块。使用CSS在另一个块中水平居中一个块通常用以下方式完成:

  #centerMe {margin:0 auto; } 

注意:在IE6上,这要求IE符合 而不是怪癖模式。要强制始终,请 DOCTYPE 。例如:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN
http://www.w3.org/TR/html4/loose.dtd\">

垂直居中更糟糕。



您可以查看 CSS中的垂直居中。这实际上是一个非平凡的问题。您正在使用的 vertical-align 样式属性仅适用于表格单元格(以跨浏览器向后兼容的方式)。这对于一个表格是值得的。


  <div>
<div class="left">
  <div align="center" class="node">
    <div class="nodeText">
      <h2 >test</h2>

  </div>
  <div class="node">
      <h2>test</h2>
  </div>
  <div class="node">
      <h2>test</h2>
  </div>
<div>

I need to center text in div nothing seems to work

.left {
  float:left;
  width:200px;
  border:solid 1px black;
  text-align: center;
}
.node {
  height:200px;
  border:solid 1px black;
  margin:0 auto;
}
.nodeText{
  vertical-align: middle;
}
h2{
     text-align: center;
}

Tnxs

解决方案

Do you mean vertical centering, horizontal centering or both?

Horizontal centering of inline content is easy. Just apply text-align: center to the containing block. Centering horizontally a block within another block with CSS is typically done with:

#centerMe { margin: 0 auto; }

Note: on IE6 this requires that IE be in "standards compliant" rather than "quirks" mode. To force this always put a DOCTYPE on your HTML document. For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

Vertical centering is much worse.

You may want to look at Vertical Centering in CSS. This is actually a non-trivial problem. The vertical-align style property you're using only applies to table cells (in a cross-browser backwards compatible way). This would be trivial to do with a table for what it's worth.

这篇关于中心文本在div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆