gpt4 book ai didi

html - 如何相对于底部布局 HTML 元素?

转载 作者:行者123 更新时间:2023-11-28 02:50:10 24 4
gpt4 key购买 nike

我正在尝试在 react 中布局 3 个元素(没有 jQuery 和 Bootstrap )。看图:

Page Layout

本质上,应用程序有一个容器 div,它占据 Body 的 100% 高度,该高度设置为窗口高度。它具有三个子元素:

  • 标题:相对于容器顶部定位
  • 页脚:相对于容器底部定位;可能有可变高度
  • 内容:相对于页脚顶部定位

将容器和页眉放置到位很容易,但页脚和内容是困难的部分。我的代码看起来像这样:

CSS:

html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}

.container {
height: 100%;
}

#footer: {
position: fixed,
right: 0,
bottom: 0,
left: 0,
}

我正在尝试了解实现它的最佳方法。你能建议我应该如何编写 CSS 吗?

最佳答案

HTML:

<body>
<div class="container">
<header>
</header>
<div class="content">
</div>
<footer>
</footer>
</div>
</body>

CSS:

body {
margin: 0;
padding: 0;
background-color: white;
}

.container {
width: 100vw;
height: 100vh;
background-color: black;
display: flex;
flex-direction: column;
}

header {
flex: 0 0 120px;
background-color: green;
}

.content {
flex: 1 1;
background-color: blue;
overflow: hidden;
}

footer {
flex: 0 0 10%;
background-color: red;
}

代码笔:https://codepen.io/anon/pen/WZKwEb

说明:布局使用CSS3 flexbox。所有元素都有不同的背景颜色,以提供比例和高度的视觉反馈。

顶级容器 div 是 flexbox 容器。它的高度和宽度分别设置为 100vh(100% 视口(viewport)高度)和 100vw(100% 视口(viewport)宽度)。

页眉和页脚被设置为特定的高度(页眉以像素为单位,页脚以百分比为单位,为了演示两者),而内容 div 被设置为占据剩余空间,并相应地增长或缩小(flex: 1 1).

如果页眉或页脚的内容超出了它们的特定高度,它们的高度将相应调整,代价是内容 div。 content div 的 overflow 设置为 hidden,这意味着它的内容既不会溢出到页眉或页脚,也不会导致出现滚动条,而只会 overflow hidden 的内容。

关于html - 如何相对于底部布局 HTML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701509/

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