Single/sub pixel misalignment of divs on ipad and iphone safari

后端 未结 8 687
粉色の甜心
粉色の甜心 2020-12-23 12:20

I\'ve got the following situation, and I need some help...

  1. Two divs, same size, same location, one on top of the other
  2. Everything works as expected on
相关标签:
8条回答
  • 2020-12-23 12:25

    I had a similar problem in a recent project where I had background image masks with different background color to colorize the resulting icons in mobile Safari. The problem was that when the page was scaled down by Safari, there was a line of the background color showing around the image, even though it should have been masked. I never found a way to prevent that leaking of the background color when the page is scaled down. It's clearly an error in mobile Safari's algorithms that recalculate the background and mask. I did find a workaround: I put an outline on the element with the same color as the background of the element's parent. The outline is outside the element and therefore masks the part bleeding out. If your element's parent has a pattern background that's drastic, this won't work that well, but if it's a solid color, it'll do just fine.

    0 讨论(0)
  • 2020-12-23 12:27

    A negative margin is the only way I found to prevent this.

    For example, if you have a faint horizontal gap between 2 divs, adding a top margin of -1px to the second div will make it overlap slightly and the gap will not reappear as the page is scaled.

    Some situations (like image sprites or repeat patterns) may need a little more tweaking, but the general idea is the same. For a sprite, make sure there is no big color change within 1 pixel of the cropping border. The bleed is never more than 1 pixel, so a 1 pixel adjustment is enough.

    0 讨论(0)
  • 2020-12-23 12:27

    It totally depends on one's situation, but in our case we use negative margins as proposed by this thread or a box shadow since outline only applies to all borders and ie. outline-bottom does not exist.

    /*
     * Prevent faint lines between elements.
     * @link http://stackoverflow.com/questions/5832869
     */
    box-shadow: 0 1px 0 red;
    
    0 讨论(0)
  • 2020-12-23 12:28

    The problem is not only with divs matching together, but also with image sprites.

    I followed the advise in this thread of setting initial viewport scale to 1.0. The sub-pixel bug was gone, but then I tested my website on other devices, like Android, and realized a full size page is annoying, since users have to re-scale every time it's loaded. So I preferred to disable the scale and wait until Apple fixes it. Then one day I was thinking how to solve the problem with the margins of the page, and I came to this simple solution in CSS:

    html {
      min-width: 1024px;
    }
    

    Devices capable of this resolution, such as iPad in horizontal position, will set the document scale to 1.0. In my case this is enough solution, since I can show the page is working just right, and the sub-pixel bug is in Safari/iOS, which will be solved in the future hopefully.

    0 讨论(0)
  • 2020-12-23 12:28

    I also solved the iOS sub pixel gap issue (on a full screen site) by using overflow-x: hidden; to stop side ways scrolling & viewpoint scale to stop pitch zooming. I also had holder divs set at width: 101%; and all the elements/image divs inside set to float: left;. This means the sub pixel gaps are all on the left hand site but pushed out of view by the holder div set at 101% width.

    0 讨论(0)
  • 2020-12-23 12:29

    Remove "clear:both" (if there is) from div below the gap.

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