【发布时间】:2011-01-30 03:37:45
【问题描述】:
我创建的每个网页都有这个问题。我用来将内容放在页面中心的“主容器”div 上方总是有一个上边距。我正在使用 css 样式表并将正文中的边距和填充设置为 0px,并将 div 中的边距和填充设置为 0:
body{
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
margin-right: 0px;
padding: 0;
color: black;
font-size: 10pt;
font-family: "Trebuchet MS", sans-serif;
background-color: #E2E2E2;
}
div.mainContainer{
height: auto;
width: 68em;
background-color: #FFFFFF;
margin: 0 auto;
padding: 0;
}
我在网上看了很多次,但我所能做的就是设置这些边距和填充属性。还有什么我应该做的吗? IE和Firefox中存在margin。
这里是对代码的更全面的了解(它处于创建的初始阶段,所以没有太多内容......)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title></title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
<link href="../Styles/KB_styles1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="mainContainer">
<p>Here is the information</p>
</div>
</body>
</html>
这是 CSS:
@charset "utf-8";
/* CSS Document */
body{
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
margin-right: 0px;
padding: 0;
color: black;
font-size: 10pt;
font-family: "Trebuchet MS", sans-serif;
background-color: #E2E2E2;
}
/* ---Section Dividers --------------------------------------------------------------*/
div.mainContainer{
position: relative;
height: auto;
width: 68em;
background-color: #FFFFFF;
margin: 0 auto;
padding: 0;
}
div.header{
padding: 0;
margin: 0;
}
div.leftSidebar{
float: left;
width: 22%;
height: 40em;
margin: 0;
}
div.mainContent{
margin-left: 25%;
}
div.footer{
clear: both;
padding-bottom: 0em;
margin: 0;
}
/* Hide from IE5-mac. Only IE-win sees this. \*/
* html div.leftSidebar { margin-right: 5px; }
* html div.mainContent {height: 1%; margin-left: 0;}
/* End hide from IE5/mac */
【问题讨论】:
-
我没有看到任何奇怪的地方。您能否将此作为 SSCCE 重新发布? sscce.org,从
<doctype>和<html>直到</html>具有最小 所需的样式表以在<style>元素中重现相同的问题。 -
感谢您的帮助 - 我想我找到了问题所在。段落边距似乎正在转移到 div。当我将 p{margin: 0;} 添加到 css 时,问题就消失了。
-
@RoryG,请将您的解决方案作为答案发布在下面并接受您自己的答案。如您所见,这对仍在尝试回答您的原始问题的人最有帮助。