gpt4 book ai didi

html - 当我有容器 div 时如何创建粘性页脚?

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:04 25 4
gpt4 key购买 nike

我有一个有效的粘性页脚,其页面结构如下:

<body>
<nav></nav>
<main-content></main-content>
<footer>
</body>

由于我使用的 JavaScript 库的限制,我不得不像这样重组我的页面:

<body>
<div class="app-root">
<nav></nav>
<main-content></main-content>
<footer>
</div>
</body>

用新的父元素实现同样的粘性页脚效果最简单的方法是什么?我也可以使用不同的父元素,例如内联元素,但这似乎是一种 hack,并没有给我需要的结果。

这没有用:

html, body, .app-root{
height: 100vh;
}

这是我追求的“粘性”行为:

enter image description here

最佳答案

Flexbox 是你的 friend 。我强烈建议学习它。

.app-root {

// You wouldnt actually need the height listed like this since your
// elems would have actual content.
height: 1200px;
width: 100%;
background-color: pink;

// Use flexbox for rendering content.
display: flex;

// This is telling the content to render vertically rather than
// the native horizontal rendering.
flex-direction: column;
}

main-content {

// Tells this element to take up as much space as it can within
// its parent.
flex: auto;
}

nav {
height: 50px;
width: 100%;
background-color: green;
}

footer {
height: 50px;
width: 100%;
background-color: lightblue;
}

https://jsfiddle.net/ezova0ty/

关于html - 当我有容器 div 时如何创建粘性页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42450023/

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