简单HTML应该在页面中央显示div
为什么这个极其简单的网页不在页面中央显示id为“mainContent”的div 现在div在左边。但我希望div集中在网页(body元素)中 出什么事了简单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
<!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是对的,你应该去掉文本对齐:居中正文规则中的代码>。