【问题标题】:Display: flex and Position: absolute conflict显示:flex 和 Position:绝对冲突
【发布时间】:2017-05-21 11:14:30
【问题描述】:

这是问题的screenshot
到底部:1 - Chrome,2 - Opera,3 - Firefox,4 - Edge。

下面我写了可执行代码。
为方便起见,这里codepen

基本理念:图库标签和徽标位于中心位置,其余位于两侧。
Transform 将用于动画。

如果您能帮助了解如何在 Firefox 中维护 flex,我将不胜感激。
更准确地说,我认为 align-items: center 在 Firefox 中没有正确显示。请帮忙。

.gallerypage {
  margin: 0;
  padding: 0;
  background: #fff;
}

.gallerypage .header {
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-flex-flow: row nowrap;
      -ms-flex-flow: row nowrap;
          flex-flow: row nowrap;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
      -ms-flex-align: center;
          align-items: center;
  width: 100%;
  height: 100px;
  background: gray;
}

.gallerypage .header a {
  cursor: pointer;
  position: absolute;
  -webkit-flex-shrink: 0;
      -ms-flex-negative: 0;
          flex-shrink: 0;
  text-align: center;
  white-space: pre-wrap;
  text-indent: -6px;
  line-height: 30px;
  font-family: "Kaushan Script", cursive;
  font-size: 30px;
}

.gallerypage .header .sketches {
  background-color: #fff;
  width: 110px;
  -webkit-transform: translate(-400px, 0px);
      -ms-transform: translate(-400px, 0px);
          transform: translate(-400px, 0px);
}

.gallerypage .header .smth {
  background-color: #fff;
  width: 140px;
  height: 60px;
  -webkit-transform: translate(-200px, 0px);
      -ms-transform: translate(-200px, 0px);
          transform: translate(-200px, 0px);
}

.gallerypage .header .gallery {
  position: static;
  text-indent: -14px;
  line-height: normal;
  width: 185px;
  height: 90px;
  background: gold;
  font-size: 60px;
}

.gallerypage .header .logo {
  position: static;
  width: 60px;
  height: 80px;
  background: hotpink;
}

.gallerypage .header .logo .vs-logo {
  display: block;
  -webkit-flex-shrink: 0;
      -ms-flex-negative: 0;
          flex-shrink: 0;
  width: 60px;
  height: 80px;
}

.gallerypage .header .blog {
  background-color: #fff;
  width: 60px;
  height: 38px;
  -webkit-transform: translate(200px, 0px);
      -ms-transform: translate(200px, 0px);
          transform: translate(200px, 0px);
}

.gallerypage .header .about {
  white-space: pre-wrap;
  background-color: #fff;
  width: 100px;
  -webkit-transform: translate(400px, 0px);
      -ms-transform: translate(400px, 0px);
          transform: translate(400px, 0px);
}
<div class="gallerypage">
  <div class="header">
    <a class="sketches">Sketches</a>
    <a class="smth">Something serious</a>
    <a class="gallery">Gallery</a><a class="logo"></a>
    <a class="blog">Blog</a><a class="about">About it</a>
  </div>
/<div>

【问题讨论】:

  • @Michael_B,不,这是另一个问题。
  • @VostokSisters,我认为这是同样的问题。因为您的绝对定位的 flex 项目不会从 Firefox 中的正常流程中删除,所以它们的对齐方式与其他浏览器不同,在其他浏览器中,绝对定位按预期工作。
  • 我说的不是这个。 在 Firefox 中,绝对定位的弹性项目不会从文档流中删除。 这意味着它们仍被视为流内弹性项目并占用容器中的空间。这将导致与 Chrome 和其他浏览器不同的布局。

标签: html css firefox flexbox


【解决方案1】:

Firefox 解决了垂直和水平对齐问题。我还应用了一个简单的动画,以确保它与您正在尝试做的事情兼容(悬停在标题上以转换其他项目)。

链接:http://codepen.io/anon/pen/YNXNLN

要在firefox上修复垂直居中对齐,添加这个样式(我的意思是合并,因为你已经使用了transform属性):

a {
 transform: translateY(-50%);
}

但是它会被定位在 Chrome 的最顶部,所以你需要添加这个样式来修复它:

a {
 top: 50%;
}

水平对齐“左:50%”也是如此,并且您已经使用 translateX,因此该值将取决于您。

【讨论】:

  • 它只是工作,基本上......谢谢你的回答!但在帖子中我写道,变换将用于动画。我想默认设置,元素位于标题的中心。当你将header悬停时,应该是从header中心出现的过渡动画类。
  • 但实际上不,这不是我要找的...Link
  • 这绝对是我想要的。太感谢了!它可以在 Chrome 和 Firefox 上运行。但老实说,我也有decided 我的问题,而且它没有弹性。 :D 稍后我也会在答案中添加我的决定。
  • 我同意,flexbox 非常有用,但最好不要将它与绝对定位混合使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 2017-04-23
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多