175

I am using -webkit-transform (and -moz-transform / -o-transform) to rotate a div. Also have position fixed added so the div scrolls down with the user.

In Firefox it works fine, but in webkit based browsers it's broken. After using the -webkit-transform, the position fixed doesn't work anymore! How is that possible?

Joshua
  • 3,055
  • 3
  • 22
  • 37
iSenne
  • 2,656
  • 5
  • 26
  • 26
  • 4
    A demo page often helps people answer questions - jsbin.com lets you make temporary pages to illustrate the problem if you don't want to link to your site. – Rich Bradshaw Apr 14 '10 at 11:53
  • jsfiddle.net is another good example of a temporary editing bin. – Kyle Apr 14 '10 at 12:04
  • @Rich Bradshaw jsbin.com is very nice. Didn't know it until now. Most of my projects I run local, so I will use it next time. Tnx – iSenne Apr 15 '10 at 08:57
  • @iSenne You can easily apply a workaround by defining the fixed element directly on the body or adding it to the body with jQuery/JavaScript. – Student of Hogwarts Feb 20 '13 at 12:28
  • As of 06/2013 it's always broken for my Webkit 28.x.x, here the test http://jsfiddle.net/molokoloco/zhTR4/ – molokoloco Jun 29 '13 at 00:21
  • 8
    It **doesn't work** fine in Firefox at all. – vsync Aug 12 '14 at 15:27
  • See also [SO: webkit css 'transform3d' + 'position: fixed' issue](http://stackoverflow.com/questions/15194313/webkit-css-transform3d-position-fixed-issue). This is tentatively part of the W3C spec, which would make it the correct behavior for the browser (but the [spec definitions](https://www.w3.org/Bugs/Public/show_bug.cgi?id=16328) are in flux). – thirdender Dec 11 '14 at 11:08
  • Here is an article talking about this bug and there is a link to a simple exemple: http://meyerweb.com/eric/thoughts/2011/09/12/un-fixing-fixed-elements-with-css-transforms/ – Etienne Dupuis Aug 24 '16 at 19:28
  • 5
    Still an issue in 2017. Seems that they're still sticking to the "It's a feature not a bug!" argument... – max Dec 20 '17 at 16:02
  • Just want to add another live demo that demonstrates the bug https://poet.codes/e/PMVjDNz0VCx#styles.css – Krasimir Jul 17 '19 at 13:04
  • 10 yrs later ....... and not even a work-around. Maybe someone should suggest that they add a way to exclude children from a transformation – T S Feb 25 '22 at 16:15

21 Answers21

128

The CSS Transforms spec explains this behavior. Elements with transforms act as a containing block for fixed position descendants, so position:fixed under something with a transform no longer has fixed behavior.

They do work when applied to the same element; the element will be positioned as fixed, and then transformed.

smfr
  • 1,289
  • 1
  • 8
  • 3
  • 12
    That's the only helpful and correct answer. Stop translating the parent element and translate the childlements where the fixed element is part of it. Here's my fiddle:[JSFIDDLE](https://jsfiddle.net/daFalk/zremdore/1/) – Falk Jul 18 '16 at 10:30
  • 4
    and what if I want to transform a fixed element too? – Marc Sep 07 '16 at 08:26
95

After some research, there has been a bug report on the Chromium website about this issue, so far Webkit browsers can't render these two effects together at the same time.

I would suggest adding some Webkit only CSS into your stylesheet and making the transformed div an image and using it as the background.

@media screen and (-webkit-min-device-pixel-ratio:0) {
  /* Webkit-specific CSS here (Chrome and Safari) */

  #transformed_div {
    /* styles here, background image etc */
  }
}

So for now you'll have to do it the old fashioned way, until Webkit browsers catch up to FF.

EDIT: As of 10/24/2012 the bug has not been resolved.


This appears to not be a bug, but an aspect of the specification due to the two effects requiring separate coordinate systems and stacking orders. As explained in this answer.

Community
  • 1
  • 1
Kyle
  • 65,599
  • 28
  • 144
  • 152
12

Something (a bit hacky) that worked for me is to position:sticky instead:

.fixed {
     position: sticky;
}
yckart
  • 32,460
  • 9
  • 122
  • 129
  • 5
    http://updates.html5rocks.com/2012/08/Stick-your-landings-position-sticky-lands-in-WebKit ah yeah.. but not well supported yet it seems – coiso Mar 10 '14 at 17:54
  • 2
    sticky is different. The main problem is that with fixed we want to ignore the container's position (very useful. for opacity animations e.g.) I can't believe this bug is still there years later. Horrible. – FlorianB Jul 07 '16 at 00:09
  • 1
    This is the right solution in 2022. `position: sticky` is widely supported, and it fixes this behaviour. – Qasim Jun 20 '22 at 09:10
7

For anyone who finds their background images are disappearing in Chrome because of the same issue with background-attachment: fixed; - this was my solution:

// run js if Chrome is being used
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
    // set background-attachment back to the default of 'scroll'
    $('.imagebg').css('background-attachment', 'scroll');

    // move the background-position according to the div's y position
    $(window).scroll(function(){

        scrollTop = $(window).scrollTop();
        photoTop = $('.imagebg').offset().top;
        distance = (photoTop - scrollTop);
        $('.imagebg').css('background-position', 'center ' + (distance*-1) + 'px');

    });
}  
Jayden Lawson
  • 2,154
  • 2
  • 21
  • 24
