【问题标题】:how to grow columns to be the same height of content of either column using css flexbox?如何使用css flexbox将列增长到与任一列的内容高度相同?
【发布时间】:2022-02-06 05:51:24
【问题描述】:

演示:https://jsfiddle.net/chovy/7vgLpm9h/7/

我很难让导航与内容一样高(或者如果您删除内容与导航一样高的内容)。

两者都应该是最高柱高度的 100%。

我还需要将最后一个导航项放在导航栏的底部。

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

html,
body,
main {
  height: 100%;
  min-height: 100vh;
  width: 100vw;
}

main {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
}

aside {
  width: 100rem;
  border: 1px solid red;
  align-self: stretch;
  flex-grow: 1;
}

section {
  flex-grow: 1;
  border: 1px solid green;
}

nav li {
  margin-bottom: 1rem;
}

nav li:last-child {
  margin-top: auto;
  margin-bottom: 0;
  color: blue;
  font-weight: 700;
}
<main>
  <aside>
    <header>header</header>
    <nav>
      <ul>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
      </ul>
    </nav>
  </aside>
  <section>
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column.
  </section>
</main>

【问题讨论】:

  • 删除:对齐项目:弹性开始; ?和所有的高度:100%?
  • 似乎什么也没做。实际上更糟 - 如果你有解决方案,请分叉小提琴。
  • 这行得通,但是如何让最后一个导航项位于框的底部?

标签: css flexbox


【解决方案1】:

简化您的代码,如下所示。您必须删除一些不必要的属性

body {
  margin: 0;
}
main {
  min-height: 100vh;
  display: flex;
}
aside {
  width:100px;
  border: 1px solid red;
  display: flex;
  flex-direction: column;
}
nav {
  flex-grow: 1;
  display: flex;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
section {
  flex: 1;
  border: 1px solid green;
}
nav li:not(:last-child) {
  margin-bottom: 1rem;
}
nav li:last-child {
  margin-top: auto;
  color: blue;
  font-weight: 700;
}
<main>
  <aside>
    <header>header</header>
    <nav>
      <ul>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
        <li>link 1</li>
      </ul>
    </nav>
  </aside>
  <section>
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column. grow this to 100% of either column.
    grow this to 100% of either column.
  </section>
</main>

【讨论】:

  • 没有小提琴我无法测试这个。
  • asidsection 的高度如何?
猜你喜欢
  • 2018-07-05
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 2016-03-13
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多