gpt4 book ai didi

css - 使用网格布局实现浏览器左侧固定位置疑惑?

转载 作者:行者123 更新时间:2023-11-28 02:04:56 24 4
gpt4 key购买 nike

最近在研究网格布局,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>

关于css - 使用网格布局实现浏览器左侧固定位置疑惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com