簡體   English   中英

我們如何使用 css 使側邊欄變粘?

[英]How do we make the sidebar sticky using css?

Sitcky 側邊欄

我需要使 cre_asideCol 列具有粘性。 但是我遇到了一些問題,並且無法創建此側邊欄粘性。

使用 css 你應該寫到側邊欄:

position: sticky;
top: 0;

您可能需要添加額外的標簽包裝器,以使粘性定位起作用

 .entry-content { width: 400px; height: 400px; background-color: aqua; position: relative; } .sidebar { position: sticky; background-color: red; width: 375px; height: 100%; left: 0; } .menu { position: absolute; right: 0; } .cre-asideCol { position: absolute; left: 0px; background-color: orange; width: 300px; height: 100%; overflow: auto; }
 <div class="entry-content"> <div class="sidebar"> <div class="menu"> <nav>menu 1</nav> <nav>menu 2</nav> <nav>menu 3</nav> <nav>menu 4</nav> <nav>menu 5</nav> </div> <div class="cre-asideCol"> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </p> </div> </div> </div>

我會設置position: sticky 但是你必須注意頂部的屬性,你設置了什么,因為那意味着側邊欄將從那個點開始粘住。 因此,如果您有position:sticky, top:20px,它會在滾動超過 20px 后粘住。

現在問題解決了

 overflow: inherit; align-items: flex-start;

我們需要將這些 CSS 屬性添加到側邊欄的父元素中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM