Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单HTML应该在页面中央显示div_Html_Css_Alignment - Fatal编程技术网

简单HTML应该在页面中央显示div

简单HTML应该在页面中央显示div,html,css,alignment,Html,Css,Alignment,为什么这个极其简单的网页不在页面中央显示id为“mainContent”的div 现在div在左边。但我希望div集中在网页(body元素)中 出什么事了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en-US" xml:lang="en-US" xmlns

为什么这个极其简单的网页不在页面中央显示id为“mainContent”的div

现在div在左边。但我希望div集中在网页(body元素)中

出什么事了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> </title>

    <style type="text/css">
    <!--
        body {
            text-align: center;
            background-color: red;
        }

        #mainContent {
            width: 800px;
            background-color: blue;
        }

        #content {
            width: 600px;
            float: left;
            background-color: green;

            overflow: hidden;
        }

        #sidebar {
            width: 200px;
            float: left;
            background-color: yellow;

            overflow: hidden;
        }
    -->
    </style>
</head>
<body>

    <div id="mainContent">
        <!-- Left Column/Bar -->
        <div id="content">
            <p>content</p>
        </div>

        <!-- Right Column/Bar -->
        <div id="sidebar">
            <p>sidebar</p>
        </div>
    </div>

</body>
</html>

内容

边栏


您只需添加
页边距:0自动使其顶部和底部的边距为0,并自动设置左侧和右侧的边距

    #mainContent {
        width: 800px;
        margin: 0 auto;
        background-color: blue;
    }

您只需添加
margin:0 auto使其顶部和底部的边距为0,并自动设置左侧和右侧的边距

    #mainContent {
        width: 800px;
        margin: 0 auto;
        background-color: blue;
    }
试试看<代码>文本对齐:居中
居中文本,不一定是页面上的所有元素


试试看<代码>文本对齐:居中
居中文本,不一定是页面上的所有元素。

您还必须更改:

body {
        text-align: center;
        background-color: red;
    }
到那

body {
        background-color: red;
    }

你还必须改变这一点:

body {
        text-align: center;
        background-color: red;
    }
到那

body {
        background-color: red;
    }

请说明呈现页面的浏览器这可能有助于您:提示:不要使用颜色名称,请写入十六进制值。这可能对你有帮助:提示:不要使用颜色名称,写十六进制值。它已被弃用。请阅读:@merianos nikos是正确的,您应该去掉
text align:center在正文规则中。@merianos nikos是对的,你应该去掉
文本对齐:居中