css - HTML Position DIV 标签到绝对TOP

标签 css html position absolute

.myDiv  
{

background-image: url(urltomyimgheader);

height:100px;

margin: auto;

position:relative;

top:-10px;

width:600px;

}

这是一个简单的 div 标签,用于显示 Logo ...

我将顶部设置为 -10px 以使其触及浏览器的顶部,除非 Logo 和页面之间存在间隙。

但是我对使用 -10px 来定位 div 感觉很糟糕 :( 有更好的跨浏览器兼容的解决方案吗?

最佳答案

@Tatu Ulmanen 是正确的,这是一个浏览器默认问题。您不必添加负边距,只需设置 body/html 标签的默认 CSS 属性即可。

为了安全起见,您应该使用 CSS 重置来摆脱浏览器样式默认设置。这是您可以用来解决问题的代码。下面是一个漂亮的 CSS 重置,您可以从现在开始将其添加到所有 CSS 样式表的顶部。

html, body {
padding: 0;
margin: 0;
}

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, font, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td
                        { margin: 0; padding: 0; border: 0; outline: 0; font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit; vertical-align: baseline; }
    blockquote:before,
    blockquote:after,
    q:before, q:after   { content: ""; }
    blockquote, q       { quotes: "" ""; }
    body                { line-height: 1; color: black; background: white; }
    caption, th, td     { text-align: left; font-weight: normal; }
    :focus              { outline: 0; }
    table               { border-collapse: separate; border-spacing: 0; }
    ol, ul              { list-style: none; }

关于css - HTML Position DIV 标签到绝对TOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2685105/

相关文章:

html - 如何将图像放入div?

css - 一页上的粘滞按钮

javascript - 如何修复滚动侧边栏?

html - CSS:使用边距/填充将 Div 拉伸(stretch)到其父级高度的 100%

html - mac 和 windows FF/Chrome 之间的相同父 div 的顶部位置绝对不同

css - 如何固定内部有 LOTS 元素的位置

css - IE7 和下拉列表 - 添加某种边距

html - Internet Explorer 11 通过元标记禁用 "display intranet sites in compatibility view"不起作用

html - AWS Cloudfront视频速度加载

javascript - 防止固定位置的背景在单击事件时跳转到页面顶部?