Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code CSS media queries targeting ALL mobile devices and tablets?

@media only screen and (max-device-width : 640px) { /* Styles */ }  @media only screen and (max-device-width: 768px) { /* Styles */ } 

This is what I have so far. The PSD mockup for the mobile site I'm working on, is 640px wide. The other one, the tablet version of the site, is 768px. I was able to test the site in my web browser by only using max-width, but now it's time to get this site working on the devices, and it's still rendering the regular full size web page. The two queries above are my best guess. Where am I going wrong?

like image 509
user1666487 Avatar asked Sep 18 '12 03:09

user1666487


People also ask

How do I write multiple media queries in CSS?

You may use as many media queries as you would like in a CSS file. Note that you may use the and operator to require multiple queries to be true, but you have to use the comma (,) as the or operator to separate groups of multiple queries. The not keyword can be used to alter the logic as well.

Which CSS media query would you use to target a high resolution mobile display?

In the CSS, we want to add a (max-width: 600px) for the media query which tells the computer to target devices with a screen width of 600px and less. Inside the media query, we change the background styles for the body to background-color: #87ceeb; .

What is the media query for tablets?

Media Query is a popular technique that enables to deliver a style sheet to different devices which have different screen sizes and resolutions respectively. They are used to customize the appearance of a website on multiple devices.


1 Answers

This can be done with Level 4 Media Queries: (Browser Support is quite good - CaniUse)

Interaction Media Features

The idea here is to target devices based on their capabilities. (as apposed to say, checking the size or resolution of the device which tend to be moving targets)

The pointer media feature queries the quality of the pointer mechanism used by the device.

The hover media feature queries the user’s ability to hover over elements on the page with a given device.

So to answer the question...

Mobile devices/tables are similar in that:

1) The accuracy of the primary input mechanism of the device is limited.

This means we could use the following media query:

@media (pointer:coarse) {     /* custom css for "touch targets" */ } 

div {    width: 400px;    height: 200px;    color: white;    padding: 15px;    font-size: 20px;    font-family: Verdana;    line-height: 1.3;    background: green;  }  @media (pointer:coarse) {     div {      background: red;    }  }
<h2>The <a href="https://www.w3.org/TR/mediaqueries-4/#pointer">pointer media feature</a> queries the quality of the pointer mechanism used by the device.</h2>  <div>The background here will be green on 'desktop' (devices with an accurate pointing mechanism such as a mouse) and red on 'mobile' (devices with limited accuracy of primary input mechanism) </div>

Codepen Demo

2) The primary pointing system can’t hover

So our media query would look like this:

@media (hover: none) {     /* custom css for devices where the primary input mechanism cannot hover     at all or cannot conveniently hover } 

NB: Chrome/Android prior to version 59 required the on-demand value for hover/any-hover media queries. This value was later removed from the spec and no longer required by Chrome from version 59. So to support older versions of Android you need

@media (hover:none), (hover:on-demand) {    /* custom css for "touch targets" */ } 

div {    width: 400px;    height: 150px;    color: white;    padding: 15px;    font-size: 20px;    font-family: Verdana;    line-height: 1.3;    background: green;  }  @media (hover:none), (hover:on-demand){     div {      background: red;    }  }
<h2>The <a href="https://www.w3.org/TR/mediaqueries-4/#hover">hover media feature</a> queries the user’s ability to hover over elements on the page</h2>  <div>The background here will be green on 'desktop' (devices which support hover) and red on 'mobile' (devices which don't [easily] support hover ) </div>

Codepen Demo

NB:

Even if you were to connect a mouse to a mobile/tablet, the hover media feature still matches none since their primary interaction mode doesn't support hover.

If we do want to take secondary devices into consideration we could use the any-pointer and any-hover features

So if we wanted mobile devices connected with a pointing device to be treated like a 'desktop' we could use the following:

@media (any-hover: hover) { ... } 

Extra reading

like image 130
Danield Avatar answered Sep 19 '22 09:09

Danield