【发布时间】:2015-11-01 03:52:01
【问题描述】:
我对页面布局有非常具体的要求,但我似乎无法做到 100% 正确。
我的布局是这样的,我在每个部分都添加了数字以便于解释:
我目前的尝试: https://jsfiddle.net/metheria/sxxbqtnb/71/embedded/result/
我最初的尝试是填充一个虚拟 div 以创建纵横比,但这在 div 下留下了很多空白空间。我需要这个布局不超过 100% 的宽度和高度,目前正在发生这种情况。
我目前的尝试使用 flex 布局,但我什至无法在其中加入 5,因为我什至看不到 3、4 和 6 中的文本。我知道这很可能是由“padding-bottom: 18%”,但没有那个填充,我什至看不到 div,更不用说可能保持纵横比尝试......
有没有将这些纵横比要求和完整布局结合起来?
另外,在 (1) 中,我想分成 4 个部分: - 一个图像 - 中间的空间来分隔图像和两个按钮 - 两个左对齐的按钮
这就引出了我的最后一个问题。我需要所有的 div 都是“display: flex;”吗?使这种布局工作还是导航栏不能灵活?还有地图。
html代码:
<div id="navbar">navbar</div>
<div id="map">mapa</div>
<div id="infos">
<div class="test">
<div class="test-wrap">
<div id="multimedia"></div>
<div id="ambassador"></div>
<div id="next"></div>
</div>
</div>
<div id="footer"> text </div>
</div>
还有css代码:
body, html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
*, *:before, *:after {
/* Chrome 9-, Safari 5-, iOS 4.2-, Android 3-, Blackberry 7- */
-webkit-box-sizing: border-box;
/* Firefox (desktop or Android) 28- */
-moz-box-sizing: border-box;
/* Firefox 29+, IE 8+, Chrome 10+, Safari 5.1+, Opera 9.5+, iOS 5+, Opera Mini Anything, Blackberry 10+, Android 4+ */
box-sizing: border-box;
}
#navbar {
display: flex;
background-color: pink;
height: 54px;
flex: 0 0 54px;
width: 100%;
}
#map {
width: 100%;
height: 70%;
background-color: blue;
}
#infos {
height: 30%;
}
.test {
position: relative;
height: auto;
background-color: transparent;
display: flex;
}
.test-wrap {
width: 100%;
padding: 0;
margin: 0;
font-size: 0;
display: flex;
/*justify-content: space-between;*/
flex-wrap: wrap;
}
#next {
position: relative;
height: 100%;
width: 15%;
display: inline-flex;
padding-bottom: 18%;
background-color: #666;
}
#multimedia {
position: relative;
height: 100%;
width: 25%;
display: inline-flex;
padding-bottom: 18%;
background-color: #111;
}
#ambassador {
position: relative;
height: 100%;
width: 60%;
display: inline-flex;
padding-bottom: 18%;
background-color: #b92b2e;
}
#footer {
display: inline-block;
position: relative;
height: 15px;
flex: 0 0 15px;
width: 100%;
background-color: tomato;
}
【问题讨论】:
-
1) 你说你已经为你的部分编号,但我在你链接的图像中看不到数字。 2) 你的布局需要嵌套的弹性框。 3) 你只需要在 flexbox 上使用
display: flex,而不是在 flex-items(它是孩子)上。弹性项目可以是固定高度,就像您希望导航栏一样。 -
@woestijnrog 我的错,我更新了图片,现在应该有数字了。因此,我会将包含我当前所有 div 的 div 设置为“display: flex;”或者我可以将它添加到正文中吗?我还更新了 CSS,因为出于某种原因它不是我的最终版本。我已经将导航栏设置为固定高度,但我不明白为什么总 div 的宽度超过 100%。
-
您可以在正文上使用
display: flex;。你的尺寸会崩溃,因为你需要至少一个弹性项目来增长/缩小到剩下的东西。但是你的#map 总是 70%。 -
@woestijnrog 我究竟如何使地图增长/缩小到剩下的部分?
-
你需要分配你的弹性盒子(body)的高度。导航栏的高度是固定的,infos 占 30%,而 #map 应该会增长以填满其余部分。所以在你的#map 上使用
flex: 1;。