CSS 媒体查询似乎无法正常工作

Posted

技术标签:

【中文标题】CSS 媒体查询似乎无法正常工作【英文标题】:CSS Media Queries Does Not Seem To Be Working Properly 【发布时间】:2022-01-23 10:17:11 【问题描述】:

我添加了这个 CSS 媒体设置,用于在 iPhone 尺寸方面更改网页的字体大小:

body
    font-size:10pt;

@media only screen and (max-width: 375px) 
    body
        font-size:20pt;
    

但是当我调整它的大小时,它并没有显示20pt的字体大小。

我还添加了这个元标记:

<meta name="viewport" content="width=device-width, initial-scale=1">

那么这里出了什么问题?

如何为最大宽度为 375 像素的 iPhone 尺寸正确添加 CSS 媒体查询?

提前致谢。

【问题讨论】:

它工作正常 @LaljiTadhani 我使用 Chrome Resizer Window 对此进行了测试,但它不起作用。你是怎么测试的? 也试过了。创建了一个默认的 html 文档。在正文中添加了您的 CSS 和一些文本。使用 chrome resizer,你可以看到文本是如何变化的(375px 更大,376px 更大) 【参考方案1】:

这取决于您要测试的模型。

/* ----------- iPhone 4 and 4S ----------- */

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) 



/* Portrait */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: portrait) 


/* Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: landscape) 



/* ----------- iPhone 5, 5S, 5C and 5SE ----------- */

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2) 



/* Portrait */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: portrait) 


/* Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: landscape) 



/* ----------- iPhone 6, 6S, 7 and 8 ----------- */

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 667px) 
  and (-webkit-min-device-pixel-ratio: 2)  



/* Portrait */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 667px) 
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: portrait)  



/* Landscape */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 667px) 
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: landscape)  



/* ----------- iPhone 6+, 7+ and 8+ ----------- */

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 414px) 
  and (max-device-width: 736px) 
  and (-webkit-min-device-pixel-ratio: 3)  



/* Portrait */
@media only screen 
  and (min-device-width: 414px) 
  and (max-device-width: 736px) 
  and (-webkit-min-device-pixel-ratio: 3)
  and (orientation: portrait)  



/* Landscape */
@media only screen 
  and (min-device-width: 414px) 
  and (max-device-width: 736px) 
  and (-webkit-min-device-pixel-ratio: 3)
  and (orientation: landscape)  



/* ----------- iPhone X ----------- */

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 812px) 
  and (-webkit-min-device-pixel-ratio: 3)  



/* Portrait */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 812px) 
  and (-webkit-min-device-pixel-ratio: 3)
  and (orientation: portrait)  



/* Landscape */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 812px) 
  and (-webkit-min-device-pixel-ratio: 3)
  and (orientation: landscape)  


【讨论】:

以上是关于CSS 媒体查询似乎无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

CSS 媒体查询在移动设备上不起作用

媒体查询在 iOS 版 Chrome 中无法正常工作

CSS 媒体查询最近不适用于 iPad

CSS媒体查询由于某种原因不起作用

Microsoft Edge,媒体查询无法正常工作

为啥媒体查询的顺序在 CSS 中很重要?