html - 媒体查询不起作用(提供代码)

标签 html css media-queries

出于某种原因,我无法使我的媒体查询正常工作....有人可以解释发生了什么吗?

屏幕尺寸为 241(chrome,即 ff)

CSS

@media screen and (max-width: 500px)
{
    body
      {
        width: 100%;
        background-color: black;
      }

  #header
    {
      display: none;
    }
}

HTML

<!DOCTYPE html> 
<html>
<head>    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

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

    <title>Test</title>

    <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
    <link href='http://fonts.googleapis.com/css?family=Great+Vibes' rel='stylesheet'  type='text/css'>
 </head>
 <body> 
     <div id="header">
         <p>This is my header</p>
     </div>

     <p>This is a paragraph</p>
 </body>
 </html>

因拼写错误而编辑

最佳答案

仔细检查您的媒体查询是否是样式表中的最后一项,并且没有被其他应用的样式覆盖,例如

    <style>
    @media screen and (max-width: 500px)
    {
    body
      {
        width: 100%;
        background-color: black;
      }
      #header
        {
          display: none;
        }
    }
    body {background-color: white;}
    </style>

我给出的例子永远不会改变 body 元素的背景颜色,因为媒体查询下面的样式会覆盖它

关于html - 媒体查询不起作用(提供代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019918/

相关文章:

javascript - 使用 JavaScript/jQuery 在任何给定元素上显示多个 CSS 值

html - 从 Google Chrome 样式选项卡添加 css @media

javascript - 是否可以将 div 中的随机内容克隆到另一个页面?

html - 如何向默认 ASP.NET 页面的边缘添加底纹?

HTML/CSS : A href exceeds linking image - how to avoid?

html - 在容器中设置文本以在跨度之间自动换行,但在跨度内的空格上不换行(理想情况下,使用 CSS)

html - 如何让 CSS 媒体查询在 Outlook/Gmail 上正常工作?

css - 我必须使用什么 webpack 加载器来组合媒体查询?

html - 如何为特定的 UL 分配 Active Class?

javascript - 函数点击jquery申请多个元素?