指令是,当是screen < 600px时,我得到的div顺序是这样的red, green, blue:

enter image description here

screen > 600px岁时,我希望在下图中实现这一结果:

result

我的代码是:

body {
  background-color: white;
}

/* Add your CSS here */
[data-testid] {
  width: 100%;
  height: 100%;
  padding: 20px;
  color: white;
}

@media screen and (min-width: 600px) {
  .grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  }
  
  [data-testid="red"], [data-testid="blue"] {
  grid-column: 1;
}
  
  [data-testid="green"] {
  grid-column: 2;
  grid-row: span 2;
}
}
<body>
  <div class="grid-container">
    <div data-testid="red" style="background-color: red;">Red</div>
    <div data-testid="green" style="background-color: green;">Green</div>
    <div data-testid="blue" style="background-color: blue;">Blue</div>
  </div>
</body>

but I'm getting this problem as in the image : my result

我如何才能修复它以达到我想要的结果?

推荐答案

问题在于填充物.

这将创建宽度为100%(网格单元格)加上2*20px的项目.

此代码段通过设置box-sizing: border-box使填充成为宽度的一部分.

body {
  background-color: white;
}


/* Add your CSS here */

[data-testid] {
  width: 100%;
  height: 100%;
  padding: 20px;
  color: white;
  box-sizing: border-box;
}

@media screen and (min-width: 600px) {
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  [data-testid="red"],
  [data-testid="blue"] {
    grid-column: 1;
  }
  [data-testid="green"] {
    grid-column: 2;
    grid-row: span 2;
  }
}
<body>
  <div class="grid-container">
    <div data-testid="red" style="background-color: red;">Red</div>
    <div data-testid="green" style="background-color: green;">Green</div>
    <div data-testid="blue" style="background-color: blue;">Blue</div>
  </div>
</body>

Html相关问答推荐

如何格式化HTML文本,使其环绕关键字

仅包含名为X的子元素的元素的XPath?

如何使用html和css将两个项目垂直对齐

Angular 15 p-对话框不使用组件 Select 器呈现HTML

标签数据的XPath?

如何在html和css中创建动态调整大小的表元素

如何在R中渲染Quarto文档时动态设置html文件名

如何将功能附加到嵌入式药剂中的按钮?

如何从多个TO中获取一个范围标记以溢出其父标记

多次使用组件时计数Angular 14中嵌套数组的指令

为什么一个 CSS 网格框比其他网格框低?

为什么盒子不在div中显示?

Github上部署需要花费几小时时间:等待github pages部署批准

防止HTML输入中出现负数但接受小数

两部分问题 - 是什么导致了这个空白?

font awesome 的 CDN/CSS 如何工作?

不理解 CSS 中的 General sibling combinator (~)

如何创建带有偏移边框线的双色边框?

SVG 适用于 Safari,不适用于 Chrome

如何将图像高度设置为等于文本高度?