在移动视图中,我的网格项目以一种我不想要的方式显示.(请看调整到583px左右的页面)

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;600&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  margin: 70px 10% 50px 10%;
  font-family: Poppins, sans-serif;
  color: #4E5060;
  background-color: #FAFAFA;
  display: grid;
  justify-content: center;
}

header {
  text-align: center;
  margin-inline: auto;
  width: 50%;
}

header h1 {
  font-weight: 200;
  margin-bottom: 20px;
}

p {
  color: #A7A6AA;
}

header p {
  margin-bottom: 50px;
}

.bold {
  font-weight: bold;
}

.main-grid {
  display: grid;
  grid-template-columns: repeat(3, 350px);
  grid-template-rows: repeat(4, .5fr);
  gap: 30px;
}

.card {
  background-color: #fff;
  padding: 25px 35px;
  height: 255px;
  box-shadow: 0 10px 10px #ddd;
  border-radius: 8px;
  min-width: 350px;
  max-width: 350px;
  margin-inline: auto;
}

.card:nth-of-type(1) {
  grid-row: 2/4;
  border-top: 5px solid hsl(180, 62%, 55%);
}

.card:nth-of-type(2) {
  grid-column: 2 / 3;
  grid-row: 1 / 3;
  border-top: 5px solid hsl(0, 78%, 62%);
}

.card:nth-of-type(3) {
  grid-column: 2 / 3;
  grid-row: 3 / 5;
  border-top: 5px solid hsl(34, 97%, 64%);
}

.card:nth-of-type(4) {
  grid-column: 3 / 4;
  grid-row: 2 / 4;
  border-top: 5px solid hsl(212, 86%, 64%);
}

.card h3 {
  margin-bottom: 10px;
}

.card p {
  font-size: 14px;
}

@media (min-width: 601px) and (max-width: 1200px) {
  header {
    width: 100%;
  }
  .main-grid {
    grid-template-columns: 1fr 1fr;
    gap: 20px;
  }
  .card {
    margin-inline: initial;
  }
  .card:nth-of-type(1) {
    grid-column: 1;
    grid-row: 1;
    margin-left: auto;
  }
  .card:nth-of-type(2) {
    grid-column: 2;
    grid-row: 1;
  }
  .card:nth-of-type(3) {
    grid-column: 1;
    grid-row: 2;
    margin-left: auto;
  }
  .card:nth-of-type(4) {
    grid-column: 2;
    grid-row: 2;
  }
}

