【问题标题】:Flexbox: Align-self: center doesn't work with position: fixed in firefoxFlexbox:对齐自我:中心不适用于位置:在Firefox中修复
【发布时间】:2017-07-20 10:44:22
【问题描述】:

我在 Firefox 中将 div 与 flexbox 水平对齐时遇到了问题。它在 Chrome 中运行良好。

这是代码:

<header id="header">
    <p class="small-hidden">I am</p>
    <h1>Håkan</h1>
    <p>web designer from Sweden, living in the Basque Country, Spain</p>
    <div class="contact-icons">
        <a href="#.com"><i class="mi mi-Mail"></i></a>
        <a href="#"><i class="mi mi-whatsapp"></i></a>
        <a href="#contact"><i class="mi mi-PenPalette"></i></a>
    </div>
</header>    
 --------------------------------------------------------------------------

header {
  background-image: url(../img/hakan_mobile.jpg);
  background-size: cover;
  height: 70vh;
  display: flex;
  flex-direction: column;
  justify-content:  flex-end;
  align-items: center;
  color: #777;
  }
@media only screen and (min-width: 40em) {
  header {
    background-image: url(../img/hakan.jpg);
    height: 100vh;
    width: 40vw;
    position: fixed;
    background-position: center;
    justify-content: center;
    }
  }
.contact-icons {
  position: fixed;
  top: 0;
  }
@media only screen and (min-width: 40em) {
  .contact-icons {
    bottom: 2rem;
    top: inherit;
    }
  }

我要做的是将文本(p + h1 + p)放在标题的下部,并且链接应该在小屏幕上居中并固定在屏幕顶部,并在底部居中更大的屏幕。但是,如果我使用position: fixed(在移动设备上向下滚动时需要保持链接在屏幕顶部可见),.contact-icons 将不会水平居中。如果我删除position: fixed,它会起作用。

我在容器上尝试了align-items: center,在.contact-icons 上尝试了align-self: center,但结果相同。

在 Chrome 中完美运行,但我无法在 Firefox 中运行。有什么建议吗?

小提琴:https://jsfiddle.net/1qrqxd7m/

在小提琴上它看起来几乎居中,但仍然不是。在我的工作副本上,它看起来更加偏离中心。

【问题讨论】:

  • 我很乐意提供帮助,但是你能在某个地方设置一个 sn-p 吗?例如将代码重新格式化为 SCSS 并将其放入 jsfiddle?还有什么是bp-large?你能把它清理一下吗?
  • 对不起!采用 CSS 并添加了 Fiddle...

标签: html css firefox flexbox


【解决方案1】:

也许对 Firefox 使用“position:sticky;”进行特殊尝试

【讨论】:

  • @Håkan your jsfiddle.net/1qrqxd7m 在 chrome 和 firefox 中看起来一样(
  • 是的,我现在意识到......但它仍然不是很好的工作项目。也许其他一些事情是相互矛盾的......
【解决方案2】:

由于您使用的是position: fixed,因此预计 flex 容器的样式不会影响 .contact-icons 的定位。

只需将它们相对于整个页面水平居中:

position: fixed
left: 50%
transform: translateX(-50%)

【讨论】:

  • 我的错...标题在大屏幕上不是全宽(但 40vw),使用它会使.contact-icons 在全屏上居中。此外,在较小的尺寸上,.contact-icons 中的图标从 50% 开始并相互堆叠......
  • 如果是 40vw,只需在 .contact-icons 上设置 left: 20vw。这个 sn-p 是通用的,可以在任何情况下使用,就像“将left 设置为父容器的一半,然后设置transform: translateX(-50%)”。
  • 它不像我想象的那样工作......它没有居中,但.contact-icons 容器从 50% 开始。
  • 问题是,如果我使用我的原始代码,但删除 position: fixed 它完全按照我想要的方式排列......但我失去了固定位置......
  • 您能否更新小提琴以说明您遇到的问题?否则我无法猜测出什么问题。我很确定我提供的代码是正确的,并且似乎足以弄清楚如何在您的情况下使用 left 属性。
猜你喜欢
  • 1970-01-01
  • 2019-02-09
  • 2017-12-10
  • 1970-01-01
  • 2018-09-15
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
相关资源
最近更新 更多