无法使 CSS 元素居中
我无法用 CSS 让我的网站以我的生活为中心。我已经尝试了网络上建议的所有常用方法,包括:
body {
text-align: center;
}
#container {
width: 770px;
margin: 0 auto;
text-align: left;
}
然后使用
<div id="container>
<!-- Centered Content Goes here-->
</div>
但它不会去中心。它位于页面的左侧。
我想要居中的元素的 CSS 示例如下:
#topHeader
{
background:url(images/top_header.jpg);
position:absolute;
width: 695px;
height: 242px;
top: 0px;
left: 0px;
}
因此,我的 HTML 将如下所示:
<div id="container>
<div id="topHeader></div>
<!-- All other elements go here as well-->
</div>
但正如我之前提到的,该元素保持不变。 谢谢! 埃里克
I cannot get my site to be centered for the life of me with CSS. I have tried all the usual methods suggested around the web including:
body {
text-align: center;
}
#container {
width: 770px;
margin: 0 auto;
text-align: left;
}
Then using
<div id="container>
<!-- Centered Content Goes here-->
</div>
But it just wont go to the center. It stays at the left side of the page.
An example of the CSS for the element that I want to be centered is this:
#topHeader
{
background:url(images/top_header.jpg);
position:absolute;
width: 695px;
height: 242px;
top: 0px;
left: 0px;
}
So, my HTML would look like this:
<div id="container>
<div id="topHeader></div>
<!-- All other elements go here as well-->
</div>
But as I mentioned before, the element stays put.
Thanks!
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试用这个
死亡中心
Try with this
dead centre
主要问题是
#topHeader
元素的绝对定位。因为你已经用top: 0px; 绝对定位了它left: 0px;
,这正是它要定位的位置 - 页面的左上角。首先从
#topHeader
元素中删除绝对定位。The primary issue is the absolute positioning of your
#topHeader
element. Because you have it absolutely positioned withtop: 0px; left: 0px;
, that's exactly where it's going to be positioned - at the top left of the page.Start off by removing the absolute positioning from the
#topHeader
element.尝试将其添加到 css 文件的顶部:
然后添加一个position:relative;指向您想要居中的班级。实际上,尝试将其添加到 html 正文之一,以便所有类都使用相对位置。可能你有position:absolute;设置然后与左侧组合:0px;强制标题包含忽略边距:0 auto;并停留在页面左侧。
Try adding this to the top of your css file:
Then add a position: relative; directive to the class you want centered. Actually, try adding it to the html, body one so that all your classes use relative position. It might be that you have position: absolute; set which then combines with the left: 0px; to force your header contain to ignore the margin: 0 auto; and stay on the left of the page.
您绝对放置标题,以便它从包含块(即主体)而不是父元素中偏移。你想要的是相对定位。
绝对:
相对:
You're placing the header absolutely so it's being offset from the containing block (i.e. body), not the parent element. What you want is Relative positioning.
Absolute:
Relative:
尝试所有这些居中方法时要检查的一件事是确保您的文档类型对于正在使用的方法是正确的。
希望这对您有帮助。
One thing to check when trying out all of these methods of centering is to make sure that your doctype is correct for the method that is being used.
Hope this helps for you.
据我所知,这根本行不通。
text-align
居中文本或内联内容,而不是块元素。编辑: 另一方面,分解者的链接是有道理的。不幸的是,仅适用于固定大小的块。
As far as I know it simply doesn't work.
text-align
centers text or inline content, not block elements.Edit: On the other hand The Disintegrator's link makes sense. Unfortunately, only for fixed-sized blocks.