@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?
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.
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; .
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.
This can be done with Level 4 Media Queries: (Browser Support is quite good - CaniUse)
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:
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>
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>
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) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With