Access to hardware pixels on mobile devices - html

Information to clarify the terminology from here.
Hardware pixel: A physical pixel on the display. For example, an iPhone 5 has a screen with 640 horizontal hardware pixels.
Device-independent pixel (dip): A scaling of device pixels to match a uniform reference pixel at a normal viewing distance, which should
be approximately the same size on all devices. An iPhone 5 is 320 dips
wide.
CSS pixel: The unit used for page layout controlled by the viewport. Pixel dimensions in styles such as width: 100px are specified in CSS
pixels. The ratio of CSS pixels to device independent pixels is the
page's scale factor, or zoom.
Is there are any way to set relationship between Hardware pixel and CSS pixel like 1:1.
I mean if i want set my div's width to 100px, it will be exactly 100 hardware pixels even on Retina displays.

Screen scale factor (Retina, presently either 2x or 3x) is distinct from page scale factor. At the same page scale factor, 1px would be 1x1, 2x2, or 3x3 hardware pixels, depending on the (Retina) display.
It sounds like what you're looking to do is to drive the screen at a "native" resolution which would make it appear that it has 2x or 3x as many pixels, but at a (non-Retina) screen scale of 1.
To accomplish that, you'd have to transform the view by multiplying its size by the screen scale factor, while scaling it by the inverse of its screen factor.
You can set the head's <meta name="viewport" content="width=device-width-times-2, initial-scale=0.5"> but your content will be far less readable and sharp.
If you're looking to do this on an element basis, you can set
-webkit-transform-style: preserve-3d;
-webkit-transform: scale3d(0.5,0.5,0.5);
3d is necessary, as a 2d transformation may lead to issues with the touch area not matching up with the element's location in the view.

I think you can use device-width with min-device-width and max-device-width properties to access hardware pixels. device-width - describes the width of the output device. For example:
#media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { ... }
Here you can find a map of standard devices and media queries for them.
Also, you can setup media queries for different devices DPI you can use min-resolution property in media on this way:
#media print and (min-resolution: 300dpi) { ... }
To replace the old min-device-pixel-ratio syntax use this:
#media screen and (min-resolution: 2dppx) { ... }
Here is a source.
Basically, in clear CSS you can't get access to hardware pixels. You can just optimize pages to different resolutions.

You can use:
#media screen and (max-width: 300px) { /* To Do */ }
link visit: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

If you want the exact relationship I don't think you can do it in CSS.
But you can do it in JS :
To get the width of a 100px wide div :
var myDivWidth = 100 / window.devicePixelRatio;
So on an iPhone 5 with a pixelRatio of 2.0 you will have a 50px wide div spread over 100 physical pixels.
You could to the same with css transform:scale(0.5) if you want to shrink div content too.

Related

Mobile website changing to desktop when rotating to landscape mode

I am using #media screen and (max-width:768px) to change the view of my website on mobile but when I rotate to landscape mode it changes to desktop website meaning it shows content which was hidden using
#media screen and (max-width:768px). How can I avoid this?
What #media screen and (max-width:768px){ ... } does is it will only apply everything in that block if the device the user has has a screen of a width of 768px or lower (taking into consideration double pixel density etc, but that a whole other can of worms)
So when you turn your phone sideways it's height becomes it's width and since that is more than 768px, it does not apply your "mobile layout".
One option would be to increase 768px to a bigger number, or even split some of the rules for smaller and higher widths, like for example having rules for max-width: 640px and for max-width: 960px

how to change "height" element value in XS (extra small) bootstrap?

