html - 如何实现这种响应式布局
问题描述
我有 3 个元素:A、B 和 C。
在手机上,我想要这个:
AA
AA
BB
BB
CC
CC
在桌面上,我想要这个:
AABB
AABB
CCBB
CCBB
我怎样才能做到这一点?
解决方案
你已经标记了这个flexbox,但是你想要的布局是 2-d,所以你应该使用css-grid。
将下面示例中的类替换为媒体查询。
document.querySelector("button").addEventListener("click", () => document.body.classList.toggle("desktop"));
div {
font-size: 20px;
padding: 10px;
text-align: center;
}
#a {
background: red;
color: white;
grid-area: grid-a;
}
#b {
background: green;
color: black;
grid-area: grid-b;
}
#c {
background: black;
color: white;
grid-area: grid-c;
}
.desktop main {
display: grid;
grid-template-columns: auto-fit auto-fit;
grid-template-rows: auto-fit auto-fit;
grid-template-areas: "grid-a grid-b" "grid-c grid-b";
}
<main>
<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
</main>
<button>toggle</button>
推荐阅读
- javascript - 验证一个长字符串是否有足够的可破坏空间用于显示
- xml - 无法使用 Powershell 使用 Xpath 循环 XML
- xml - XPath 访问外循环
- reactjs - 创建 React App Typecsript 问题(保留关键字接口)
- c++ - 如何访问包含指向字符串的指针的向量的元素?
- .htaccess - 重定向到同一目录的 RewriteBase 规则不起作用
- javascript - 匹配时存储在变量中的正则表达式不起作用
- python - 使用 pg.key.get_pressed() 按键,命令重复多次一按
- android - 在 Android 上检索错误对象而不是 HTTP 400
- javascript - 在进入输赢方法之前,如何让我的屏幕更新?