我遇到了一个奇怪的问题,我只在 Safari 中遇到过(Mac 上的 7 - 不知道 Windows)。
当用户缩小时,标题上方会出现一个空格。在 100%(无缩放)或放大时,没有空间。
根据过去的经验,我决定尝试添加 padding-top(没有用)和 border-top,它们确实有效。
所以 - 我希望这里有人可以向我解释:
- 你知道为什么 border-top 会解决这个问题吗?
- 这是 Safari 的错误,还是我误解了什么?
- 是否有更好的方法来解决这个问题?对于这个特定的客户来说,边界顶部会有问题, 我想找到一个更清洁的解决方案。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Title</title>
</head>
<body>
<div id="headerwrapper">
<div id="headerliner">
<header>
<nav class="menu">
<ul>
<li><a href="#">Top Level Menu Item</a>
<ul class="sub-menu"><li><a href="#" >Child Menu Item</a></li></ul>
</li>
</ul>
</nav>
</header>
</div>
</div>
</body>
</html>
CSS:
html, body {
margin: 0;
padding: 0;
}
#headerwrapper {
margin: 0;
padding: 0;
}
#headerliner {
margin: 0;
padding: 0;
}
header {
background: black;
margin: 0 auto;
padding: 1px 0 0 0;
/* Remove the below line, and the space appears in Safari */
border-top: 1px solid black;
}
最佳答案
有没有更好的方法来解决这个问题?
除了使用一些 border-top
之外,找到解决此问题的方法hack,让我们检查元素并找出问题的根本原因。缩小时上面有空格的原因是因为默认情况下,Safari 给出了 margin-top: 16px
和 margin-bottom: 16px
至 <ul>
元素。
所以要修复它,只需做
ul {
margin-top: 0;
}
参见 full page demo . ( fiddle here )
根据您的评论:
the issue I am facing on the full production version of this code is that the nav is supposed to be 36px down from the top of the header / wrapper element, so that margin is specific, intentional, and necessary
您始终可以使用 padding-top
在你的 <nav>
元素而不是 margin-top
在 <ul>
(demo)。
nav.menu {
padding-top: 36px;
}
为什么 border-top 会解决这个问题?这是 Safari 的错误吗?
知道为什么border-top
修复它,让我们看看 W3C specifications on collapsing margins :
Collapsing margins
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. ... Adjoining vertical margins collapse... Horizontal margins never collapse.
Two margins are adjoining if and only if ... both belong to in-flow block-level boxes that participate in the same block formatting context no line boxes, no clearance, no padding and no border separate them ...
这是关于 <header>
的两种情况的比较和 <ul>
.
没有边框,margin-top
就没有什么可分隔的了的 <header>
(父级)和 <ul>
的( child ),和the collapsed margin ends up outside the parent ,导致出现空白。但是,使用 border-top
将它们分开,边距不再折叠并且 margin-top
的 <ul>
最终在父级内部,空间消失。
关于html - Safari 中的缩放问题 - 为什么 Border-top 会修复它,还有其他方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942449/