2013-05-17 50 views
-1

我需要垂直居中我的黃色方塊的幫助。絕對定位元素的孩子的垂直居中不起作用

<html> 
<head> 
    <title></title> 
</head> 
<body style="border-collapse: collapse;"> 
    <div style="margin: 100px; width: 100px; height: 200px; background-color: gray; border: 1px solid gray;"> 
     <table style='background-color: white; width: 100%; height: 100%; border-collapse: collapse; vertical-align: top;'> 
      <tr> 
       <td style='height: 33.33%;'> 
        <div style="position: relative; width: 100%; height: 100%;"> 
         <div style="position: absolute; top: -50%; height: 100%; display: table-cell; vertical-align: middle;"> 
          <div style="border: 1px solid #ccc; width: 30px; height: 30px; background-color: yellow; ">A</div> 
         </div> 
         <div style="position: absolute; bottom: -50%; height: 100%; display: table-cell; vertical-align: middle;"> 
          <div style="border: 1px solid #ccc; width: 30px; height: 30px; background-color: yellow; ">B</div> 
         </div> 
        </div> 
       </td> 
      </tr> 
      <tr> 
       <td style="height: 33.33%;"> 
        <div style="position: relative; width: 100%; height: 100%;"> 
         <div style="position: absolute; bottom: -50%; height: 100%; display: table-cell; vertical-align: middle;"> 
          <div style="border: 1px solid #ccc; width: 30px; height: 30px; background-color: yellow; ">C</div> 
         </div> 
        </div> 
       </td> 
      </tr> 
      <tr> 
       <td style="height: 33.33%;"> 
        <div style="position: relative; width: 100%; height: 100%;"> 

         <div style="position: absolute; bottom: -50%; height: 100%; display: table-cell; vertical-align: middle;"> 
          <div style="border: 1px solid #ccc; width: 30px; height: 30px; background-color: yellow; ">D</div> 
         </div> 
        </div> 
       </td> 
      </tr> 
     </table> 
    </div> 
</body> 
</html> 

回答

0

通過爲每個框添加margin-top: 50%;得到修復。