我的布局基于css-grids。我通过使用
设法将标题保留在顶部 position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 10;
但是,如果我尝试对任何侧边栏使用相同的代码(但顶部为200px,则为实例)将无法正常工作。
如果您觉得更简单(https://codepen.io/fergos2/pen/MWgLqgL?editors=1100),这是我的代码笔中的代码。但是,我也将其包括在下面,但不带油脂。
我不想使用position:fixed破坏网格布局。还有其他更简单的方法同时保留网格吗?...在此先感谢您帮助这个新手!
<div class="container">
<header class="header pd">Header</header>
<div class="left-sidebar pd">
<div class="box-1 pd">
Box-1
</div>
<footer class="footer pd">
Footer
</footer>
</div>
<div class="main-content pd">
Main content
</div>
<div class="right-sidebar pd">
<div class="box-2 pd">
Box-2
</div>
<div class="box-3 pd">
Box-3
</div>
</div>
</div>
.container {
width: 100%;
margin: 0 auto;
background-color: #eee;
display: grid;
grid-template-rows: min-content 1fr;
grid-template-columns: 1fr 2fr 1fr;
grid-gap: 15px;
grid-template-areas: "head head head"
"leftbar main rightbar";
& > * {
background-color: pink;
color: #ggg;
font-size: 20px;
font-family: sans-serif;
}
}
.pd {
padding: 15px;
}
.header {
grid-area: head;
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 10;
}
.left-sidebar {
grid-area: leftbar;
position: -webkit-sticky;
position: sticky;
top: 200px;
display: flex;
flex-direction: column;
justify-content: flex-start;
.box-1 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.footer {
color: green;
border: 1px solid purple;
}
}
.main-content {
grid-area: main;
}
.right-sidebar {
grid-area: rightbar;
display: flex;
flex-direction: column;
justify-content: flex-start;
.box-2 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.box-3 {
color: green;
border: 1px solid purple;
}
}
答案 0 :(得分:1)
如果我尝试对任何侧边栏使用相同的代码(但顶部为200px,则为实例)
不要将此属性应用于具有类.left-sidebar
的块,而是应用于其内部的块。您可以为此创建一个附加元素。例如,.left-inner
元素。
.container {
width: 100%;
margin: 0 auto;
background-color: #eee;
display: grid;
grid-template-rows: min-content 1fr;
grid-template-columns: 1fr 2fr 1fr;
grid-gap: 15px;
grid-template-areas: "head head head" "leftbar main rightbar";
}
.container>* {
background-color: pink;
color: #ggg;
font-size: 20px;
font-family: sans-serif;
}
.pd {
padding: 15px;
}
.header {
grid-area: head;
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 10;
}
.left-sidebar {
grid-area: leftbar;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.left-sidebar .left-inner {
position: -webkit-sticky;
position: sticky;
top: 200px;
}
.left-sidebar .box-1 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.left-sidebar .footer {
color: green;
border: 1px solid purple;
}
.main-content {
grid-area: main;
}
.right-sidebar {
grid-area: rightbar;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.right-sidebar .box-2 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.right-sidebar .box-3 {
color: green;
border: 1px solid purple;
}
<div class="container">
<header class="header pd">Header</header>
<div class="left-sidebar pd">
<div class="left-inner">
<div class="box-1 pd">
Box-1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<footer class="footer pd">
Footer
</footer>
</div>
</div>
<div class="main-content pd">
Main content Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius
modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit
qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi
consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? Ut enim ad minima
veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat
quo voluptas nulla pariatur? Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae
consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</div>
<div class="right-sidebar pd">
<div class="box-2 pd">
Box-2
</div>
<div class="box-3 pd">
Box-3 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
还有CodePen
上的相同代码