css – 是否可以在媒体查询中嵌套媒体查询?

前端之家收集整理的这篇文章主要介绍了css – 是否可以在媒体查询中嵌套媒体查询?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这可能吗?这似乎是一个整洁的解决方案,但我不知道它是否会有效。
  1. @media only screen
  2. and (min-device-width : 320px)
  3. and (max-device-width : 480px) {
  4.  
  5. /* Code for both portrait and landscape */
  6.  
  7. @media (orientation:portrait) {
  8.  
  9. /* Code for just portrait */
  10.  
  11. }
  12.  
  13. @media (orientation:landscape) {
  14.  
  15. /* Code for just landscape */
  16.  
  17. }
  18.  
  19. }

解决方法

您应该能够使用 nest @media rules this way in CSS3,但大多数浏览器尚未支持。详见 this answer

您将不得不完全展开和重复顶级媒体查询内部规则,以便它可以在浏览器之间工作(我想像,SCSS处理器会产生类似的东西):

  1. @media only screen
  2. and (min-device-width: 320px)
  3. and (max-device-width: 480px) {
  4. /* Code for both portrait and landscape */
  5. }
  6.  
  7. @media only screen
  8. and (min-device-width: 320px)
  9. and (max-device-width: 480px)
  10. and (orientation: portrait) {
  11.  
  12. /* Code for just portrait */
  13.  
  14. }
  15.  
  16. @media only screen
  17. and (min-device-width: 320px)
  18. and (max-device-width: 480px)
  19. and (orientation: landscape) {
  20.  
  21. /* Code for just landscape */
  22.  
  23. }

猜你在找的CSS相关文章