37

EDIT 2016-07-04(Since this question is getting popular): This is a bug in Chrome. Developers are actively working on a fix.

EDIT 2017-05-14 The bug seems to be fixed, the fix will be introduced in Chrome 60

EDIT 2018-05-04 A fix has been merged, but the bug still appears to be present.

So I have this very ugly-looking window that is centered on the screen by this CSS:

.popup
{
   position: fixed;
   top: 0;
   bottom: 0;

   transform: translate(-50%, -50%);
}

However, it looks like this on Chrome (the font looks really blurry):

Chrome

But like this on Firefox:

enter image description here

When I remove the transform rule, the text looks nice and crispy again, but then it's no longer correctly centered.

When I go to chrome://flags and execute #disable-direct-write it looks nicer, but users are obviously not going to do that and it doesn't solve the problem.

How can I make my font look nice while still having the window centered?

My chrome version is 44.0.2403.155

I have a three.js demo using WebGL that renders on a background canvas. When I disable the demo, the problem no longer occurs.

JSFiddle with the background.

JSFiddle without the background.

joppiesaus
  • 5,471
  • 3
  • 26
  • 36

10 Answers10

16

A suggestion from a related discussion solved the issue for me: https://stackoverflow.com/a/46117022/7375996

Using calc with some offset solved the issue in my case:

transform: translate(calc(-50% + 0.5px), calc(-50% + 0.5px));
Dmitry.Kolosov
  • 635
  • 4
  • 8
9

I've tried different solutions in different, sometimes scary, combinations:

  • translate3d instead of translateY
  • zoom:2; transform: scale(0.5); or zoom:0.5; transform: scale(2);
  • transform: translate(calc(-50% + 0.5px), calc(-50% + 0.5px));
  • -webkit-filter: blur(0);
  • perspective: 1000px
  • scale(1.0, 1.0)
  • -webkit-font-smoothing: subpixel-antialiased;

none of them work in july 2019.

The only solution i found for modals, that should be centered - to use a flexbox aligment instead of transform: translate.

.modal__container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
}

  .modal {
    box-sizing: border-box;
    max-width: 80%;
    max-height: 80%;
    padding: 20px;
    overflow-y: auto;
    background: #fff;
  }
<div class="modal__container">
  <div class="modal">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</div>
4

Problem in half of pixel.

Try: transform: translate(-50%, -51%);

It will work!

egor.xyz
  • 2,847
  • 1
  • 21
  • 18
  • 14
    No, it does not work. it might look nicer at a specific situation and specific resolution, but this is not a solution to the problem. – joppiesaus Aug 10 '16 at 16:15
  • 1
    Give me example when it's not working. This problem exist only in Chrome. I saw many solutions like `translateZ(0)` or `perspective:1000px` or `z-index:1`. Idea to move your layout to another layer. But problem in many cases when you are using `scale(1.1)` or `translateX(-50%)` not enough: 0.5px and browser making text like blurry. In many cases you can change height of current tag (Add 1px). – egor.xyz Aug 11 '16 at 20:33
3

I found out -webkit-filter: blur(0) could fix your blurry font in Chrome on Windows:

JSFiddle

#projectPopup {
    ...
    -webkit-filter: blur(0);
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
  • @joppiesaus oh, it's strange(. It's OK in my Chrome 44 on Windows 7/8.1/10, OS X, Ubuntu 14.04, something like [this](http://oi60.tinypic.com/jpy8sx.jpg). Which OS do you use? Is [this snippet](http://jsfiddle.net/sergdenisov/8vfz9m29/2/) crashed too? – sergdenisov Aug 19 '15 at 16:28
  • It's probably because I'm using Chrome 45 now. I'll test it on Linux with the latest stable version(44), I'm sure that will be fine. – joppiesaus Aug 20 '15 at 10:07
1

Use a normalization of the transform after your animation:

Transform X/Y normalization

Or We scale the texture with zoom double, then scale down again. In some cases this cannot be applied due to other complex transformations or translations, and is not perfect.

...{
zoom:2;
-webkit-transform: scale(0.5);
transform: scale(0.5);
}
Community
  • 1
  • 1
Miguel
  • 3,349
  • 2
  • 32
  • 28
1

The height of the elements must be even numbers https://prnt.sc/mtxxa2

1

2019-04-15, still happening to me in Chrome. Found that changing position: fixed to absolute fixed it:

.popup
{
   position: absolute;  <-- just like that
   top: 50%;
   left: 50%;

   transform: translate(-50%, -50%);
}

Using absolute may or may not suit your particular case, just my 2 cents.

David Suarez
  • 1,090
  • 11
  • 12
1

The only solution that worked for me:

Translates can cause blur due to result not being rounded to the nearest pixels, so rounding the div height to an even number fixes it.

We can't do it in CSS as it doesn't know yet its final height, so we have to do it after rendering. Using Jquery is a breeze:

$(window).on("load", function() {
    if($('.popup').height()%2==1) $('.popup').height( 2*Math.round($('.popup').height()/2 ) ) ;
    if($('.popup').width()%2==1) $('.popup').width( 2*Math.round($('.popup').width()/2 ) ) ;
});

Credits: https://stackoverflow.com/a/57382347/1136132

joseantgv
  • 1,943
  • 1
  • 26
  • 34
0

First of all add overflow: hiddden to the parent element of the modal, if that's not enough add this to modal's style : display: contents Happy coding, love from Mauritania

UPanda
  • 99
  • 1
  • 8
-1

I tried every solution and only this is working for me (chrome 53)

dialog {
  position: fixed;
  top: 50%;
  transform: translate(0, -50%);
}
manuel-84
  • 2,582
  • 1
  • 23
  • 23