CSS - “position: fixed” acting like “absolute” in Firefox

淺唱寂寞╮ 提交于 2019-12-01 02:26:38

Through the process of elimination I was able to determine that having the following in my body was causing all the problems with fixed divs in Firefox:

-o-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;

I had originally added this code to prevent flickering in certain CSS transitions throughout the site, but I guess I'll have to add it to each individual class now.

I had the exact same problem, turns out the following CSS property of a parent element was causing the problem.

transform: translate3d(0px, 0px, 0px);

It appears that some browsers will will apply fixed positioning relative to the window, while Firefox is applying it relative to the <body />. You need to make your body 100% tall:

body {
    height: 100%;
}

But the margin from your .header is collapsing outside of the body element. Change this:

margin: 25px auto;

to this:

margin: 0 auto; /* updated - thanks JoshC */
padding: 25px auto;

I solved the issue by moving the element that uses position: fixed; out of its original parent element that uses transform: translateX(-50%);.

Thus...

<div class="transformed-container">
   <div="fixed-element"></div>
</div>

...became...

<div class="transformed-container"></div>

<div class="fixed-element"></div>

Two things led me to this conclusion:

  1. @Pankaj's answer shows that the translate value can cause an issue.
  2. @Wildhoney's comment to another answer references an explanation of the underlying cause: http://meyerweb.com/eric/thoughts/2011/09/12/un-fixing-fixed-elements-with-css-transforms/

The problem seems to be in your body, i've added width:100%; height:100%; and overflow:hidden; to it in my fire fox and it looked just fine, except for the bottom menu-bar that went half of it's height over the bottom.

Not sure why the browsers were rendering differently, though the solution is pretty simple. You need to give the parent elements (html/body) a height of 100% in order to fill the entire page. It seems like FF rendered the fixed elements at the bottom of the contents as opposed to the bottom of the window. Adding the following will make it work across browsers:

html, body {
    height: 100%;
}

In addition, you should also use padding on .header element as opposed to a margin. This will solve another issue.

.header {
    margin: 0 auto;    /* use a value of 0 rather than 25px */
    padding: 25px 0;
}

I tested all this in the browser, it will work in FF now. It should also render properly in Chrome and others.

Beltran

I needed to remove some css classes from the superior container of the fixed-on-scroll element that had a transition, from the animateCSS library.

$(window).on('scroll', function () {
     if (distance <= 65) {
        $('#my-contaniner').removeClass('animated fadeInLeft'); //delete problematic classes for FF
Add your code
 });

Maybe it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!