Monday, March 19, 2018

Why does a CSS border look different on Android?

Leave a Comment

I have a box with a border.

border: 1px solid #000; 

I am using the following viewport setup:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> 

The border seems to be 2 pixels on the top and right side. What is the reason for this?

http://i.imgur.com/7yHjy.png


Additional: there are no other CSS rules other than a width and height.

2 Answers

Answers 1

The meta tag that targeted pixel-density specifically has been depreciated and now both Android and iPhone seem to be just using the one metatag:

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

But, if you try to make a 1px border, it will be thicker and thinner on different sides depending on the mobile device's pixel density.

How some devices render '1px' with multiple pixels and it is not always pretty because they are using different pixel ratios (dpr) such as 1.5, 2 and 3. Sometimes, all 4 sides of a 1px border will not match.

This is some CSS to make 1px display properly on 2dpr iPhone by dividing 1px by half:

@media only screen and (-webkit-min-device-pixel-ratio: 2) {  div {  border-width: 0.5px;  } 

And similar techniques are shown here: http://n12v.com/css-retina-and-physical-pixels/ https://developer.android.com/guide/webapps/targeting.html

Answers 2

Unless you have a very good reason for it (doubtful), disabling user-zoom is a very bad idea. See user-scalable=no … Evil or Slightly Not Evil? for examples of why this is bad. It also gives some examples where user-scalable=no is perfectly acceptable.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment