最近研究网格布局,layout in this image,我自己的实现如下:
body,
html {
margin: 0px;
padding: 0px;
height: 100%;
}
header,
footer {
background-color: #999;
text-align: center;
padding: 10px;
color: #fff;
}
header {
grid-column: 1 / 5;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 30px;
}
.container {
max-width: 1200px;
height: 100%;
margin: 0 auto;
display: grid;
/* display: inline-grid; */
grid-template-rows: 1fr;
grid-template-columns: repeat(4, 1fr);
grid-gap: 15px 20px;
grid-template-areas: "m b i t" "m s s s" "m c c c";
}
.item-1 {
background-color: aquamarine;
height: 100vh;
left: 0px;
grid-area: m;
}
.item-2 {
background-color: coral;
grid-area: b;
height: 120px;
margin-top: 10px;
}
.item-3 {
background-color: beige;
grid-area: i;
height: 120px;
margin-top: 10px;
}
.item-4 {
background-color: darkturquoise;
grid-area: t;
margin-top: 10px;
}
.item-5 {
background-color: darkgray;
grid-area: s;
}
.item-6 {
background-color: darkcyan;
grid-area: c;
padding-bottom: 50px;
}
<div>
<header class="header">this is header</header>
<div class="container">
<div class="item-1">clo 1</div>
<div class="item-2">col 2</div>
<div class="item-3">col 3</div>
<div class="item-4">col 4</div>
<div class="item-5">
<h2>this is 44</h2>
</div>
<div class="item-6">
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
</div>
</div>
<footer>this is footer</footer>
</div>
但是,我现在遇到了一个问题,我已经努力冥想了几天。我无法理解为什么,我希望每个人都可以提供帮助:
对于左侧的item1
,是固定屏幕高度和浏览器高度的固定效果,但如果我使用grid
做布局,就没有办法实现左边的固定效果,如何解决呢? (由于响应式布局,在屏幕小于特定大小后将隐藏item1
,并将显示menu
按钮)
非常感谢您解决任何疑问的任何帮助
你可以使用position: sticky
而不是fixed
。
body,
html {
margin: 0px;
padding: 0px;
height: 100%;
}
header,
footer {
background-color: #999;
text-align: center;
padding: 10px;
color: #fff;
}
header {
grid-column: 1 / 5;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 30px;
}
.container {
max-width: 1200px;
height: 100%;
margin: 0 auto;
display: grid;
/* display: inline-grid; */
grid-template-rows: 1fr;
grid-template-columns: repeat(4, 1fr);
grid-gap: 15px 20px;
grid-template-areas: "m b i t" "m s s s" "m c c c";
}
.item-1 {
background-color: aquamarine;
height: 100vh;
left: 0px;
grid-area: m;
/* added */
position: sticky;
top: 0;
}
.item-2 {
background-color: coral;
grid-area: b;
height: 120px;
margin-top: 10px;
}
.item-3 {
background-color: beige;
grid-area: i;
height: 120px;
margin-top: 10px;
}
.item-4 {
background-color: darkturquoise;
grid-area: t;
margin-top: 10px;
}
.item-5 {
background-color: darkgray;
grid-area: s;
}
.item-6 {
background-color: darkcyan;
grid-area: c;
padding-bottom: 50px;
}
<div>
<header class="header">this is header</header>
<div class="container">
<div class="item-1">clo 1</div>
<div class="item-2">col 2</div>
<div class="item-3">col 3</div>
<div class="item-4">col 4</div>
<div class="item-5">
<h2>this is 44</h2>
</div>
<div class="item-6">
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
<h1>123123</h1>
</div>
</div>
<footer>this is footer</footer>
</div>