@media (max-width: 600px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
  header {
    width: 100%;
  }
  .card {
    min-width: 350px;
    max-width: initial;
  }
  .card:nth-of-type(1) {
    grid-column: 1;
    grid-row: 1;
  }
  .card:nth-of-type(2) {
    grid-column: 1;
    grid-row: 2;
  }
  .card:nth-of-type(3) {
    grid-column: 1;
    grid-row: 3;
  }
  .card:nth-of-type(4) {
    grid-column: 1;
    grid-row: 4;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
    <link rel="stylesheet" href="style.css">

    <title>Frontend Mentor | Four card feature section</title>
  </head>
  <body>
    <header>
      <h1>Reliable, efficient delivery<br><span class="bold">Powered by Technology</span>
      </h1>

      <p>Our Artificial Intelligence powered tools use millions of project data points 
      to ensure that your project is successful</p>
    </header>

    <section class="main-grid">

      <div class="card">
        <h3>Supervisor</h3>
        <p>Monitors activity to identify project roadblocks</p>
      </div>
      <div class="card">
        <h3>Team Builder</h3>
        <p>Scans our talent network to create the optimal team for your project</p>
      </div>
      <div class="card">
        <h3>Karma</h3>
        <p>Regularly evaluates our talent to ensure quality</p>
      </div>
      <div class="card">
        <h3>Calculator</h3>
        <p>Uses data from past projects to provide better delivery estimates</p>
      </div>
      
    </section>
  </body>
</html>

页面上的第二张和第四张卡片很大,就像我想要的(和期望的那样),而其他两张卡片更小.换句话说,它没有均匀地调整大小.

我怎样才能得到我想要的所有4张卡都是相同宽度的行为?(如果你继续将视区缩小到370px,栅格确实是这样的行为)

此外,我将媒体查询设置为600px,但我注意到它不会改变,直到我将浏览器大小调整到约450px?有人知道这是为什么吗?

推荐答案

在手机上,你的.card受到这margin-inline: auto;的影响.

我将此添加到您的移动媒体查询中,以修复移动设备上的错误大小:

  .card {
    min-width: 350px;
    max-width: initial;
    margin-inline: 0;
  }

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;600&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  margin: 70px 10% 50px 10%;
  font-family: Poppins, sans-serif;
  color: #4E5060;
  background-color: #FAFAFA;
  display: grid;
  justify-content: center;
}

header {
  text-align: center;
  margin-inline: auto;
  width: 50%;
}

header h1 {
  font-weight: 200;
  margin-bottom: 20px;
}

p {
  color: #A7A6AA;
}

header p {
  margin-bottom: 50px;
}

.bold {
  font-weight: bold;
}

.main-grid {
  display: grid;
  grid-template-columns: repeat(3, 350px);
  grid-template-rows: repeat(4, .5fr);
  gap: 30px;
}

.card {
  background-color: #fff;
  padding: 25px 35px;
  height: 255px;
  box-shadow: 0 10px 10px #ddd;
  border-radius: 8px;
  min-width: 350px;
  max-width: 350px;
  margin-inline: auto;
}

.card:nth-of-type(1) {
  grid-row: 2/4;
  border-top: 5px solid hsl(180, 62%, 55%);
}

.card:nth-of-type(2) {
  grid-column: 2 / 3;
  grid-row: 1 / 3;
  border-top: 5px solid hsl(0, 78%, 62%);
}

.card:nth-of-type(3) {
  grid-column: 2 / 3;
  grid-row: 3 / 5;
  border-top: 5px solid hsl(34, 97%, 64%);
}

.card:nth-of-type(4) {
  grid-column: 3 / 4;
  grid-row: 2 / 4;
  border-top: 5px solid hsl(212, 86%, 64%);
}

.card h3 {
  margin-bottom: 10px;
}

.card p {
  font-size: 14px;
}

@media (min-width: 601px) and (max-width: 1200px) {
  header {
    width: 100%;
  }
  .main-grid {
    grid-template-columns: 1fr 1fr;
    gap: 20px;
  }
  .card {
    margin-inline: initial;
  }
  .card:nth-of-type(1) {
    grid-column: 1;
    grid-row: 1;
    margin-left: auto;
  }
  .card:nth-of-type(2) {
    grid-column: 2;
    grid-row: 1;
  }
  .card:nth-of-type(3) {
    grid-column: 1;
    grid-row: 2;
    margin-left: auto;
  }
  .card:nth-of-type(4) {
    grid-column: 2;
    grid-row: 2;
  }
}

@media (max-width: 600px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
  header {
    width: 100%;
  }
  .card {
    min-width: 350px;
    max-width: initial;
    margin-inline: 0;
  }
  
  .card:nth-of-type(1) {
    grid-column: 1;
    grid-row: 1;
  }
  .card:nth-of-type(2) {
    grid-column: 1;
    grid-row: 2;
  }
  .card:nth-of-type(3) {
    grid-column: 1;
    grid-row: 3;
  }
  .card:nth-of-type(4) {
    grid-column: 1;
    grid-row: 4;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
    <link rel="stylesheet" href="style.css">

    <title>Frontend Mentor | Four card feature section</title>
  </head>
  <body>
    <header>
      <h1>Reliable, efficient delivery<br><span class="bold">Powered by Technology</span>
      </h1>

      <p>Our Artificial Intelligence powered tools use millions of project data points 
      to ensure that your project is successful</p>
    </header>

    <section class="main-grid">

      <div class="card">
        <h3>Supervisor</h3>
        <p>Monitors activity to identify project roadblocks</p>
      </div>
      <div class="card">
        <h3>Team Builder</h3>
        <p>Scans our talent network to create the optimal team for your project</p>
      </div>
      <div class="card">
        <h3>Karma</h3>
        <p>Regularly evaluates our talent to ensure quality</p>
      </div>
      <div class="card">
        <h3>Calculator</h3>
        <p>Uses data from past projects to provide better delivery estimates</p>
      </div>
      
    </section>
  </body>
</html>


现在您的网格元素将具有相同的大小,如果您想要更改它们的大小,您可以更改margin-inline属性(例如 margin-inline: 5px)

Css相关问答推荐

为什么我的Angular material css文件不工作?

我应该在哪里放置expo /Reaction本地项目的CSS重置文件?

创建始终为剪辑元素的300%高度并左对齐的圆形剪辑路径

关闭Superset中的Deck.gl工具提示

为什么CSS flex似乎应用了两次内部元素的填充?

如何使用混合混合模式更改动画背景上的文本 colored颜色

.NET 8 预览版 6 中不使用新项目模板提供 Blazor CSS 文件

不明白为什么 div 不均匀并且不填满页面

SVG样式中的CSS类名是否是全局的?

如何在Safari中隐藏CSS的offset-path属性?CSS支持规则不按预期工作

Material UI Modal 因背景而变暗

条件宽度和省略号不适用于样式化组件 - React.js

纯 CSS/JS 的 SVG 连续向内方形螺旋动画

在样式化组件中使图像重叠

CSS nth-child 表示大于和小于

如何定位 CSS 网格布局中的特定列或行?

iPhone X / 8 / 8 Plus CSS 媒体查询

Bootstrap 3 Glyphicons CDN

:last-child 没有按预期工作?

Rails 中正确的 SCSS assets资源 struct