gpt4 book ai didi

css - 如何使用 CSS-grid 创建粘性标题?

转载 作者:行者123 更新时间:2023-11-27 23:34:18 27 4
gpt4 key购买 nike

我想在使用 CSS 网格滚动时使我的标题具有粘性。

我已经尝试过这里提出的解决方案:Sticky header on css grid含义:

position: sticky; 
top:0;

然而它不起作用......

.wrapper {
display: grid;
grid-template-areas: "header" "middle" "footer";
grid-template-rows: auto 1fr auto;
height: 100vh;
}


/* Header */

header {
order: 1;
grid-area: header;
display: grid;
grid-gap: 100px;
grid-template-areas: "logo nav";
grid-template-columns: auto 1fr;
grid-auto-flow: column;
position: sticky;
top: 0;
}

nav {
display: grid;
grid-area: nav;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}


/* Middle */

.middle {
order: 2;
grid-area: middle;
display: grid;
grid-template-columns: 50px 1fr 50px;
}

.middle>* {
grid-column: 2 / -2;
}


/* Footer */

footer {
order: 3;
grid-area: footer;
display: grid;
grid-template-columns: 1fr auto 1fr;
}

.footer-links {
display: grid;
grid-column: 2 /-2;
grid-template-columns: 1fr;
grid-auto-flow: column;
align-items: center;
}
<body>
<div class="wrapper">

<!-- Header -->
<header>
<a href="./index.html" title="Welcome" class="logo"><img src="img/logo_jaeaess_glitch.png" alt="Logo of the VJ Jääß (Jess de Jesus)" style="width:42px;height:42px"></a>
<nav>
<a href="./index.html" title="Welcome" class="welcome active">Welcome</a>
<a href="./about.html" title="About" class="about">About</a>
<a href="./artwork.html" title="Art Work" class="artwork">Art Work</a>
<a href="./events.html" title="Events" class="events">Events</a>
</nav>
</header>
<!-- Middle -->
<section class="middle">
</section>

<footer>
<div class="footer-links">
<a href="https://www.instagram.com/jaeaess/" target="_blank">Instagram</a>
<p>&copy; 2019 Jääß</p>
</div>
</footer>
</div>

</body>

一切都按照我的意愿显示,除了标题向下滚动而不是保持固定...

(对于那些想知道的人,我下令只是在开发的后期阶段将其移动到媒体查询中)

感谢您的回答!

最佳答案

要使标题在滚动时固定,您可以将其设置为position: fixed。这需要您为页眉设置固定高度。这将使 header 元素在内容之上流动,并忽略相对于窗口的滚动。

  
.wrapper {
/* the header will not take any vertical place so shift wrapper down a bit*/
margin-top: 3rem;
display: grid;
/*I removed the header area as we don't need it anymore and would not work with fixed position anways*/
grid-template-areas: "middle" "footer";
grid-template-rows: 1fr auto;
/*I set the wrapper height to `200vw` so its easier to see the header not scrolling. Also take maring top into account*/
height: calc(200vh - 3rem);
}


/* Header */

header {
display: grid;
grid-gap: 100px;
grid-template-areas: "logo nav";
grid-template-columns: auto 1fr;
grid-auto-flow: column;
position: fixed;
top: 0;
height: 3rem;
width: 100%;
}

nav {
display: grid;
grid-area: nav;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}


/* Middle */

.middle {
order: 2;
grid-area: middle;
display: grid;
grid-template-columns: 50px 1fr 50px;
}

.middle>* {
grid-column: 2 / -2;
}


/* Footer */

footer {
order: 3;
grid-area: footer;
display: grid;
grid-template-columns: 1fr auto 1fr;
}

.footer-links {
display: grid;
grid-column: 2 /-2;
grid-template-columns: 1fr;
grid-auto-flow: column;
align-items: center;
}
<div class="wrapper">
<!-- Header -->
<header>
<a href="./index.html" title="Welcome" class="logo"><img src="img/logo_jaeaess_glitch.png" alt="Logo of the VJ Jääß (Jess de Jesus)" style="width:42px;height:42px" /></a>
<nav>
<a href="./index.html" title="Welcome" class="welcome active">Welcome</a
>
<a href="./about.html" title="About" class="about">About</a>
<a href="./artwork.html" title="Art Work" class="artwork">Art Work</a>
<a href="./events.html" title="Events" class="events">Events</a>
</nav>
</header>
<!-- Middle -->
<section class="middle"></section>

<footer>
<div class="footer-links">
<a href="https://www.instagram.com/jaeaess/" target="_blank">Instagram</a
>
<p>&copy; 2019 Jääß</p>
</div>
</footer>
</div>

Ps.: 你现在有点过度使用 css 网格了。它专为二维布局而设计。如果您使用 flexbox,您的布局会容易得多(!)。在 IE 11 中使网格工作也是一个 pain .

关于css - 如何使用 CSS-grid 创建粘性标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57325337/

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