4

我有一个非常简单的问题,但我似乎无法解决它。我在 div 中有一个表格,这是主要元素。现在我在表格中有一个 td 标签,其宽度和高度设置为 iphone 屏幕尺寸。现在我基本上想将该表与 div 标记内的 td 单元格居中,以便“屏幕”将在任何浏览器窗口中居中。我怎么做?

用表格替换那个 div 解决了我的问题,但我想用一个 div 代替。

谢谢你

4

5 回答 5

8

试试这个,我做这个代码 -演示在这里,非常简单的代码(我只为 IE 旧版本使用了 hack 代码和额外的 span 标签) http://jsfiddle.net/ERuX4/1/

<div class="demo wraptocenter">
   <span></span>
   <img src="http://www.spitzer.caltech.edu/images/twitter.png?1295473781" alt="" />
</div> 

css

.wraptocenter * {
    vertical-align: middle;
}
.wraptocenter span {
    display: inline-block;
    height: 100%;
    width: 1px;
}
.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

/*End wraptocenter - image Vcenter  patch styles*/    
.demo  {
         width:100px;
         height:100px;
         border:1px solid red;
         position:absolute;
         top:50%;
         left:50%;
         margin-top:-100px;
         margin-left:-100px;}
于 2012-04-04T11:20:34.007 回答
0
<style type="text/css">
.outer { width:800px; height:640px; }
.inner {
    position:relative; top:50%; left:50%;
    width: 320px;
    margin-left: -160px; /* half the width */
    height: 240px;
    margin-top: -120px; /* half the height */
</style>

<div class="outer">
    <div class="inner">I'm centered!</div>
</div>
于 2012-04-04T10:48:59.500 回答
0

试试这个:

div#centerlayout 
{ 
    position:absolute; 
    left:50%; 
    top:50%; 
    height:XXXpx; 
    width:XXXpx; 
    margin-top:-XXXpx; 
    margin-left:-XXXpx; 
} 
于 2012-04-04T10:50:00.030 回答
0

HTML

    <div class="img_thumb">
    <table>
        <tr><td>adfasfdasf</td></tr>
        <tr><td>adfasfdasf</td></tr>
        <tr><td>adfasfdasf</td></tr>
        <tr><td>adfasfdasf</td></tr>
        <tr><td>adfasfdasf</td></tr>
    </table>
</div>

CSS

.img_thumb {
    background: none repeat scroll 0 0 red;
    display: table-cell;
    height: 200px;
    vertical-align: middle;
    width: 200px;

}
table {
    border: 1px solid;
    width: 100px;
    margin:auto;
}

或查看演示:- http://jsfiddle.net/QHEnL/16/

于 2012-04-04T11:14:19.987 回答
0

我没有在 IE 中测试过(在 FF 和 Safari 中工作),但是如果你使用 jQuery,你可以使用它来page使页面中的类居中 div:

var pageMargin = ($(document).width() - $("div.page").width()) / 2;
$("div.page").css("margin-left", pageMargin).css("margin-right", pageMargin);
于 2012-06-01T21:01:40.283 回答