html - 使用媒体查询时更改显示样式不起作用

标签 html css flexbox media-queries

我已经编写了两种不同类型的 css 显示样式(一种用于 flex,一种用于网格),并且随着屏幕尺寸的变化,它们中的每一种都会以不同的方式应用。我先做了移动版,然后是桌面版。 下面是移动版本的 html&CSS 代码,它按原样显示。

@media (max-width: 400px) {
  .container {
    background-color: lavender;
    display: flex;
    justify-content: center;
  }

  .header {
    background-color: rgb(255,80,100);
  }

  header {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .p {
    background-color: #5ABFF6;
  }

  p {
    font-size: 40px;
    padding: 95px;
    font-weight: bold;
  }

  .aside1 {
    background-color: #F8D557;
  }

  .aside1_1 {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .aside2 {
    background-color: #EA77B1;
  }

  .aside2_2 {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .footer {
    background-color: #AAE89D;
  }

  footer {
    font-size: 80px;
    padding: 100px;
    font-weight: bold;
  }
}

<body>
    <div class="container header">
        <header> Header </header>
    </div>
    <div class="container p">
        <p>But Apple is clearly disappointed with
           itself over this whole thing. It’s a humbling embarrassment
           for a company that so often highlights its focus on user
           security and privacy. "Security is a top priority for every
           Apple product, and regrettably we stumbled with this 
           release</p>
    </div>
    <div class="container aside1">
        <div class="aside1_1">Aside1</div>
    </div>
    <div class="container aside2">
        <div class="aside2_2">Aside2</div>
    </div>
    <div class="container footer">
        <footer>Footer</footer>
    </div>

但是,如果我附上第二个 CSS 代码(用于桌面屏幕尺寸),即使我将宽度缩小到 400 像素以下,页面布局也会以某种方式变得困惑。 下面是完整的 CSS 代码。

@media (max-width: 400px) {
  .container {
    background-color: lavender;
    display: flex;
    justify-content: center;
  }

  .header {
    background-color: rgb(255,80,100);
  }

  header {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .p {
    background-color: #5ABFF6;
  }

  p {
    font-size: 40px;
    padding: 95px;
    font-weight: bold;
  }

  .aside1 {
    background-color: #F8D557;
  }

  .aside1_1 {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .aside2 {
    background-color: #EA77B1;
  }

  .aside2_2 {
    font-size: 80px;
    padding: 95px;
    font-weight: bold;
  }

  .footer {
    background-color: #AAE89D;
  }

  footer {
    font-size: 80px;
    padding: 100px;
    font-weight: bold;
  }
}

@media (min-width:400px) {
  body {
    background-color: lavender;
    display: grid;
    grid-template-columns: 25% 50% 25%;
    grid-template-rows: 25% 50% 25%;
    text-align: center;
    height: 800px;
  }

  .header {
    background-color: rgb(255,80,100);
    grid-column: 1 / 4;
    padding-top: 50px;
  }

  header {
    font-size: 40px;
    padding: 20px;
    font-weight: bold;
  }

  .p {
    background-color: #5ABFF6;
    grid-column: 2 / 3;
  }

  p {
    font-size: 30px;
    padding: 20px;
    font-weight: bold;
  }

  .aside1 {
    background-color: #F8D557;
    grid-column: 1/2;
    grid-row: 2/3;
  }

  .aside1_1 {
    font-size: 40px;
    padding: 20px;
    font-weight: bold;
  }

  .aside2 {
    background-color: #EA77B1;
  }

  .aside2_2 {
    font-size: 40px;
    padding: 20px;
    font-weight: bold;
  }

  .footer {
    background-color: #AAE89D;
    grid-row: 3 / 4;
    grid-column: 1 / 4;
  }

  footer {
    font-size: 40px;
    padding: 20px;
    font-weight: bold;
    padding-top: 70px;
  }
}

最佳答案

因为 CSS 从 TOP 应用到 BOTTOM

这意味着最后设置的规则将被执行。

所以总是把你的媒体查询放在 css 的底部,例如

.container {
  /* your desktop styles goes here */
}

@media (max-width: 400px) {
  .container {
    /* your media styles goes here */
  }
}

更新

看看这个也Why do I have to put media queries at the bottom of the stylesheet?

关于html - 使用媒体查询时更改显示样式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47570645/

相关文章:

HTML 版本选择

css - 不要为特定的父类应用 CSS

css - 两个div并排-一个在最左边,另一个在中间

html - Chrome 不再显示文本

css - flex-box 如何在多个 flex-rows 的右侧有一个静态固定列?

javascript - 显示数组中的某些元素

c# - 使用 razor @Html.textboxfor 将字符串表示为货币或小数

html - 在标题内部对齐导航项

javascript - 如何在javascript中从列表中的HTML保存多个文本对象

css - 包含 flex 滚动条的 flex 布局