html - Safari 中的缩放问题 - 为什么 Border-top 会修复它,还有其他方法吗?

标签 html css safari

我遇到了一个奇怪的问题,我只在 Safari 中遇到过(Mac 上的 7 - 不知道 Windows)。

当用户缩小时,标题上方会出现一个空格。在 100%(无缩放)或放大时,没有空间。

根据过去的经验,我决定尝试添加 padding-top(没有用)和 border-top,它们确实有效。

所以 - 我希望这里有人可以向我解释:

  1. 你知道为什么 border-top 会解决这个问题吗?
  2. 这是 Safari 的错误,还是我误解了什么?
  3. 是否有更好的方法来解决这个问题?对于这个特定的客户来说,边界顶部会有问题, 我想找到一个更清洁的解决方案。

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: 16pxmargin-bottom: 16px<ul>元素。

ul margin analysis

所以要修复它,只需做

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

comparison

没有边框,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/

相关文章:

javascript - 当兄弟存在时,为什么 nextElementSibling 和 nextSibling 为 null?

html - 为什么我的子选择器 #id > .test 没有效果?

php - 如何包含和读取 XML 的 HTML?

HTML &lt;style&gt; 在 bash 脚本中的使用

javascript - Safari 中的 SVG 组元素不会触发 Wheel 事件

html - W3 CSS 验证错误解析错误

html - 如何在兄弟悬停时显示元素?

html - 垂直居中两个div

javascript - Videojs-contrib-hls 在 safari 上返回未定义

google-chrome - 如何知道客户端使用哪个 websocket 版本?