扩展父母元素与flex的儿童元素

Erd*_*ss4 8 html css css3 flexbox

我已经使用CSS flex来并排显示两个div,它们包含在一个包装内,我一直在努力让内部#myClippetWrapper是我设置高度的地方,所以在#myClippetWrapper我可以设置的子元素中height: 100%;.

但是正如你可以看到从#myClippetWrapper主要部分之外的所有内部元素运行下面的代码片段,它们都悬挂在主要内容div之外?

我不想使用overflow: auto因为我不想在那里使用滚动条,我只需要子元素#myClippetWrapper不在主要部分/ div之外.

main {
  margin: 0 auto;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 8px;
  background-color: red;
  width: 100%;
  max-width: 50%;
  height: auto;
}

#myClippetWrapper {
  display: flex;
  height: 700px;
}

#clippetNav {
  padding: 10px;
  width: 250px;
  height: 100%;
  background-color: #222222;
  margin-right: 10px;
}

#codeAndNotesWrapper {
  display: flex;
  width: 100%;
}

#codeAndNotesWrapper>div {
  flex-basis: 100%;
  height: 100%;
}

#codeView {
  padding: 10px;
  /*flex: 0 0 40%;*/
  height: 100%;
  background-color: #222222;
  margin-right: 10px;
}

#noteView {
  padding: 10px;
  /*flex: 1;*/
  height: 100%;
  background-color: #222222;
}

#codeNotesEditor {
  height: 100%;
  background-color: #EAEAEA;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <div id="myClippetWrapper">

    <div id="clippetNav">

    </div>

    <div id="codeAndNotesWrapper">

      <div id="codeView">

      </div>

      <div id="noteView">

        <div id="codeNotesEditor">

        </div>

      </div>

    </div>

  </div>
</main>
Run Code Online (Sandbox Code Playgroud)

Mic*_*l_B 5

在许多情况下,flexbox消除了使用百分比高度的需要。

flex容器的初始设置为align-items: stretch。这意味着在flex-direction: row(如您的代码中)flex项将自动扩展容器的整个高度。

或者,您可以使用flex-direction: column然后应用于flex: 1子项,这也可以使flex项扩展父项的整个高度。

main {
  max-width: 50%;
  margin: 10px auto;
  padding: 8px;
  background-color: red;
}

#myClippetWrapper {
  display: flex;
  height: 700px;
}

#clippetNav {
  display: flex;
  padding: 10px;
  width: 250px;
  margin-right: 10px;
  background-color: #222222;
}

#codeAndNotesWrapper {
  display: flex;
  width: 100%;
}

#codeAndNotesWrapper>div {
  display: flex;
  flex-basis: 100%;
}

#codeView {
  display: flex;
  padding: 10px;
  margin-right: 10px;
  background-color: #222222;
}

#noteView {
  display: flex;
  flex-direction: column;
  padding: 10px;
  background-color: #222222;
}

#codeNotesEditor {
  flex: 1;
  background-color: #EAEAEA;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <div id="myClippetWrapper">
    <div id="clippetNav"></div>
    <div id="codeAndNotesWrapper">
      <div id="codeView"></div>
      <div id="noteView">
        <div id="codeNotesEditor"></div>
      </div>
    </div>
  </div>
</main>
Run Code Online (Sandbox Code Playgroud)

jsFiddle