Why on Safari the transform translate doesn't work correctly?

前端 未结 3 2061
甜味超标
甜味超标 2020-12-03 07:04

I often use this code to center a div in view:

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transfo         


        
相关标签:
3条回答
  • 2020-12-03 07:41

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

    The key for safari (including ios devices) is to add the other transform css rules and not just:

    transform: translateY(-50%);
    

    You need to add to it this group of rules:

    -ms-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    

    Here is some working code of mine:

    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%);
    }
    
    0 讨论(0)
  • 2020-12-03 07:44

    In some cases you'll have to use:

    -webkit-transform: translate3d(-50%,-50%,0);
    

    Sometimes works better with mobile browser.

    0 讨论(0)
  • 2020-12-03 07:53

    You need another vendor prefixed style.

    .centered {
      position: fixed;
      top: 50%;
      left: 50%;
      /* bring your own prefixes */
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
    

    Please refer below to know which browser supports what and what prefix has to be added. http://caniuse.com/#feat=transforms2d

    0 讨论(0)
提交回复
热议问题