如何在窗口大小上使CSS网格更改布局?

发布于 2025-02-02 17:28:51 字数 824 浏览 1 评论 0原文

CSS网格是否具有像材料UI网格必须根据布局尺寸更改的技巧?但是我想使用普通的CSS来做到

这一点,我现在有

.grid {
  height: 100vh;
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 2fr 8fr 2fr;
  grid-template-areas: 
    'header header'
    'sidebar body'
    'footer footer';

}


.header {
  grid-area: header;
  background-color: blue;
}

.sidebar {
  grid-area: sidebar;
  background-color: green;

}

.body {
  grid-area: body;
  background-color: red;
}

.footer {
  grid-area: footer;
  background-color: purple;
}

一个 ”在此处输入图像描述

它成为此布局。当屏幕较小时,我希望它变成

header (2/14)
sidebar (2/14)
body (8/14)
footer (2/14)

Does the CSS grid have a trick like the material UI grid has to change based on layout size? but I wanna do it with regular CSS

I have this right now

.grid {
  height: 100vh;
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 2fr 8fr 2fr;
  grid-template-areas: 
    'header header'
    'sidebar body'
    'footer footer';

}


.header {
  grid-area: header;
  background-color: blue;
}

.sidebar {
  grid-area: sidebar;
  background-color: green;

}

.body {
  grid-area: body;
  background-color: red;
}

.footer {
  grid-area: footer;
  background-color: purple;
}

enter image description here

It becomes this layout. When the screen is smaller I want it to become

header (2/14)
sidebar (2/14)
body (8/14)
footer (2/14)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2025-02-09 17:28:51

使用媒体查询。就像材料UI和其他所有UI库的使用一样,

您也可以根据自己的愿意更改最大宽度

@media only screen and (max-width: 600px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-rows: 2fr 2fr 8fr 2fr;
    grid-template-areas: 
      'header'
      'sidebar'
      'body'
      'footer';
  }
}

use media querys. Just as material ui and every other ui libraries use

You can change the max-width as you prefer

@media only screen and (max-width: 600px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-rows: 2fr 2fr 8fr 2fr;
    grid-template-areas: 
      'header'
      'sidebar'
      'body'
      'footer';
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文