出现 CSS 未知边距/填充

标签 css html margin padding

我的页眉导航 ID 标签之间似乎有未知的边距或填充。

这里是 div 标签:

<div id="container">

     <div id="header">
          <p align="center">HEADER</p>
     </div>

     <div id="navi">
          <p align="center">NAVIGATION</p>
     </div>

</div>

附带的样式表:

#container
{
    width: 800px;
    background-color: #AFAF97;
    margin: 0px auto;
}

#header
{
    height: 75px;
    width: 100%;
    background-color: #888888;
}

#navi
{
    height: 50px;
    width: 100%;
    background-color: #aaaaaa;
    /*margin-top: -16px;*/
}

这是未知边距/填充的屏幕截图。 http://imgur.com/l37DVFc

另一种解决方法是设置一个负的 margin-top 值。但它为什么以及如何存在?

最佳答案

始终使用重置样式

* {
    margin: 0;
    padding: 0;
}

Using a CSS Reset, CSS authors can force every browser to have all its styles reset to null, thus avoiding cross-browser differences as much as possible.

更高级的 css 重置。

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

固定:demo

关于出现 CSS 未知边距/填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488113/

相关文章:

javascript, css 过渡函数

ios - 带负边距的全宽菜单在 safari 和 ios 上提供无限水平滚动

html - 设置带有背景图片的提交表单按钮?

html - Bootstrap maxcdn 影响我的 CSS。请帮我解决

css - 仅使用 CSS 从中心旋转三 Angular 形图像?

php - Index.php 无法在 Facebook 和搜索引擎中正确显示

javascript - 在 Angular 7 中实现 HTML CSS 和 Javascript 模板

html - 跨浏览器兼容的 SVG 大小调整

html - Firefox 9 (Mac) 解析边距有问题?

html - 为什么 1px 的边框会抵消 100px 的 margin-top 并杀死滚动条?