7

August 2016 and fixed position & animation / transform is still a problem. The only solution that worked for me – was to create an animation for the child element that takes longer time.

Aurelio
  • 24,702
  • 9
  • 60
  • 63
defligra
  • 79
  • 1
  • 2
  • Please answer to new questions. Those questions need you more rather than the person who asked question in 2010. They must have solved the problem by now don't you think ? Also this question already has an accepted answer. – Umair Farooq Aug 16 '16 at 19:51
  • 9
    Nope! It's still a problem... the person who asked the question might have found another solution – but I found this thread for a reason. – defligra Aug 16 '16 at 19:55
  • As you wish. I left that comment while reviewing first questions by people. And since you have joined just today and it was your first answer and also a late answer so that is why I left that comment. I didn't downvote. That is a good chance for you. – Umair Farooq Aug 16 '16 at 20:07
  • 1
    @UmairFarooq maybe asking another question would be useless because it could be marked as duplicate. I came here just with a google search and i found this question useful , **defligra** answer too. – koMah Dec 13 '16 at 12:25
3

Actually I found another way to fix this "bug":

I have container element which hold page with css3 animations. When the page completed the animation, the css3 property has value: transform: translate(0,0);. So, I just removed this line, and everything worked as it should - position: fixed is displayed properly. When css class is applied to translate the page, translate property is added and css3 animation worked as well.

Example:

.page {
     top: 50px;
     position: absolute;
     transition: ease 0.6s all;
     /* -webkit-transform: translate(0, 0); */
     /* transform: translate(0,0); */
 }
 .page.hide {
     -webkit-transform: translate(100%, 0);
     transform: translate(-100%, 0);    
 }

Demo: http://jsfiddle.net/ZWcD9/

lowselfesteemsucks
  • 825
  • 1
  • 13
  • 21
  • 1
    For me, it was the fact of having these styles on the wrapper containing the fixed element that was preventing the fixed one from being sticky: -webkit-perspective: 1000; -webkit-transform-style: preserve-3d; Took these off and everything works fine. They were questionable optimizations anyway! – Alkanshel Mar 16 '15 at 01:23
  • Removing the transform, by any means, is probably the best workaround so far. Something like a fade-in, once complete, should be removable without affecting the appearance of the element. Actually, I'm not sure what having a transformX(0) hanging around would do to rendering performance, if anything at all; it could either be ignored, or could hurt performance, or could make it better by forcing some kind of 3D acceleration. Who knows. In any case, once an animation is complete, or even just before a fixed element is added to it, one can simply remove the CSS classes for the transform. – Triynko Aug 10 '15 at 03:07
