163

I am using -webkit-transform (and -moz-transform / -o-transform) to rotate a div. Also have position fixed added so the div scrols 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?

iSenne
  • 2,476
  • 5
  • 23
  • 25
  • 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
  • 7
    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
  • 3
    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

17 Answers17

108

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,089
  • 1
  • 7
  • 3
  • 9
    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
  • 2
    and what if I want to transform a fixed element too? – Marc Sep 07 '16 at 08:26
93

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
  • 60,843
  • 27
  • 138
  • 150
  • @Kyle Sevenoaks Tnx for your awnser. Helps a lot :). – iSenne Apr 15 '10 at 08:56
  • Just so u know. I decided to remove the button in webkit based browsers :). Customer doesn't use it anyway. Until webkit based browser catch up with ff, I don't support it. Not the best solution, but for now the quickest... – iSenne Apr 15 '10 at 12:47
  • Well that makes sense if the customers don't even use it, why bother putting it in? Hope that Webkit catches up, also that the other browsers catch up to the webkit transitions! – Kyle Apr 16 '10 at 09:49
  • 1
    That would be really nice. There are a lot of CSS3 functions I want to use :). So you know, I made (my first) a jQuery plugin for it. http://bit.ly/cVWxvR. Really simple, but usefull! – iSenne Apr 16 '10 at 11:56
  • I have used CSS3 functions for the special users of webkit browsers, but others just have to wait. Making jQuery plugins is a great way to go about it, keep up the good work! – Kyle Apr 16 '10 at 12:07
  • The problem is what happens if it's a navigation and not just a ribbon. You need clickable elements, I think I'm going for a JS based solution. – Tom Roggero Feb 14 '12 at 15:49
  • Thanks, I had the same problem with repositioning a fixed element with `top: 0`. It simply did not show. My fix is by removing the attribute when set to 0 (which was my use case). Simply add your object with jQuery's `css` function and along the lines of `transform: ''` and `'-webkit-transform': ''`. – ReSpawN Aug 08 '13 at 13:12
  • 39
    Even more years later, still not resolved. Pretty sad. – Amalgovinus Mar 13 '15 at 22:46
  • 12
    According to [this answer](http://stackoverflow.com/a/15256339/215384) it's not a bug but part of the spec. – MichaelRushton Mar 17 '15 at 15:49
  • 1
    find firfox has the same problem. – soytian May 19 '15 at 09:46
  • 1
    April 1, 2016 – still busted :[ – Drew Apr 02 '16 at 01:10
  • 25 May, 2016 Still not working, ended up just removing the transform – Matt McCabe May 25 '16 at 08:53
  • I've been looking at a solution for this since a bug came up on a website I was working on. The previous developer somehow managed to achieve this but he had some mistakes on the html leaving some open tags. Once I fixed the missing closing tags I crashed into this exact problem and I am scouring the internet looking for an almost impossible solution... until I've found this fiddle: http://jsfiddle.net/vishl/wzpsf/ where as you can see the fixed element is working correctly. Someone knows what's happening here and can shed some light on it? – MacK May 26 '16 at 11:04
  • @MacK The fixed element isn't inside the transformed element. – Falk Jul 18 '16 at 09:47
  • How is this still not fixed ? It's been 6 years. – justnorris Aug 03 '16 at 10:44
  • Added link to explanation, thanks to @MichaelRushton for pointing it out. – Kyle Aug 03 '16 at 10:59
  • 18
    sit back and watch -- I bet it will live to its 10 year anniversary – lzl124631x Oct 29 '16 at 11:25
  • 1
    May 2017. Still an issue. – Pat M May 10 '17 at 15:03
  • *sigh* May 17, 2017 – aks. May 17 '17 at 14:57
  • 3
    End of July, 2017 -- they still don't know I'm a soviet spy, chrome `translate3d` with `fixed` still not working. Signing off. – John Weisz Jul 29 '17 at 09:20
  • 31
    Aug 30, 2017, captain's log. We also have encountered the strange anomaly, which was described so long ago by other captains. A solution is still to be implemented. – Luoruize Aug 30 '17 at 11:30
  • 3
    Feb 2018: we seem to have encountered some kind of ancient technology, no one can explain – Sascha Feb 19 '18 at 15:40
  • March, 2018: Still my firefox dev edition is punching me down with this issue... :( – Merc Mar 28 '18 at 03:57
  • 22
    This is the bug that my father's father warned me about. – djmcr Oct 10 '18 at 15:22
  • 6
    January 2019: This bug is still going strong – Jan-Willem de Boer Jan 16 '19 at 08:05
  • 5
    The 3rd month of the 19th year of the 2nd Millennium, still not resolved – Owen M Mar 03 '19 at 22:44
  • 1
    Summer 2019 checking in. – Johan Lindskogen Jul 10 '19 at 13:44
  • 3
    September 2019 ! – Allan Jebaraj Sep 04 '19 at 05:18
  • 3
    October (1 day short of november) 2019 checking in. I thought I was going crazy. The fact that it hasn't been fixed 9.5 years later is mind boggling. – finitenessofinfinity Oct 30 '19 at 12:36
  • 1
    9 years, 9 month. We are getting close! – Wojciechu Jan 30 '20 at 22:20
  • 3
    This bug is giving me more trouble than the coronavirus pandemic 26 March 2020! – Juan Artau Mar 26 '20 at 17:24
  • 5
    We made it to 10 years guys! – André Silva Jul 13 '20 at 16:35
  • 5
    When you come to SO looking for answers, this is exactly the kind of comment thread you don't want to see. – Bangkokian Aug 16 '20 at 17:34
  • 1
    2021 and the bug still hasn't been resolved – Kamal Alhomsi Feb 24 '21 at 18:49
8

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

.fixed {
     position: sticky;
}
yckart
  • 28,174
  • 7
  • 112
  • 121
  • 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
  • 1
    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
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,025
  • 2
  • 19
  • 23
6

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
  • 20,064
  • 8
  • 54
  • 61
defligra
  • 69
  • 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
  • 6
    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
  • 747
  • 1
  • 10
  • 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! – Amalgovinus 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
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
  • 515
  • 6
  • 23
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
  • 515
  • 6
  • 11
1

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
  • 112
  • 9
0

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

.fixed_element {
   -webkit-transform: translateZ(0);
   position: fixed;
   ....
} 
Ron
  • 18,164
  • 27
  • 98
  • 185
  • 20
    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
  • 3
    Thank you, this fixed me an issue. Saved my life! – styke Oct 19 '13 at 21:18
  • 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
  • 467
  • 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
  • 3,930
  • 1
  • 26
  • 19
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
  • 2,813
  • 3
  • 28
  • 35
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
  • 9
  • 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
  • 22,596
  • 18
  • 73
  • 147
-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
  • 2,353
  • 2
  • 35
  • 47
  • 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