20

In my CSS I defined a transition for a class. For some reason, when I hover over the class with the transition, the transition-duration for some reason alters the font color elsewhere (form placeholders and certain links). (This happens only in Safari as far as I can tell.)

Here's a jsFiddle that shows what I'm talking about:

http://jsfiddle.net/EJUhd/

Does anyone know why this occurs and how I can prevent it?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
tvalent2
  • 4,959
  • 10
  • 45
  • 87

7 Answers7

35

I was struggling with a similar issue. For me, random links throughout the page became apparently bold (clearly something to do with OSX and anti-aliasing in Safari, as Chrome (in windows 7 and OSX) as well as the same version of Safari in Windows worked fine.

The solution is not obvious, and depending on what you are doing might not be optimal, but adding this line of code fixed the issue:

-webkit-transform: translateZ(0);

This basically triggers the GPU to do animation, and the text no longer had artifacts in my site. Do note that it's not always appropriate to use it, as it may use more battery life and use more resources. Sometimes however, it uses less, so basically check the performance when you add it.

You add this to the normal state not the :hover animated state.

img { -webkit-transform: translateZ(0); }

As opposed to on the:

img:hover { /* not here */ }

The other very positive side effect is that depending on the animation you are doing, it might be smoother through the GPU. So you won't get the choppy animation you mention in your follow up post. In my case, the animation was more seamless in safari. I was doing a 120% scale and 5 degree rotation of an image with some box-shadow appearing at the same time. In my situation, it did not reduce CPU usage unfortunately.

Mauricio
  • 571
  • 4
  • 11
  • Great fix! I'm doing a -3deg rotation so I hope it's not a huge drain on resources. – tvalent2 Mar 19 '12 at 00:42
  • Be aware changes in Safari 6 (on iOS) may invalidate this method of forcing GPU acceleration. – Steven Lu Jan 14 '13 at 19:52
  • I added `-webkit-transform: translateZ(0);` to the `container` class in the original fiddle, but it still changes the font of the placeholder text in Safari Version 7.0.3 (9537.75.14) on OSX 10.9.2. Here's my modified fiddle http://jsfiddle.net/sYe6v/. Any idea on what I'm doing wrong? – sguha Apr 11 '14 at 19:10
3

There is no more relevant topic I've found for a problem I had, but that's related to mentioned above issue. So, might be helpful for some one.

In two words: I have some container (popup), some element inside. Appearing goes the following way: container background is fading up to dark via opacity and element inside is scaling up (like coming closer to us from behind). Everything works great everywhere but not in Safari (Mac/Win/iPhone). Safari "initially" shows my container, but it blinks some strange way (tiny short flash appears).

Only adding -webkit-transform: translateZ(0); (to container!!!) did help.

.container {
    -webkit-transform: translateZ(0); /* <-- this */
}

.container section {
    -webkit-transform: translateZ(0) scale(.92); /* <-- and I added translate here as well */
    -webkit-transition: -webkit-transform .4s, opacity .3s;
    opacity:0;
}

.container.active section {
    -webkit-transform:translateZ(0) scale(1);
    -webkit-transition: -webkit-transform .3s, opacity .3s;
    opacity:1;
}

But speaking of the transitions, there was also the following part of code:

.container {
    ...
    top:-5000px;
    left:-5000px;
    -webkit-transition: opacity .5s, top 0s .5s, left 0s 5s, width 0s 5s, height 0s 5s;
}
.container.active {
    -webkit-transition: opacity .5s;
    top:0;
    left:0;
    width:100%;
    height:100%;
}

considering, that I want to show/hide the popup using only css switching (and also to make it disappear nicely instead just "display:none").

so, somehow on appearing Safari (obviously) was inheriting transition properties besides "opacity" even as I've overloaded them with only -webkit-transition: opacity .5s;

so, adding the following solved the problem:

.container {
    ...
    -webkit-transition: opacity .5s, top 0s 0s, left 0s 0s, width 0s 0s, height 0s 0s;
}
Agat
  • 4,577
  • 2
  • 34
  • 62
2

I can't begin to tell you why it's doing this, but Safari isn't changing your text color, it's anti-aliasing the text differently while the transition is in motion. The text edges get smoother, and the text itself becomes thinner. This is extra obvious if you zoom in on the fiddle with accessibility tools. At some smaller sizes, the shading around the button next to the form text shifts too. (Is it possible that Safari is redrawing some things, or reorienting them on a sub-pixel level during the transitions ? Somebody explain this please, it's driving me nuts now!)

Because I have no real idea why it's doing this either, these might not be the best solutions:

Depending on what you're transforming, replacing the css transform with a javascript animation will probably fix it.
For example in your fiddle, the problem also occurred with a scale transformation, but not with a similar jQuery animate function.

There seem to be some shades and styles where the anti-aliasing change is less obvious (at least in the fiddle), so you could also try styling the placeholders and other effected text differently.
(This thread may help with styling the placeholders, if you go that route: Change an HTML5 input's placeholder color with CSS )

Community
  • 1
  • 1
Brid
  • 181
  • 3
2

Thanks to the identification of anti-aliasing above, as well as help from the articles below, I modified my code to include translate3d(0,0,0) and the problem disappeared:

    -webkit-transition-duration: .17s, .17s translate3d(0,0,0);

The transition isn't as smooth as it once was but that's a subject for another question.

Wonky text anti-aliasing when rotating with webkit-transform in Chrome

http://johanbrook.com/design/css/a-fix-for-antialiasing-issues-in-webkit-browsers/

http://www.webkit.org/blog/386/3d-transforms/

Community
  • 1
  • 1
tvalent2
  • 4,959
  • 10
  • 45
  • 87
0

i had the same problem, while a transition some text became antialiased. this happen only in anchor text that are positioned relative e with z-index inside an element positioned and with z-index itself. if i remove all position and index the problem disappear.

ganchan
  • 317
  • 4
  • 4
0

There is a similar problem using transition and translate3d. Sometimes any element on the page with :hover styles shows its hover behavior. I have this problem using a slider. Put the -webkit-transform: translateZ(0); to the :hover element and its behavior is normal.

geo
  • 1,781
  • 1
  • 18
  • 30
0

For rotation() maybe it's fine, but for scale() It didn't worked the -webkit-transform: translateZ(0); formula.

I used :

-webkit-font-smoothing: antialiased;
Despertaweb
  • 1,672
  • 21
  • 29