how can i change the value of my "height" element when the device or width/height changes? my default height size is 180px (that's a rectangle in LG(large screen) but the height doesn't change when i try visit in mobile device (XS) mode.
i want to increase the value of "height" when someone visits website from XS or mobile devices.
i"m using bootstrap 3.3.7 in my project.
Defining Proper Media Queries
Bootstrap has clearly defined breakpoints for different kinds of
devices, specified by using CSS media queries. The following are the
breakpoint categories used for the different types of devices:
Extra Small Devices (e.g. cell phones) are the default, creating the “mobile first” concept in Bootstrap. This covers devices
smaller than 768px wide.
“Small Devices” (e.g. tablets) are targeted with #media (min-width: 768px) and (max-width: 991px) { ... }.
https://www.sitepoint.com/responsive-web-design-tips-bootstrap-css/

What is the best way to detect smaller devices like mobiles or tablets in CSS?

When i read about responsive design, people always seam to use this statement:
#media screen and(max-width: )
But mobile phones today seem to have really great resolution (often more than pc), whats the best way to detect small devices?
Thx ;=)
The screen resolution does not matter. The value used in media queries is the device width. For example:
My phone has a screen with a resolution of 1280x720 pixels. When held upright (in portrait mode) the width is 720px, but since it is an HD screen, it has a 200% ratio, and the resulting device width is 360px. This is the value used in media queries:
/* Even though my phone has a screen width of 720px… */
#media screen and (max-width: 360px) {
/*
* This code will apply
*/
}
#media screen and (min-width: 361px) {
/*
* This code will not apply
*/
}
The general rule is that phones in portrait mode have a device width less or equal to 400px, regardless of how many actual pixels their screen contains.
You can't directly query physical size.
You can, however, perform a media-type query for DPI along with Height and Width.
Example
#media(resolution: 326dpi) and (device-width: 640) and (device-height: 1136) {
// Iphone 5s
}
This should be a good starting point: List of displays by pixel density
Physical pixels and CSS pixels are not the the same on retina/HD mobile displays.
Research the viewport meta tag for information on device-width. i.e. <meta name="viewport" content="width=device-width"> is the CSS pixel width scaled at 100%.
See Viewport Device-Widths for a list of common mobile screen sizes.
When you are doing responsive design, you don't actually "detect" the screen size, rather you "target" various size using CSS Media Queries.
If you are using a library like Modernizer for example, that's when you are actually doing detection for various properties.

Defining css width/heights in device independent units?

I have a div and I'm trying to figure out how to get it to occupy the same amount of screen space regardless of device display density.
For example, let's say I have two devices that are each 5 inches wide. The first display has a device-pixel-ratio=1, and the second has a device-pixel-ratio=2.
Device 1: 5 inches wide, device-pixel-ratio=1
Device 2: 5 inches wide, device-pixel-ratio=2
So the second device has twice as many pixels packed into the same space.
My div style:
.myDivStyle {
width: 100px;
height: 50px;
}
If I understand correctly, Device 2 would appear to render the div at half the width/height as on Device 1.
If that's the case, is there a way to define our width/height in a device-independent unit? Or do we have to scale all our styles manually on page load etc after we examine the device-pixel-ratio attribute?
Thank you
Redefining your css size by using em unit would be good.
Some good links in this reference. Please check these
w3.org
w3.org
css-tricks
All the above links urge that, em is best suitable in cases when you want your document to behave well on wide range of devices.
This sounds more like you need to work with percentages instead of pixels. So it will use a "percentage" amount of your screen
.myDivStyle {
width: 50%;
height: 25%;
}
It would solve the problem with the ammount of pixels. This would solve the issue if your working with the screen of an Iphone and an Android, since they use two completely different resolutions. For instance, i believe the Iphone uses 320 by .. something, while my own Samsung Galaxy uses i think 480 by something. (dunno the true values)
Most of the devices would come under the following 3 resolutions
1) HVGA-Half of VGA (320 x 240)
2) WVGA- wide VGA(800x 480) - nearly 1.5 times of HVGA
3) HVGA 2x- (640 X 960) - IPHONE 4 uses this resolution
Write seperate css files for the above three resolutions by
#Media screen and (min-width: 320px) and (max-width: 480px){
/* css files here*/
}
or
#Media screen and (min-device-pixel-ratio: 2)
{/* css files here*/
}
Do the same with other resolutions. A css class created in 1 resolution should be copy pasted in all three resolutions to get a perfect view. By this way you can showcase a perfect device specific style

Nexus 7 screen.width returns 800 but media query max-width: 720px still applies

I have a Google Nexus 7 tablet I'm using for testing some responsive designs. In Javascript, the screen.width returns as 800px, as expected (the native resolution).
How come the following media query is picked up by the tablet when the max width of the screen is > 720px?
#media only screen and (max-width: 720px) and (orientation: portrait) {
.test{ background: red;}
}
Android does target density scaling in order to accommodate the varying screen densities of the Android ecosystem. The Android browser targets a medium screen density by default, trying to emulate the size of elements as if the screen was an MDPI screen.
Using this website, you can see that the result of this scaling is that device-width is 601 px and device-height is 880 px on the Nexus 7. Therefore, it falls within your max-width: 720px declaration and the background appears red.
window.screen.width and .height always returns the actual screen size. You have to remember that the Viewport Size and the Screen Size are two different things altogether.
If you do not want this behavior, you may add target-densitydpi=device-dpi to your <meta name="viewport"> tag. This will disable the Android target density scaling: device-width and device-height will report the native screen resolution of the device.
More information about Android's target density scaling is available in the Android Developers' Documentation.