Section 1
Content goes here.
Section 2
Content goes here.
Section 3
Content goes here.
在固定的内容(通常是顶部导航)上添加额外的高度,并在锚链接目标部分的CSS中使用伪类“::before”来添加相应的负边距。这将抵消固定内容的高度,并使目标部分出现在正确的位置。
HTML代码示例:
Section 1
Content goes here.
Section 2
Content goes here.
Section 3
Content goes here.
CSS代码示例:
.sticky-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px; /* Add extra height to account for the sticky header */
}
section::before {
display: block;
content: "";
margin-top: -50px; /* Add negative margin to account for the sticky header height */
height: 50px; /* Same as the extra height added to the sticky header */
visibility: hidden;
}