gpt4 book ai didi

css - 网格垂直滚动

转载 作者:行者123 更新时间:2023-11-27 23:46:32 25 4
gpt4 key购买 nike

我有一个带有嵌套左右网格的网格。我希望我的左侧网格占据浏览器的整个高度并固定在适当的位置。我希望我的右侧网格在向其添加内容时获得垂直滚动条。

body{ margin: 0 0; padding: 0 0 ; }

.grid {
display: grid;
grid-template-columns: 25% 75%;
grid-gap: 10px;
grid-auto-rows: 500px;
}

.left{
display: grid;
grid-template-rows : repeat(3,1fr);
grid-gap : 5px;
grid-auto-rows: 500px;
}

.one{ background: violet; }

.two{ background: indigo; }

.three { background: blue; }

.right{
display: grid;
grid-template-rows: repeat(4,1fr);
grid-gap : 5px;
}

.four{ background: green; }

.five{ background: yellow; }

.six { background: orange; }

.seven{ background: red}
<body>
<div class="grid">
<div class="left">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
<div class="right">
<div class="four">4</div>
<div class="five">5</div>
<div class="six">6</div>
<div class="seven">7</div>
</div>
</div>
</body>

最佳答案

如果您有 grid-auto-rows: 500px,您打算如何让左侧网格占据整个高度并保持固定?在许多情况下,这会溢出容器。

这是您的代码的修订版,具有固定的左侧网格和 grid-auto-rows: 500px 以及右侧的 overflow: auto网格。

.grid {
display: grid;
grid-template-columns: 1fr 3fr; /* switched from percentages for spacing efficiency */
grid-gap: 10px;
/* grid-auto-rows: 500px; */
height: 100vh; /* new */
}

.left {
display: grid;
grid-template-rows: repeat(3, 1fr);
grid-gap: 5px;
/* grid-auto-rows: 500px; */
}

.right {
display: grid;
/* grid-template-rows: repeat(4, 1fr); */
grid-gap: 5px;
grid-auto-rows: 500px; /* new */
overflow: auto; /* new */
}

.one { background: violet; }
.two { background: indigo; }
.three { background: blue; }
.four { background: green; }
.five { background: yellow; }
.six { background: orange; }
.seven { background: red }
body { margin: 0 0; padding: 0 0; }
<div class="grid">
<div class="left">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
<div class="right">
<div class="four">4</div>
<div class="five">5</div>
<div class="six">6</div>
<div class="seven">7</div>
</div>
</div>

关于css - 网格垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773339/

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