html - 使用 CSS 网格在移动设备上拆分顶部和底部的侧边栏

标签 html css css-grid

是否可以在移动设备上使用 CSS 网格拆分边栏,但在桌面设备上将其视为一种?

在移动设备上,我希望一切都堆叠起来介绍 侧边栏的一部分 内容,然后是侧边栏的其余部分.

在桌面上,我希望IntroContent 堆叠在一列中,2 个侧边栏堆叠在它们旁边。

我得到的关闭是

grid-template-areas:
  "intro"
  "sidebarTop"
  "content"
  "sidebarBottom";
@media (min-width: 600px) {
  grid-template-areas:
    "intro sidebarTop"
    "content sidebarTop"
    "content sidebarBottom";
}

但是 sidebarTop 总是尽可能多地占用空间。
我希望它只使用它使用的空间,让 sidebarBottom 占用其余空间。

有什么建议吗?

Example

最佳答案

您可以从这样的开始,然后根据您的需要调整每个 block :

main {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(4, 25%);
  grid-template-areas: "intro" "sidebarTop" "content" "sidebarBottom";
  grid-gap: 5px;
}

section {
  box-sizing: border-box;
  padding: 15px;
  background-color: tomato;
  color: white;
}

@media (min-width: 600px) {
  main {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: repeat(3, 33.33%);
    grid-template-areas: "intro sidebarTop" "content sidebarTop" "content sidebarBottom";
  }
}
<main>
  <section id="intro">
    intro
  </section>
  <section id="sidebarTop">
    sidebarTop
  </section>
  <section id="content">
    content
  </section>
  <section id="sidebarBottom">
    sidebarBottom
  </section>
</main>

关于html - 使用 CSS 网格在移动设备上拆分顶部和底部的侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58462015/

相关文章:

HTML CSS FLEX 问题,div 没有正确居中

html - OutLook 呈现不必要的空间

html - 更改 css 下拉子菜单字体大小

javascript - 如何防止在 jQuery 动画切换上重复动画?

css - 如何让 div float 在 jQuery UI 对话框上方?

html - 无法在 div 上应用动态高度 - 时间轴 View

html - 当什么都没有时,CSS 网格填充空间

html - 无法在CSS网格中创建一列

html - 将导航栏颜色混合到背景颜色(渐变?)

html - 不使用 CSS3 的边缘更具吸引力和美观