3 media queries for iphone portrait, landscape and

2019-03-09 14:39发布

I have tried the different combinations of width & device-width but on the iPhone in landscape this code never turns the background red;

I am having problem when I have more than 1 media query. See this JSfiddle example the div background is never green unless you remove the last media query

This is what I would like 3 different media queries which target:

  1. both smartphones and tablets(portrait only). This will be where I have all my generic styles for responsive layout
  2. width: 320px - 479px - this will apply to small screens, such as iphone in portrait only
  3. width: 480px - 640px - this will apply to larger screens such as iphone in landscape and ipad in portrait. Not ipad in landscape.

Note this is for a HTML email so its not possible to use JS.

@media only screen and (max-width: 640px) {
    body { padding: 10px !important }
}
/* Small screen */
@media only screen and (min-device-width: 320px) and (max-device-width: 479px) {
    body { background: blue !important }
}
/* iPhone landscape and iPad portrait */
@media only screen and (max-device-width: 480px) and (max-device-width: 640px) {
    body { 
       background: red !important 
       -webkit-text-size-adjust:none; 
    }
}

Reference: http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

5条回答
男人必须洒脱
2楼-- · 2019-03-09 14:59

Media queries also support device orientation. You should try that

@media screen and (orientation:portrait) {
    /* Portrait styles */
}

@media screen and (orientation:landscape) {
    /* Landscape styles */
}

You can also combine them with width like so

@media screen and (max-device-width : 320px) and (orientation:portrait) {

}
查看更多
甜甜的少女心
3楼-- · 2019-03-09 15:00

Responsive emails can be tough because there are so many e-mail clients out there, and support can be limited for media queries. You may just want to look into making your divs fluid with percentages, so they scale, rather than media queries. The people at zurb have some great templates which may help out too. http://www.zurb.com/playground/responsive-email-templates

Hope that helps.

查看更多
三岁会撩人
4楼-- · 2019-03-09 15:01

Your own attempt modified

@media only screen and (max-width: 640px) and (orientation: portrait) {
    body { padding: 10px !important }
}

/* Small screen */
@media only screen and (min-device-width: 320px) and (max-device-width: 479px) and (orientation: portrait) {
    body { background: blue !important }
}

/* iPhone landscape and iPad portrait */
@media only screen and (max-device-width: 480px) and (orientation: landscape),
@media only screen and (max-device-width: 640px) and (orientation: portrait)  {
    body { 
       background: red !important 
       -webkit-text-size-adjust:none; 
    }
}
查看更多
萌系小妹纸
5楼-- · 2019-03-09 15:03

Take a look at this resource, will give you media queries for pretty much everything http://arcsec.ca/media-query-builder/ , you need to be be specifying a min width aswell. also less of the !important, dirty :)

in your case

@media only all and (min-device-width: 320px) and (max-device-width: 480px) {
/* insert styles here */
}
查看更多
倾城 Initia
6楼-- · 2019-03-09 15:03

i tried this by using Om's solution in a diff. way it worked for me, hope it helps someone

@media only screen and (max-device-width:1024px) and (orientation: portrait)     {
    /*style*/    
    }
@media only screen and (max-device-width: 1024px) and (orientation: landscape)  {
    /*style*/
}
查看更多
登录 后发表回答