2

I had this issue whilst trying to implement react-color with react-swipeable-views (rsw). The problem for me was that rsw applies translate(-100%, 0) to a tab panel which breaks the default fixed position div added over the full screen which when clicked closes the color picker model.

For me the solution was to apply the opposite transform to the fixed element (in this case translate(100%, 0) which fixed my issue. I'm not sure if this is useful in other cases but thought I would share anyway.

Here is an example showing what I've described above:

https://codepen.io/relativemc/pen/VwweEez

mrmadhat
  • 146
  • 12
1

on my phonegap project the webkit transform -webkit-transform: translateZ(0); worked like a charm. It was already working in chrome and safari just not the mobile browser. also there can be one more issue is WRAPPER DIVs are not completed in some cases. we apply clear class in case of floating DIVs.

<div class="Clear"></div> .Clear, .Clearfix{clear:both;}
abksharma
  • 576
  • 7
  • 26
1

Probably due to a bug in Chrome as I can't replicate in Safari nor Firefox, but this works in Chrome 40.0.2214.111 http://jsbin.com/hacame/1/edit?html,css,output

It's a very particular structure so it's by no means a universally applicable one-lined css fix, but perhaps someone can tinker with it to get it working in Safari and Firefox.

Kerry Johnson
  • 842
  • 9
  • 16
1

I just added transform: unset; to my fixed header and that worked for me!

I am also using framer motion with Next.js and having the same problem with my fixed header and this seems to fix it easily.

.header {
    position: fixed;
    transform: unset;
}
1

Setting fixed element to transform:unset; is working for me.

1

If you work with React, you can wrap the element with fixed position in a Portal, and this way it will work fine

Porkopek
  • 895
  • 9
  • 20
0

Adding the -webkit-transform to the fixed element solved the issue for me.

.fixed_element {
   -webkit-transform: translateZ(0);
   position: fixed;
   ....
} 
Ron
  • 22,128
  • 31
  • 108
  • 206
  • 22
    That didn't work for me. Are you able to create a demo of it working? – alex Mar 12 '13 at 03:01
  • i posted this solution because all the others didn't work for me. But im my case it was Safari-specific, even Chrome didn't need this fix. maybe try to experiment a bit with the `transform`...? – Ron Mar 12 '13 at 07:54
  • 4
    This fixed an issue for me in Chrome, FWIW. Thanks Ron. – Chris Apr 04 '13 at 23:38
  • FYI, on Android 2.3 this does not fix the issue. – Neil Monroe Sep 11 '13 at 18:49
  • 1
    @Neil Monroe, Android 2.3 is a whole new story. It does not support the fixed positioning at all :) – Wiseman Nov 22 '13 at 07:00
  • 8
    [Here is a Fiddle](http://jsfiddle.net/zequez/GqCKB/) where using `translateZ(0)` **DOES NOT** work. It's true that it works sometimes, I've seen occasions where it worked. But I still cannot narrow it down. – Zequez Mar 10 '14 at 18:46
0

Here is what works for me on all tested browsers and mobile devices (Chrome, IE, Firefox, Safari, iPad, iphone 5 and 6, Android).

img.ui-li-thumb {
    position: absolute;
    left: 1px;
    top: 50%;

    -ms-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
}
Murf
  • 487
  • 5
  • 9
  • 3
    Why the downvotes? It would be nice if you supplied a comment as to why you down voted my answer? – Murf Aug 15 '16 at 15:28
0

the fixed position of an element is broken if you apply transform to any ancestor.

<div style='position:fixed;-.*-transform:scale(2)'>...</div> //ok

<div style='-.*-transform:scale(2)'>
      <div style='position:fixed'>...</div> // broken
</div>
bortunac
  • 4,642
  • 1
  • 32
  • 21
0

If you can use javascript as an option this can be a workaround for positioning a position fixed element relavtive to the window when it's inside a transformed element:

  let fixedEl // some node that you is position 
              // fixed inside of an element that has a transform

  const rect = fixedEl.getBoundingClientRect()
  const distanceFromWindowTop = rect.top
  const distanceFromWindwoLeft = rect.left
  let top = fixedEl.offsetTop
  let left = fixedEl.offsetLeft

  if(distanceFromWindowTop !== relativeTop) {
    top = -distanceFromWindowTop
    fixedEl.style.top = `${top}px`
  }

  if(distanceFromWindowLeft !== relativeLeft) {
    left = -distanceFromWindowLeft
    fixedEl.style.left = `${left}px`
  }

Granted you will also have to adjust your heights and width because fixedEl will be calculating it's with based on it's container. That depends on your use case but this will allow you to predictably set the something position fixed, regardless of it's container.

Adrian Adkison
  • 3,537
  • 5
  • 33
  • 36
0

Add a dynamic class while the element transforms.$('#elementId').addClass('transformed'). Then go on to declare in css,

.translatX(@x) { 
     -webkit-transform: translateX(@X); 
             transform: translateX(@x);
      //All other subsidaries as -moz-transform, -o-transform and -ms-transform 
}

then

#elementId { 
      -webkit-transform: none; 
              transform: none;
}

