偏移HTML锚以使用网格调整固定标头

时间:2019-01-17 03:24:23

标签: html css anchor

我正在尝试使用修复标头获得锚的正确位置,但是它不起作用。我已经尝试了stackoverflow和许多其他站点提供的解决方案。我无法提供任何解决方案以使其正常工作。

我不知道原因是否是我使用网格和视口单位。

我的目标是了解如何仅使用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/

和许多其他人。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

这对我有用:

CSS

h2{
    position:relative;
}
h2 > a{
    position:absolute;
    top: -12.5vh;
}

HTML

<nav>
    <div><a href="#whoami">About</a></div> <!––Link-->
</nav>

<article>
     <h2><a id="whoami"></a>ABOUT</h2> <!––Anchor-->
</article>

希望这对以后的人有帮助。

谢谢

相关问题