.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/