gpt4 book ai didi

html - 如何将固定位置的 flex 容器覆盖在另一个 flex 容器上?

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

这可能已经在堆栈溢出时得到了解答,但我查看并遵循了找到的答案,但它不起作用我想在页面底部的 header header2 strong>(我想要 100% 高度和 100% 宽度)。如果我为 header 设置这些值,则 Mainheader 大于页面(需要滚动)。以下是代码:

.header {
border: 3px solid orange;
min-height: 100vh;
display: -webkit-flex;
display: flex;
/* -webkit-box-orient: horizontal; */
z-index: 0;
position: relative;
/* position: absolute; */
}
.headerMiddle {
border: 2px solid green;
-webkit-flex: 1;
flex: 1;
}
.header2 {
border: 3px solid blue;
min-height: 10vh;
display: -webkit-flex;
display: flex;
/* -webkit-box-orient: horizontal; */
}
.headerMiddle2 {
border: 2px solid green;
-webkit-flex: 1;
flex: 1;
}
.Mainheader {
border: 3px solid red;
min-height: 100vh;
display: -webkit-flex;
position: relative;
flex-direction: column;
display: flex;
}
<body>
<div class="Mainheader">
<div class="header">
<div class="headerMiddle"></div>
</div>
<div class="header2">
<div class="headerMiddle2">Option 1</div>
<div class="headerMiddle2">Option 2</div>
</div>
</div>


</body>

也是 JSbin 上的代码片段 here .

最佳答案

所以这是我在您的代码中更改的要点:

  1. margin: 0 添加到 body 以移除默认边距

  2. box-sizing: border-box 到所有元素,以便高度和宽度不超过窗口(您可能需要考虑使用 calc 如果你想显示所有边框)

  3. 已将 fixed 位置添加到您的 headerheader2 以根据要求定位它们。

下面的片段:

body{
margin: 0;
}
*{
box-sizing: border-box;
}
.header {
border: 3px solid orange;
min-height: 100vh;
display: -webkit-flex;
display: flex;
/* -webkit-box-orient: horizontal; */
z-index: 0;
position: fixed;
width: 100%;
height: 100%;
/* position: absolute; */
}


.headerMiddle {
border: 2px solid green;
-webkit-flex: 1;
flex: 1;
}

.header2 {
border: 3px solid blue;
min-height: 10vh;
display: -webkit-flex;
display: flex;
position: fixed;
bottom: 0;
width: 100%;
/* -webkit-box-orient: horizontal; */
}
.headerMiddle2 {
border: 2px solid green;
-webkit-flex: 1;
flex: 1;
}
.Mainheader {
border: 3px solid red;
min-height: 100vh;
display: -webkit-flex;
position:relative;
flex-direction:column;
display: flex;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="Mainheader">
<div class="header">
<div class="headerMiddle"></div>
</div>
<div class="header2">
<div class="headerMiddle2">Option 1</div>
<div class="headerMiddle2">Option 2</div>
</div>
</div>


</body>
</html>

让我知道您对此的反馈。谢谢!

关于html - 如何将固定位置的 flex 容器覆盖在另一个 flex 容器上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421972/

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