html - Centered div with position:absolute has offset for Android devices when content is larger than viewport -
issue shows on android browsers when following on page:
- a
div
element size larger device's viewport. (i used 1200px.) - one or more other
div
elements eitherleft:0; right:0; margin:auto;
orleft:50%; margin-left:-100px
style centering.
the issue "centered" div
elements aren't. have offset left (or top if centering vertically). problem shows on android devices in both chrome , dolphin (webkit). not show on desktops (tested chrome, firefox, safari, , ie).
here example css:
body{ margin:0; padding:0; } .wide-element { position:absolute; height:800px; width:1200px; left:50%; margin-left:-600px; background:url(1200px-wide.png); } .fixed-1, .fixed-2, .absolute-1, .absolute-2 { height:200px; width:200px; } .fixed-1 { position:fixed; margin:auto; left:0; right:0; top:0; bottom:0; background:rgba(0, 0, 255, .5); } .fixed-2 { position:fixed; margin:-105px 0 0 -105px; left:50%; top:50%; border:5px solid blue; } .absolute-1 { position:absolute; margin:auto; left:0; right:0; top:0; bottom:0; background:rgba(255, 0, 0, .5); } .absolute-2{ position:absolute; margin:-105px 0 0 -105px; left:50%; top:50%; border:5px solid red; }
and html:
<body> <div class="wide-element"></div> <div class="fixed-1"></div> <div class="fixed-2"></div> <div class="absolute-1"></div> <div class="absolute-2"></div> </body>
i added position:fixed
pair contrast position:absolute
pair. can see in following screenshot, fixed
divs both @ actual center of screen, while absolute
divs , left of center of layout. problematic part this offset causes elements on left side of screen cut off, , unreachable.
i'd know why (exactly) happening (and why on mobile devices), real question is:
how can reliably center div
element might larger viewport, without android devices making parts of page unreachable?
this provisional answer own question.
adding <meta name="viewport" content="width=1200">
line head
section seems force browser set viewport specified size. however, since isn't true fix, , others may need more flexible solution, i'm leaving open more complete answer.
Comments
Post a Comment