html - 在 CSS 中根据浏览器大小更改背景颜色

标签 html css media-queries background-color

我不知道这段代码有什么问题。背景颜色根本没有改变,它一直保持白色。

<style type="text/css">
      h1 {
        position: absolute;
        text-align: center;
        width: 100%;
        font-size: 6em;
        font-family: Roboto;
        transition: all 0.5s;
      }

      @media screen and (min-width: 0px) and (max-width: 400px) {
        background-color: red;
      }
      @media screen and (min-width: 401px) and (max-width: 599px) {
        background-color: green;
      }
      @media screen and (min-width: 600px) {
        background-color: blue;
      }
    </style>

最佳答案

尝试:

@media screen and (min-width: 0px) and (max-width: 400px) {
    body { background-color: red; }
}
@media screen and (min-width: 401px) and (max-width: 599px) {
    body { background-color: green; }
}
@media screen and (min-width: 600px) {
    body { background-color: blue; }
}

background-color 属性现在分配给 body

关于html - 在 CSS 中根据浏览器大小更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475124/

相关文章:

html - 更改 Shiny App 的背景颜色-- CSS

css - htaccess 重写条件错过 css 和图像

CSS 媒体查询,它不会将属性从 1024 更改为 768

html - 如何获取父级和嵌套 Div 的背景图像以进行响应式缩放

没有内联 CSS 的 JQuery 切换(向左滑动)

javascript - 缺少滚动条或缺少限制

javascript - 克服鼠标事件 Canvas 线上的抗锯齿

javascript - 移除悬停时的工具提示

html - DIVS 围绕一个形状

css - Firefox 在溢出滚动时使空 div 具有高度