【问题标题】:Unwanted margin in all browsers except Firefox除 Firefox 之外的所有浏览器中的多余边距
【发布时间】:2011-11-07 17:21:19
【问题描述】:

所以,我已经开始为我的投资组合网站制定一些基础知识。我已经完成了导航栏,但是现在在测试跨浏览器时,它在导航下方显示了几个像素的边距。这发生在除 Firefox 之外的所有浏览器中。这让我有点困惑,因为元素都有绝对定位。

HTML:

<div id="topnav">
    <ul>
        <li><a href="index.html" id="current">Home</a></li>
        <li><a href="about.html">About Me</a></li>
        <li><a href="portfolio.html">Portfolio</a></li>
        <li><a href="contact.html" class="lastli">Contact Me</a></li>
    </ul>
</div>

这个 div 位于一个容器内,该容器有 800 宽,顶部边距 20 像素,两侧为自动。 内边距和边距设置为 0。“top:195”用于将条放置在我的标题图像(即 800x195)下方。另请注意:列表项具有 1x20 背景渐变图像,设置为 repeat-x。

CSS:

#topnav {
    width:800px;
    position:absolute;
    top:194px;
    height:20px;
}

#topnav ul {
    list-style-type:none;
    text-align:center;
    height:20px;
    margin:0;
    padding:0;
}

#topnav ul li {
    display:inline;
    font-size:15px;
    line-height:20px;
}

#topnav ul li a {
    padding:0 10px;
    position:relative;
    border-left:1px solid white;
    margin-left:-6px;
}

注意,我曾多次尝试将高度设置为 20px,但似乎都没有任何效果。我也改了字体,没用。 截图在这里:http://imgur.com/a/DKkDy 顶部是 Firefox(正如我所希望的那样),底部是 Chrome。 这是一个 JSFiddle:http://jsfiddle.net/SUmF7/

【问题讨论】:

    标签: html css cross-browser


    【解决方案1】:

    您是否尝试过在样式表中设置 CSS 重置?

    /* http://meyerweb.com/eric/tools/css/reset/ 
     v2.0 | 20110126
     License: none (public domain)
    */
    
    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;
    }
    

    【讨论】:

    • 好主意,我刚刚试过了。它解决了其他一些小问题,但没有解决导航栏的问题。下面还有 1-2px 的边距(Firefox 除外)。不过,谢谢。
    【解决方案2】:

    尝试将display: inline-block 添加到您的标签元素。 topnav lia 都设置为 inline 元素,这意味着它们不会扩展到您正确设置的尺寸。

    【讨论】:

    • 成功了.. 非常感谢。我仍然对 Firefox 如何正确显示元素感到奇怪,而所有其他浏览器则不会。不管怎样,谢谢。
    • 很高兴能帮上忙!其他浏览器如何渲染它有点意思:内联元素就像软包,其尺寸由其中的内容定义,但块元素是可以指定尺寸的盒子。
    【解决方案3】:

    您是否尝试过在容器 div 上设置位置样式?

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 2011-05-07
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多