then

.transformed {
    #elementId { 
        .translateX(@neededValue);
    }
}

Now position: fixed when provided with a top and z-index property values on a child element just work fine and stay fixed until the parent element transforms. When the transformation is reverted the child element pops as fixed again. This should easen the situation if you are actually using a navigation sidebar that toggles open and closes upon a click, and you have a tab-set which should stay sticky as you scroll down the page.

mr.G
  • 19
  • 3
0

in my case I found out we can't use transform: translateX() before transform:translateY().if we want to use both we should use transform:translate( , ).

0

If you're animating back to the original position where all translates are 0, you can use this solution where you set transform: unset;:

  100% {
    opacity: 1;
    visibility: visible;
    /* Use unset to allow fixed elements instead of translate3d(0, 0, 0) */
    transform: unset;
  }
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
0

If you use React then you can use ReactDOM.createPortal.

Credit goes for Porkopek for the tip, but it took me some time to figure out what this thing is. So here's what I found:

If you return a component wrapped in ReactDOM.createPortal then as the second argument you can set the parent element, and React will append the component to that given element, so example position: fixed will be fine even as a child of a transformed component.

import ReactDOM from "react-dom";


const MyModal = () => {
    return ReactDOM.createPortal(
       <div style={{position: "fixed", top: "0", left: "0", 
                    background: "white", color: "black", 
                    padding: "3rem", zIndex: "50000"}}>
         Modal Element
       </div>,
       document.body
    )

}

In this case React will render this component as the child of the body, not as the child of the parent component.

Eqzt111
  • 487
  • 6
  • 17
-1

Please don't up vote, because this is not exact answer, but could help someone because it's fast way just to turn off the transformation. If you really don't need the transformation on the parent and you want your fixed position working again:

#element_with_transform {
  -webkit-transform: none;
  transform: none;
}
makkasi
  • 6,328
  • 4
  • 45
  • 60
  • 2
    This is, like, really in the question's title – Eugene Pankov Mar 14 '18 at 10:30
  • @EugenePankov Don't see 'none' in the title. It was what it fixed my problem and in general no one suggests to turn it off. Although this is not exact answer to that question, but it could help someone that doesn't want to use transformation and the transformation comes from an other library. So I don't want up votes, but please don't down vote. I will edit my question to say that I don't want Up vote. – makkasi Mar 14 '18 at 12:21