简体   繁体   English

CSS 网格布局,简单的响应式设计

[英]CSS grid layout, simple responsive design

I'm trying to understand the CSS grid.我正在尝试了解 CSS 网格。 My layout consist of two menus (red), one on each side of a main content box in the middle (blue).我的布局由两个菜单(红色)组成,一个在中间的主要内容框的每一侧(蓝色)。

When the screen becomes smaller and there is not enough space for 3 columns, I want the second (right) side menu to appear right under the first (left) one.当屏幕变小并且没有足够的空间容纳 3 列时,我希望第二个(右)侧菜单出现在第一个(左)侧菜单的正下方。

在此处输入图片说明

Consider the height of main content and the side menus as random values, as the height will change depending on how much content/menu items is in them.将主要内容和侧边菜单的高度视为随机值,因为高度会根据其中的内容/菜单项的数量而变化。 Solution should work regardless of height.无论高度如何,解决方案都应该有效。

My current layout almost works, except that the second menu appears under the end of the main content, and not right after the first menu.我目前的布局几乎可以正常工作,只是第二个菜单出现在主要内容的末尾,而不是在第一个菜单之后。 How can I solve this?我该如何解决这个问题?

 .main-container { width: 100%; height: 100%; } @media only screen and (min-width: 400px) { .main-container { display: grid; grid-gap: 20px; grid-template-columns: 40px 80px 40px; justify-content: center; } } @media only screen and (max-width: 400px) { .main-container { display: grid; grid-gap: 20px; grid-template-columns: 40px 80px; justify-content: center; } } .side-menu { width: 100%; height: 50px; background-color: red; } .main-content { width: 100%; height: 300px; background-color: blue; }
 <div class="main-container"> <div class="side-menu"></div> <div class="main-content"></div> <div class="side-menu"></div> </div>

Like the title states "CSS grid layout" i don't want to use js.就像标题所说的“CSS 网格布局”一样,我不想使用 js。

In media screen you can set up 2 rows to do the job Also i suggest to use "fr" instead of pixels in grid layout.在媒体屏幕中,您可以设置 2 行来完成这项工作 另外我建议在网格布局中使用“fr”而不是像素。

You can find a nice guide here https://css-tricks.com/snippets/css/complete-guide-grid/你可以在这里找到一个很好的指南https://css-tricks.com/snippets/css/complete-guide-grid/

 .main-container { width: 100%; height: 100%; } @media only screen and (min-width: 400px) { .main-container { display: grid; grid-gap: 20px; grid-template-columns: 40px 80px 40px; grid-template-rows: 40px; justify-content: center; grid-auto-flow: rows; } .side-menu:nth-child(odd) { width: 100%; height: 50px; background-color: red; grid-column: 1; } .side-menu:nth-child(even) { width: 100%; height: 50px; background-color: red; grid-column: 3; } } @media only screen and (max-width: 400px) { .main-container { display: grid; grid-gap: 20px; grid-template-columns: 40px 80px; grid-auto-rows: 40px; justify-content: center; } .side-menu { width: 100%; height: 50px; background-color: red; grid-column: 1; } } .main-content { width: 100%; height: 300px; background-color: blue; }
 <div class="main-container"> <div class="side-menu"></div> <div class="main-content"></div> <div class="side-menu"></div> </div>

You haven't defined any rows in the grid:您尚未在网格中定义任何行:

.main-container {
  display: grid;
  grid-gap: 20px;
  grid-template-columns: 40px 80px 40px;
  justify-content: center;
}

Therefore, all rows are implicit , meaning they are created automatically to accommodate items.因此,所有行都是隐式的,这意味着它们是自动创建的以容纳项目。

The sizing function for implicit rows isgrid-auto-rows and its default value is auto .隐式行的大小函数是grid-auto-rows ,其默认值为auto This means that rows take the size of the content.这意味着行采用内容的大小。

You've set the size of your grid items:您已经设置了网格项目的大小:

.side-menu {
  height: 50px;
}

.main-content {
  height: 300px;
}

So the .main-content , being the taller of the grid items, sets the height of the row:所以.main-content是网格项中较高的,设置行的高度:

在此处输入图片说明

As you can see, you have a grid container with one row.如您所见,您有一个包含一行的网格容器。

Then your media query kicks in for smaller screens:然后你的媒体查询开始用于较小的屏幕:

@media ( max-width: 400px ) {
  .main-container {
    grid-template-columns: 40px 80px;
  }
}

The new grid-template-columns rule alters the grid from three to two columns.新的grid-template-columns规则将网格从三grid-template-columns更改为两列。

This forces the grid to create a second implicit row to accommodate the second .side-menu , whose column has been removed.这会强制网格创建第二个隐式行以容纳第二个.side-menu ,其列已被删除。

在此处输入图片说明

In short, a second row exists under the first row.简而言之,第二行存在于第一行之下。 The first row is 300px tall.第一行是 300px 高。 This results in a wide vertical gap between the first and second menus.这导致第一和第二菜单之间存在较大的垂直间隙。


One Possible Solution一种可能的解决方案

Use multiple smaller rows and make your items span across them.使用多个较小的行并使您的项目跨越它们。 The

The code below renders like this:下面的代码呈现如下:

在此处输入图片说明

 .main-container { display: grid; grid-template-columns: 40px 80px 40px; grid-auto-rows: 10px; /* new */ grid-column-gap: 20px; /* adjusted */ } .side-menu:first-child { /* height: 50px; */ grid-column: 1; grid-row: span 5; } .side-menu:last-child { /* height: 50px; */ grid-column: 3; grid-row: span 5; } .main-content { /* height: 300px; */ grid-column: 2; grid-row: span 30; } @media ( max-width: 400px ) { .main-container { grid-template-columns: 40px 80px; } .side-menu:last-child { grid-column: 1; grid-row: 7 / span 5; } } .main-content { background-color: blue; } .side-menu { background-color: red; }
 <div class="main-container"> <div class="side-menu"></div> <div class="main-content"></div> <div class="side-menu"></div> </div>

codepen demo代码笔演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM