【问题标题】:Flex Layout, aspect ratio, fixed width and dynamic width columnsFlex 布局、纵横比、固定宽度和动态宽度列
【发布时间】: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;

标签: html css flexbox


【解决方案1】:

由于整个事情是填满屏幕,我们可以使用视口单位来设置具有固定比例的元素。

然后我们可以在要占用剩余空间的元素上使用flex:l

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  height: 100vh;
  background: #c0ffee;
}

header {
  height: 54px;
  background: yellow;
}

main {
  flex: 1;
  background: blue;
}

aside {
  height: 30vh;
  background: #000;
  display: flex;
}

.left.image {
  /* 1:1 */
  
  height: 30vh;
  width: 30vh;
  background: plum;
}

.left.video {
  /* 16:9 */
  
  height: 30vh;
  width: calc(30vh *16 / 9);
  background: red;
}

.mid {
  flex: 1;
  background: green;
  display: flex;
  align-items: flex-start;
}

.asidefooter {
  height: 25px;
  background: #bada55;
  align-self: flex-end;
  width: 100%;
}

.right {
  height: 30vh;
  width: 15vh;
  background: grey;
}

footer {
  height: 15px;
  background: pink;

}
<header></header>
<main></main>
<aside>
  <div class="left video"></div>
  <div class="mid">  
    <div class="asidefooter"></div>
  </div>
  <div class="right"></div>
</aside><footer></footer>

Codepen Demo

【讨论】:

  • 哇,这似乎解决了它。我只有一个问题。我尝试添加虚拟文本以查看它对内容的反应,然后发生了以下情况:i.gyazo.com/0b2de8bd5d18473bfca796f85956a4bb.png 侧边页脚被移到一边,我得到了一个滚动条:/ 是因为 align-self 属性吗?跨度>
  • 可能是的。它又快又脏。理想情况下,我会在中间部分有另一个 div 来保存内容。
  • 另一个存放内容的 div = 旁边的页脚 div 会在里面吗? jsfiddle.net/metheria/ph9unpm4
  • Nvm,我遵循了 woestijnrog 的建议,它奏效了!
猜你喜欢
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2013-01-27
  • 2014-07-01
相关资源
最近更新 更多