45

I've found a lot of threads here talking about CSS and mobile screens.

After searching, I couldn't find a solution for some devices. For example: Motorola Moto G4 Plus uses FULL HD width(1080px), only bootstraps detect the xs-view configuration.

Styling and trying and trying I couldn't get some styles set up.

So, how can I make the same procedure as Bootstrap to set up my styles and in the general procedure for all devices?

Please, only CSS or as much JS(if it possible, don't), the same as bootstrap.

I've tried the @media screen and @media all queries. Doesn't work.

Thanks for reading.

5 Answers 5

75

All touch screen and mobile devices (including tablets, smartphones) can be detected with css with the following @media rule.

@media only screen and (hover: none) and (pointer: coarse){

/* Regular CSS rules here*/

}
Sign up to request clarification or add additional context in comments.

7 Comments

Speeking of October 20 I don't know if I can trust in this rule or not. I'll give it a try.
Works, it works even with just (hover: none) which I have used before, but I wanted to optimize it to perfection, thank you for this. The only keyword prevents older browsers that do not support media queries with media features from applying the given styles. It has no effect on modern browsers.
@haZh's answer works also for me. That should be the top answer.
it still works in 2022, hovering on mobile devices has not been invented yet.
Seems to be working on 2023 :P
|
24

The @media rule is used to define different style rules for different media types/devices.

If it doesnt work, check your code. you might have made a typo somewhere.

Example:

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

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

W3schools: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

3 Comments

Thanks. i solved this using this kind of queries: @media screen and (max-width:767px), screen and (max-device-width:767px)
Honestly, I have doubts about this answer. It would certainly work with some devices but now that there are many QHD and 4K mobile, they wouldn't be selected by this. I don't know what the better solution is either.
3

CSS media query works, but what about ipads with big screens? You can do the following with javascript:

if (window.orientation !== undefined) {
  document.body.classList.add("mobile");
}

1 Comment

According to MDN it's deprecated and is no longer recommended
3

I found this code and works in all classic android devices:

  /* For Mobile Portrait View */
@media screen
  and (max-device-width: 480px)
  and (orientation: portrait){
    html{
     background-color: red;
     width: 80%;
    }
}
 
/* For Mobile Landscape View */
@media screen
  and (max-device-width: 640px)
  and (orientation: landscape){
   html{
      background-color: red;
     
    }
}
 
/* For Mobile Phones Portrait or Landscape View */
@media screen
  and (max-device-width: 640px){
    html{
      background-color: red;
     
    }
}

Source: https://www.geeksforgeeks.org/css3-media-query-for-all-devices/

1 Comment

i tried the 3rd and worked perfectly, thanks [+1]
1

For IOS (IPhone and IPAD) - and Mozilla on Android:

@media only screen and (hover: none) and (pointer: coarse){
/* Regular CSS rules here*/
}

but for Chromium on Android:

@media only screen and (hover: hover) and (pointer: coarse){
/* Regular CSS rules here*/

}

See Media Query Tester: https://hover-pointer-media-query.glitch.me/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.