Android Chrome Centering Unit

I'm having a weird problem displaying my webpage on mobile Chrome. I have a horizontal block and it just displays on desktop Chrome, but on mobile Chrome it attaches to the left border of the page.

After some research, I realized that there is a mobile “viewport” that is different from the screen size. Thus, the task is not just to focus.

The simplified markup that I use:

<body>
    <div class="block">
    </div>
</body>

And CSS:

body {
    width: 100%;
    margin: 0;
}
.block {
    position: relative;
    width: 1024px;
    height: 768px;
    background-color: red;
    top: 44px;
    margin: auto;
}

I also tried to center the block using:

left: 50%;
margin-left: -512px;
position: absolute;

But it has the same effect. I also tried adding metadata to the viewport:

<meta name="viewport" content="width=device-width, initial-scale=1">

. 1920x1080, ~ 1200 , " " 640 , , - .

- (1200 ) , . " " ... ( , , ).

, : http://37.48.93.204/androidtest.htm

Chrome, Android Chrome.

+4
9

, :

- - , margin-left: auto; margin-right: auto;. .

max-width play , .

( ) 1024px. , , 1024px.

DEMO

HTML

<html>
  <head>
    <meta name="viewport" content="width=1224">
  </head>
  <body>
    <div class="mainblock">
      <div class="topside">
        Top
      </div>
      <div class="content-wrapper">
        <div class="content">
          Content
        </div>
      </div>
      <div class="bottomside">
        Bottom
      </div>
    </div>
  </body>
</html>

CSS

body {
  width: 100%;
  margin: 0;
}

.mainblock {
  max-width: 1024px;
  width: 100%;
  height: 768px;
  margin: 0 auto;
}

.content-wrapper {
  background: url('http://lorempixel.com/1224/768/') no-repeat 50% 0;
  display: block;
  position: relative;
  max-width: 1024px;
  padding: 0;
}

.content {
  background: salmon;
  box-sizing: border-box;
  min-height: 768px;
  padding: 0;
  max-width: 1024px;
  width: 100%;
}

.topside,
.bottomside {
  max-width: 1024px;
  height: 50px;
  background-color: blue;
  margin: 0 auto;
}

@media only screen and (min-width: 1024px) {
  .mainblock, .content-wrapper {
    max-width: 1224px;
  }

  .content {
    margin: 0 auto;
    width: 1024px;
    position: relative;
    z-index: 2;
  }

}

Edit:

head HTML-:

<meta name="viewport" content="width=1224">

2:

(http://codepen.io/anon/pen/dPzJPX) .

+1

, CSS. . :

/* normal desktop */
.block {
    width: 1024px;
}

@media only screen and (max-width: 1023px) /* notebook */
{
    .block {
        width: 800px;
    }
}
@media only screen and (max-width: 793px) /* tablet */
{
    .block {
        width: 600px;
    }
}
@media only screen and (max-width: 479px) /* old smartphone */
{
    .block {
        width: 400px;
    }
}

, , , . , Table based layout Flexible Boxes. HTML.

+1

, Chrome Chrome. , :

<script type="text/javascript">

        $(document).ready(function() {

            var userAgent = navigator.userAgent;
            var cssIosURL = '<link href="${contextPath}/css/ios.css" rel="stylesheet">';
            var cssNativeAndroidURL = '<link href="${contextPath}/css/nativeAndroid.css" rel="stylesheet">';
            var cssAndroidChromeURL = '<link href="${contextPath}/css/androidChrome.css" rel="stylesheet">';

            if (userAgent.indexOf('Mac OS') >= 0) {
                if (userAgent.indexOf('Mobile') >= 0) {
                    $('head').append(cssIosURL);
                }
            }

            if (userAgent.indexOf('Android') >= 0) {
                if ((userAgent.indexOf('Firefox') < 0) && (userAgent.indexOf('Chrome') < 0) && (userAgent.indexOf('Opera') < 0)) {
                    $('head').append(cssNativeAndroidURL);
                } else {
                    $('head').append(cssAndroidChromeURL);
                }
            }

        });

    </script> 
0

CSS , :

.block {
position: absolute;
width: 200px;
height: 200px;
background-color: red;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
}

, margin-left width.

, .

: http://jsfiddle.net/6x3a4uaL/

Chrome Android-, jsFiddle.

0

"block", :

.block {
    display: block;
    margin: 0 auto;
    width: 30em;
    height: 60em;
    background-color: red;
}
0

, <center></center>. , , . , margin: auto margin: 0px auto.

0

CSS

body {
    width: 100%;
    margin: 0;
}
.block {
    display: flex;
    position: relative;
    width: 1024px;
    height: 768px;
    background-color: red;
    top: 44px;
    margin: auto;
}

HTML

 <div class="block">
    </div>

flex CSS

0

100% . , .

- http://jsfiddle.net/c5afz9b1/

  • width: 100%;
  • , , . , .
  • overflow: hidden .

, .

0

http://jsfiddle.net/t178r76n/2/

JSFiddle - , , . , , , .

- ...

.mainblock {
    width: 1024px;
    height: 768px;
    position: relative;
    max-width: 100%;
    max-height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}

, ... , .

0

All Articles