无法使CSS元素居中

无法使CSS元素居中,css,elements,Css,Elements,我不能让我的网站为我的生活与CSS为中心。我已经尝试了网络上建议的所有常用方法,包括: body { text-align: center; } #container { width: 770px; margin: 0 auto; text-align: left; } 然后使用 <div id="container> <!-- Centered Content Goes here--> </div> 因此,我的HTML将如

我不能让我的网站为我的生活与CSS为中心。我已经尝试了网络上建议的所有常用方法,包括:

body {
    text-align: center;
}
#container {
    width: 770px;
    margin: 0 auto;
    text-align: left;
}
然后使用

<div id="container>
<!-- Centered Content Goes here-->
</div>
因此,我的HTML将如下所示:

<div id="container>
<div id="topHeader></div>
<!-- All other elements go here as well-->
</div>

但正如我前面提到的,元素保持不变。 谢谢
埃里克

据我所知,它根本不起作用<代码>文本对齐使文本或内联内容居中,而不是块元素


编辑:另一方面,粉碎者的链接是有意义的。不幸的是,仅适用于固定大小的块。

据我所知,它根本不起作用<代码>文本对齐使文本或内联内容居中,而不是块元素


编辑:另一方面,粉碎者的链接是有意义的。不幸的是,仅适用于固定大小的块。

您放置的标题是绝对的,因此它与包含块(即主体)偏移,而不是与父元素偏移。你想要的是相对定位

绝对值

框的位置(可能大小)用“顶部”指定, “右”、“下”和“左” 财产。这些属性指定 相对于长方体的偏移量 包含块。绝对地 放置好的盒子从盒子中取出 正常流量。这意味着他们没有 对后期布局的影响 兄弟姐妹。而且,尽管绝对如此 定位框有边距,它们有边距 不会与任何其他利润一起崩溃

绝对:

<html>
    <head>
        <style type="text/css">
            body {
                text-align: center;
            }
            #container {
                color:blue;
                border:1px solid blue;

                width: 770px;
                margin: 0 auto;
                text-align: left;
            }
            #topHeader
            {
                color:red;
                border:1px solid red;

                position:absolute;
                width: 695px;
                height: 242px;
                top: 0px;
                left: 0px;
            }       
        </style>
    </head>
    <body>
        outside
        <div id="container">
            inside
            <div id="topHeader">deep inside</div>
            <!-- All other elements go here as well-->
        </div>
    </body>
</html>

身体{
文本对齐:居中;
}
#容器{
颜色:蓝色;
边框:1px纯蓝色;
宽度:770px;
保证金:0自动;
文本对齐:左对齐;
}
#上头
{
颜色:红色;
边框:1px纯红;
位置:绝对位置;
宽度:695px;
身高:242px;
顶部:0px;
左:0px;
}       
外部
在…内
内心深处
亲属:

<html>
    <head>
        <style type="text/css">
            body {
                text-align: center;
            }
            #container {
                color:blue;
                border:1px solid blue;

                width: 770px;
                margin: 0 auto;
                text-align: left;
            }
            #topHeader
            {
                color:red;
                border:1px solid red;

                position:relative;
                width: 695px;
                height: 242px;
                top: 0px;
                left: 0px;
            }       
        </style>
    </head>
    <body>
        outside
        <div id="container">
            inside
            <div id="topHeader">deep inside</div>
            <!-- All other elements go here as well-->
        </div>
    </body>
</html>

身体{
文本对齐:居中;
}
#容器{
颜色:蓝色;
边框:1px纯蓝色;
宽度:770px;
保证金:0自动;
文本对齐:左对齐;
}
#上头
{
颜色:红色;
边框:1px纯红;
位置:相对位置;
宽度:695px;
身高:242px;
顶部:0px;
左:0px;
}       
外部
在…内
内心深处

您放置的标题是绝对的,因此它是从包含块(即主体)偏移的,而不是从父元素偏移的。你想要的是相对定位

绝对值

框的位置(可能大小)用“顶部”指定, “右”、“下”和“左” 财产。这些属性指定 相对于长方体的偏移量 包含块。绝对地 放置好的盒子从盒子中取出 正常流量。这意味着他们没有 对后期布局的影响 兄弟姐妹。而且,尽管绝对如此 定位框有边距,它们有边距 不会与任何其他利润一起崩溃

绝对:

<html>
    <head>
        <style type="text/css">
            body {
                text-align: center;
            }
            #container {
                color:blue;
                border:1px solid blue;

                width: 770px;
                margin: 0 auto;
                text-align: left;
            }
            #topHeader
            {
                color:red;
                border:1px solid red;

                position:absolute;
                width: 695px;
                height: 242px;
                top: 0px;
                left: 0px;
            }       
        </style>
    </head>
    <body>
        outside
        <div id="container">
            inside
            <div id="topHeader">deep inside</div>
            <!-- All other elements go here as well-->
        </div>
    </body>
</html>

身体{
文本对齐:居中;
}
#容器{
颜色:蓝色;
边框:1px纯蓝色;
宽度:770px;
保证金:0自动;
文本对齐:左对齐;
}
#上头
{
颜色:红色;
边框:1px纯红;
位置:绝对位置;
宽度:695px;
身高:242px;
顶部:0px;
左:0px;
}       
外部
在…内
内心深处
亲属:

<html>
    <head>
        <style type="text/css">
            body {
                text-align: center;
            }
            #container {
                color:blue;
                border:1px solid blue;

                width: 770px;
                margin: 0 auto;
                text-align: left;
            }
            #topHeader
            {
                color:red;
                border:1px solid red;

                position:relative;
                width: 695px;
                height: 242px;
                top: 0px;
                left: 0px;
            }       
        </style>
    </head>
    <body>
        outside
        <div id="container">
            inside
            <div id="topHeader">deep inside</div>
            <!-- All other elements go here as well-->
        </div>
    </body>
</html>

身体{
文本对齐:居中;
}
#容器{
颜色:蓝色;
边框:1px纯蓝色;
宽度:770px;
保证金:0自动;
文本对齐:左对齐;
}
#上头
{
颜色:红色;
边框:1px纯红;
位置:相对位置;
宽度:695px;
身高:242px;
顶部:0px;
左:0px;
}       
外部
在…内
内心深处
试试这个 试试这个

尝试将其添加到css文件的顶部:

// Wipes out any preexisting padding and margin.
html, body {
    padding: 0;
    margin: 0;
}

然后添加一个位置:相对;指向要居中的类的指令。实际上,试着将它添加到html,body one中,这样所有类都使用相对位置。这可能是你的立场:绝对;然后与左侧组合的集合:0px;强制页眉包含忽略页边距:0自动;并停留在页面的左侧。

尝试将此添加到css文件的顶部:

// Wipes out any preexisting padding and margin.
html, body {
    padding: 0;
    margin: 0;
}

然后添加一个位置:相对;指向要居中的类的指令。实际上,试着将它添加到html,body one中,这样所有类都使用相对位置。这可能是你的立场:绝对;然后与左侧组合的集合:0px;强制页眉包含忽略页边距:0自动;并停留在页面的左侧。

主要问题是
#topHeader
元素的绝对位置。因为它绝对位于
top:0px;左:0px,这是e