2010-09-08 96 views
1

我在<td> a <div>和一些文本中有两个元素。我想将<div>顶部和文本到<td>的中间我该怎么做?内部元素的垂直对齐

编辑:应该垂直顶部对齐,而不是div内的文本。

回答

0

是否这样?

<html> 
<body> 
    <table width="100%" height="100%" border="1"> 
     <tr> 
      <td><div style="text-align: center;">some text</div></td> 
     </tr> 
    </table> 
</body> 
</html> 

...或者

<html> 
<body> 
    <table width="100%" height="100%" border="1"> 
     <tr> 
      <td align="center"><div style="text-align: left;">div text</div>some text</td> 
     </tr> 
    </table> 
</body> 
</html> 

记得设置车身高度,所以你可以在默认情况下看到的垂直居中

+0

垂直对齐!并且文本在该div之外。 – Mayur 2010-09-08 07:23:53

+0

默认情况下,文本在表格中垂直对齐,不需要valign =“center”,但是如果您喜欢,可以添加它:) – 2010-09-08 07:29:45

0

表中的任何文字垂直神韵:,不知道为什么你需要一个div。它很难获得内容,一个div内垂直allign

1

试试这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title></title> 
    <style type="text/css"> 

     html,body,h1,h2,h3,h4,h5,p,ul,li,form,button { margin:0; padding:0 } 
     body { font:normal 62.5% tahoma } 

     .my-table { height:300px } 
     .my-cell { position:relative; border:1px solid green } 
     .my-div { position:absolute; top:0; left:0; border:1px solid red } 

    </style> 
</head> 
<body> 

    <table class="my-table"> 
     <tr> 
      <td class="my-cell" align="center"> 

       <div class="my-div"> 
        I'm aligned to the top 
       </div> 

       This text is vertically centered. 

      </td> 
     </tr> 
    </table> 

</body> 
</html>