gpt4 book ai didi

html - 偏移 html anchor 以使用网格调整固定标题

转载 作者:行者123 更新时间:2023-11-28 00:34:55 24 4
gpt4 key购买 nike

我正在尝试使用固定 header 获取 anchor 的正确位置,但它不起作用。我尝试过 stackoverflow 和许多其他站点提供的解决方案。我无法制定任何解决方案来正常工作。

不知道是不是因为我用的是grid和viewport单位。

我的目标是了解如何仅使用 Html 和 css 使其工作,以及为什么我无法按照给出的解决方案使其工作。

可以看到html和css:

https://codepen.io/sevaro/pen/GPLeyE

    /* css*/ 
#container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 8vh 92vh 92vh 92vh;
grid-template-areas:
"m"
"a"
"w"
"c";
}
nav {
grid-area: m;
background-color: grey;
display: flex;
flex-flow: row;
justify-content: flex-end;
align-items: center;
position: -webkit-sticky;
position: sticky;
top: 0;
}
.about {
grid-area: a;
background-color: blue;
padding-right: 10px;
}
.work {
grid-area: w;
background-color: green;
display: flex;
flex-flow: row wrap;
justify-content: space-evenly;
align-items: center;
}
.contact {
grid-area: c;
background-color: yellow;
}
/* html */
<div id="container">

<nav>
<div class="ab"><a href="#whoami">About</a></div>
<div class="wo"><a href="#whativedone">Work</a></div>
<div class="co"><a href="#contactme">Contact</a></div>
</nav>


<article class="about" id="whoami">
</article>

<article class="work" id="whativedone">
<div class="project">
<img height="50px" width="50px" alt="Project 1">
<p>Project 1</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 2">
<p>Project 2</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 3">
<p>Project 3</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 4">
<p>Project 4</p>
</div>
<div class="project">
<img height="50px" width="50px" alt="Project 5">
<p>Project 5</p>
</div>
</article>

<article class="contact" id="contactme">
</article>

非常感谢任何帮助。

谢谢

我试过这些:http://nicolasgallagher.com/jump-links-and-viewport-positioning/demo/

还有这些:https://www.wikitechy.com/technology/css-offsetting-html-anchor-adjust-fixed-header/

和许多其他人。

最佳答案

尝试使用 scroll-margin-top ,它是 pretty widely adopted .

只需将以下 CSS 添加到您要滚动到的元素:

.element {
scroll-margin-top: 2em;
}

关于html - 偏移 html anchor 以使用网格调整固定标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54228567/

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