html - 如何使 CSS 二维网格响应?
问题描述
我有一个带有网格的二维 CSS。我无法实现响应能力。当屏幕按需要填满时,我得到以下信息。
我压缩了第 2 列和第 3 列,而不是如下响应。
我想要以下结果。
请看下面的代码:
.frame {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
grid-template-rows: repeat(2, 1fr);
grid-gap: 40px;
border: 1px solid red;
}
.blog1,
.blog2,
.blog3,
.blog4 {
height: 200px;
border: 1px solid green;
}
.blog3 {
grid-column: 3;
grid-row: 1/3;
height: 100%;
}
.blog4 {
height: 200px;
border: 1px solid green;
grid-column: 1/3;
grid-row: 2;
}
<div class="frame">
<div class="blog1">one</div>
<div class="blog2">two</div>
<div class="blog3">three</div>
<div class="blog4">four</div>
</div>
解决方案
尽管您可以将宽度更改为任何内容或将媒体更改为您想要的屏幕,但这将做到这一点!
.frame {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
grid-template-rows: repeat(2, 1fr);
grid-gap: 40px;
border: 1px solid red;
}
.blog1,
.blog2,
.blog3,
.blog4 {
height: 200px;
border: 1px solid green;
}
.blog3 {
grid-column: 3;
grid-row: 1/3;
height: 100%;
}
.blog4 {
height: 200px;
border: 1px solid green;
grid-column: 1/3;
grid-row: 2;
}
@media screen and (max-width: 2000px) {
.frame {
grid-template-columns: auto;
grid-template-rows: none;
}
.blog1, .blog2, .blog3, .blog4 {
width: 100%;
height: 200px;
grid-column: auto;
grid-row: auto;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="">
<title>Document</title>
</head>
<body>
<div class="frame">
<div class="blog1">one</div>
<div class="blog2">two</div>
<div class="blog3">three</div>
<div class="blog4">four</div>
</div>
</body>
</html>
推荐阅读
- c# - 在未安装 .Net 的 xp 上运行 .Net Framework 应用程序
- samsung-mobile - Note 5 刷 LineageOS 16 和 HyperKernel 后无 SIM 卡、无 WI-FI 和无效 imei
- python - 线性模型:鉴于这两个模型在测试数据集上的表现同样出色,您更喜欢哪一个,为什么?
- javascript - 当 Vue 组件在视口中时,如何添加 body 类?
- reactjs - 在 Formik 中使用 ErrorMessage 组件显示 setFieldErrors 的值
- c# - C#:process.OutputDataReceived 并不总是被触发
- javascript - 如何在 Nodejs 中合并多个 docx 文件?
- nginx - Nginx 动态负载均衡
- swift - 可选类型的值必须被解包以引用成员,但它是隐式解包的可选属性
- javascript - 如何从我获得的 API 数据创建项目列表?