I have two divs, which I'll refer to as A & B. The desired behaviour is:
- If the screen width is less than 400px, only A should be visible.
- If the screen in landscape, greater than 400px in width, but less than 400px in portrait orientation, only B should be visible.
- Else (the screen width is greater than 400px in both portrait and landscape), both A and B should be visible.
The second item is the one that's proving to be problematic.
My current approach is working for iOS devices, which always report min-device-width
& max-device-width
as the portrait values regardless of the current screen orientation, but not for android and others, where the device-width
values change with the orientation:
/* item 3 */
.a, .b {
display: block;
}
/* item 2 */
@media only screen and (min-width: 400px) and (max-device-width: 400px) and (orientation: landscape) {
.a { display: none; }
}
/* item 1 */
@media only screen and (max-width: 400px) {
.b { display: none }
}
Is it possible to implement this behaviour purely in media queries so that it works on all devices?
I'm keen to avoid JavaScript, UA Sniffing, and the use of the deprecated media types (e.g. handheld) if possible.