html - 为什么 justify-content space-between 什么都不做?

标签 html css flexbox

我试图通过使用 justify-content: space-between 使 top-navbot-nav 部分垂直分隔>。但是,它什么也没做。有人可以指出我做错了什么吗?

@import url(https://fonts.googleapis.com/css?family=Rock+Salt);
 html {
  font-family: 'Rock Salt', cursive;
}
.flex-container {
  display: flex;
}
header {
  width: 300px;
  height: 100vh;
  position: fixed;
  background-color: blue;
}
nav.flex-container {
  align-items: center;
  flex-direction: column;
  justify-content: space-between;
}
ul {
  padding: 0;
  margin: 0;
  list-style-type: none;
}
#logo-container {
  background-color: red;
  width: 100%;
}
#logo {
  margin: auto;
  padding: 10px 0;
}
<body>
  <div id="root-container">
    <header>
      <div id="logo-container" class="flex-container">
        <h2 id="logo">Name Goes Here</h2>
      </div>
      <nav class="flex-container">
        <div class="top-nav">
          <ul>
            <li>This is a list item</li>
            <li>This is a list item</li>
          </ul>
        </div>
        <div class="bot-nav">
          <ul>
            <li>This is a list item</li>
            <li>This is a list item</li>
          </ul>
        </div>
      </nav>
    </header>
  </div>
</body>

https://codepen.io/anon/pen/rxMPMN

最佳答案

block 元素的高度默认为 block 内容的高度,除非您定义了特定的高度。 flexbox justify-content 定义了浏览器如何在 flex 元素之间和周围分配空间。所以默认情况下它不会对 flex-direction:column 产生任何影响。

在您的示例中,nav.flex-container 没有定义任何高度,您可以设置百分比 n%(因为您已经设置了 100vh header 上,父元素)或 vh 值。

nav.flex-container {
    height: 80%; /*your value*/
}

更新笔 - https://codepen.io/anon/pen/LGRqzy

或者,也将 header 设置为 display:flex,并在 nav.flex 上添加 flex:1 (grow) -容器

header {
    display: flex;
    flex-direction: column;
}
nav.flex-container {
    flex: 1;
}

更新笔 - https://codepen.io/anon/pen/WrGPMW

关于html - 为什么 justify-content space-between 什么都不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482262/

相关文章:

javascript - Codemirror 和 iFrame 问题

html - 如何删除这两个 DIV 之间不需要的空间?

css - Bootstrap 警报从左打开,向右关闭

jQuery mouseover 和 mousleave 导致闪烁

html - 当我链接完整的 div 时,我的 Flexbox 不断缩小

html - Flexbox 不会垂直对齐内容

php - 发生自动换行时通过 JavaScript 更改 DIV 的高度

javascript - 在 div 单击时切换动态(或 'auto')高度

html - "top"CSS 属性在使用百分比时无效

html - 使用 flex 扩展到父元素外部的子元素