大家好,
对于网络开发很新,我试图学习如何使用漂亮的css网格工具,但我实际上坚持这个:
我希望我的卡在下一行中逐个自动流动(使用相同的列模板),但我实际上只在浏览器中看到一张卡。
我认为问题出在我.wrapper
身高vh
。我尝试了px
和%
,但我真的很难找到解决方案。
如果有人对这个问题有所了解,或者对我的(不好的......或好的?)编码方式有任何评论,我真的很感激!
/* Just some horrible stylization to better see boxes */
body {
padding: 0px;
margin: 0px;
}
div {
background-color: #f1c40f;
border: 1px solid white;
}
/* Definition of the 3 grids used*/
.wrapper {
height: 100vh;
box-sizing: border-box;
display: grid;
align-content: stretch;
justify-content: stretch;
grid-template-columns: 2fr 1fr 3fr 1fr 3fr 1fr 2fr;
grid-template-rows: 1fr 5fr 1fr;
grid-template-areas: "header header header header header header header"
"main main main main main main main"
"footer footer footer footer footer footer footer"
}
.main {
grid-area: main;
display: grid;
grid-template-columns: 60px 7fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: "sidebar box-content box-content";
}
/* This box-content's grid-template-columns have many ones, it is done to modify
only the grid-template-areas with media-queries for larger screen*/
.box-content {
grid-area: box-content;
display: grid;
grid-template-columns: 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr 1fr;
grid-template-rows: 1fr 7fr 1fr;
grid-auto-flow: row;
grid-template-areas: ". . . . . . . . ."
". card card card card card card card."
". . . . . . . . .";
}
/* Definition of the different element's grid-area*/
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.card {
grid-area: card;
}
.footer {
grid-area: footer;
}

<!-- I apologize for non-semantic tags, only a quick prototyping -->
<div class="wrapper">
<div class="header"> Header Header Header Header Header</div>
<div class="main">
<div class="sidebar">Sidebar Sidebar </div>
<div class="box-content">
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
</div>
</div>
<div class="footer"> Footer Footer Footer Footer Footer </div>
</div>
&#13;
感谢
答案 0 :(得分:2)
要让您的卡片逐行换行,请使用auto-fit
和minmax
功能。
以下是完整的解释:Getting columns to wrap in CSS Grid
这是一个基本演示:jsFiddle(重新调整浏览器宽度以查看效果)
以下是演示代码:
.wrapper {
height: 100vh;
display: grid;
align-content: stretch;
justify-content: stretch;
grid-template-columns: 2fr 1fr 3fr 1fr 3fr 1fr 2fr;
grid-template-rows: 1fr 5fr 1fr;
grid-template-areas: "header header header header header header header"
"main main main main main main main"
"footer footer footer footer footer footer footer"
}
.main {
grid-area: main;
display: grid;
grid-template-columns: 60px 7fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: "sidebar box-content box-content";
}
.box-content {
grid-area: box-content;
align-items: start;
align-content: start;
display: grid;
grid-gap: 10px;
padding: 10px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }
body {
padding: 0px;
margin: 0px;
}
div {
background-color: #f1c40f;
border: 1px solid white;
}
* {
box-sizing: border-box;
}
<div class="wrapper">
<div class="header"> Header Header Header Header Header</div>
<div class="main">
<div class="sidebar">Sidebar Sidebar </div>
<div class="box-content">
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
<div class="card"> Card Card Card Card Card Card </div>
</div>
</div>
<div class="footer"> Footer Footer Footer Footer Footer </div>
</div>
答案 1 :(得分:0)
/* Just some horrible stylization to better see boxes */
body {
padding: 0px;
margin: 0px;
}
div {
background-color: #f1c40f;
border: 1px solid white;
}
/* Definition of the 3 grids used*/
.wrapper {
height: 100%;
box-sizing: border-box;
align-content: stretch;
justify-content: stretch;
grid-template-columns: 2fr 1fr 3fr 1fr 3fr 1fr 2fr;
grid-template-rows: 1fr 5fr 1fr;
grid-template-areas: "header header header header header header header"
"main main main main main main main"
"footer footer footer footer footer footer footer"
}
.main {
grid-area: main;
display: grid;
grid-template-columns: 60px 7fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: "sidebar box-content box-content";
}
/* This box-content's grid-template-columns have many ones, it is done to modify
only the grid-template-areas with media-queries for larger screen*/
.box-content {
grid-area: box-content;
grid-template-rows: 1fr 7fr 1fr;
grid-auto-flow: row; padding:20px;
}
/* Definition of the different element's grid-area*/
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.card-1 {
display: grid;
width: 100%; margin-bottom:20px; min-height:500px;
}
.footer {
grid-area: footer;
}
&#13;
<!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="style.css">
<title>Layout 1</title>
</head>
<body>
<!-- I apologize for non-semantic tags, only a quick prototyping -->
<div class="wrapper">
<div class="header"> Header Header Header Header Header</div>
<div class="main">
<div class="sidebar">Sidebar Sidebar </div>
<div class="box-content">
<div class="card-1"> Card Card Card Card Card Card </div>
<div class="card-1"> Card Card Card Card Card Card </div>
<div class="card-1"> Card Card Card Card Card Card </div>
<div class="card-1"> Card Card Card Card Card Card </div>
</div>
</div>
<div class="footer"> Footer Footer Footer Footer Footer </div>
</div>
</body>
</html>
&#13;
你可以检查你的解决方案
以及更多示例 - https://gridbyexample.com/examples/