position: sticky; プロパティを使って、画面をスクロールしても、アイテムの先祖ブロック要素の相対位置に配置し続けます。
デモ
See the Pen layout-7 by logsuke (@design-list) on CodePen.
コード
<div class="container">
<div class="contents bg">
</div>
<div class="right-side bg">
<div class="item"></div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
:root {
--nav-height: 8rem;
--max-width: 1024px;
--container-padding: 0 4rem;
--gap: 1rem;
--sidebar-width: 300px;
}
.container {
display: grid;
grid-template-columns: auto var(--sidebar-width);
grid-gap: var(--gap);
height: 300vh;
}
/* contents */
.contents {
width: 100%;
}
.bg {
border: 5px solid rgb(55, 53, 53);
position: relative;
opacity: 0.5;
background-image: radial-gradient(#e331dd 2px, #e5e5f7 2px);
background-size: 40px 40px;
}
/* right-sidebar */
.right-side {
width: 100%;
padding: 2rem;
}
.item {
width: 100%;
height: 220px;
background: rgba(233, 181, 255, 0.608);
position: sticky;
top: 50px;
}
.item::before {
content: 'item';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: black;
font-size: 2rem;
}
@media screen and (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
}