【发布时间】:2014-05-14 19:32:16
【问题描述】:
我已阅读 Stackoverflow 上与此主题相关的所有其他问题,但似乎没有一个可以解决我的问题。
我的页面顶部有一个导航栏,它固定在左上角,然后在中间有一个 div,其中包含图像和一些文本。
我的内容是这样的:
<div class="center">
<div id="logo"> <a class="home" href="index.html"> <img class="noborder" src="images/logocat.png" alt="" /> </a>
</div>
<div id="top">
<p class="toptext">TEXT</p>
</div>
<div id="contact">
<table border="0" align="center">
<tr>
<td><a href="http://be.net/crookedcartoon"></td>
<td><p class="contact-text1"> CROOKEDCARTOON@GMAIL.COM </p>
<p class="contact-text2"> Alex: TEXT </p></td>
<td><a href="2014.html"><img class="noborder" src="images/seriesindex.jpg" onmouseover="this.src='images/serieshover.jpg'" onmouseout="this.src='images/seriesindex.jpg'" alt="" /></a>
</td>
</tr>
</table>
</div>
<div id="about">
<p class="content-text-index">TEXT</p>
</div>
</div>
提到的div 的 CSS 是:
.center {
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top:-492px;
margin-left:-257px;
padding:0px;
}
它不会垂直或水平居中,这个 div 周围没有 div 或包装器,或任何冲突的 css(据我所知),这变得很痛苦。
谁能提出解决办法?我已经尝试过半边距/宽度等想法,这就是上面显示的内容。
【问题讨论】:
-
为什么要使用表格布局?
-
因为我在这里读到的票数最高的技巧说绝对定位,固定高度和宽度+边距是这些值的一半,这将是使 div 居中的最佳方式,而不管浏览器大小如何.这些表格与问题无关,但是一旦解决,我现在正在更新它。
-
我注意到您在 CSS 中使用
#center而不是 '.center`。会这样吗? -
是的,这是一个关于类/ID的愚蠢混淆,但它肯定没有奏效,它创造了这个:crookedcartoon.co.uk/index.html
-
您之前的 CSS 是正确的。您所要做的就是将
#center更改为.center顺便说一句,<center>元素已被弃用。您应该只使用 div。