html – 具有两个相等高度子项的Flexbox布局,其中一个包含具有滚动内容的嵌套Flexbox

前端之家收集整理的这篇文章主要介绍了html – 具有两个相等高度子项的Flexbox布局,其中一个包含具有滚动内容的嵌套Flexbox前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个< img>右侧的面板包含控件标题和一长串项目.所以我有一个嵌套的flexBox情况:
  1. .container (flex)
  2. -> .child img
  3. -> .child controls panel (flex)
  4. -> .controls
  5. -> .content-wrapper scrolling list

在一个非常基本的层面上,两个并排面板很容易,是一个简单的弯曲与对齐项目:拉伸.. https://codepen.io/neekfenwick/pen/MOxYxz

在那个基础水平上,它是How do I keep two divs that are side by side the same height?的重复问题.

一旦右侧的面板与垂直滚动列表变得更加复杂,我无法看到如何使其父级的高度,第二个.child,匹配第一个.child的高度. https://codepen.io/neekfenwick/pen/XzGJGw

  1. +-------------+---------------+
  2. | | Controls |
  3. | Left child | |
  4. | +---------------+
  5. | | A|
  6. | | Vertically |+
  7. | | scrolling ||
  8. | | items ||
  9. | | |+
  10. | | V|
  11. +-------------+---------------+

我已经阅读了Flexbox item with scrollable content,但它解决了水平滚动问题,而不是像这样的垂直布局.此外,Nested flexbox with scrolling area处理垂直堆叠的嵌套FlexBox,我认为行和列方向的这种组合混淆了我的情况.

作为代码段:

  1. .container {
  2. display: flex;
  3. align-items: stretch;
  4. }
  5.  
  6. .child {
  7. border: 1px solid grey;
  8. background-color: lightgrey;
  9. flex: 1 1 auto;
  10. }
  11.  
  12. .controls-panel {
  13. display: flex;
  14. flex-direction: column;
  15. }
  16.  
  17. .controls {
  18. flex: 0 0 auto;
  19. }
  20.  
  21. .content-wrapper {
  22. flex: 1 1 auto;
  23. width: 400px;
  24. overflow-y: auto;
  25. }
  26. .content-item {
  27. display: inline-block;
  28. width: 100px;
  29. height: 100px;
  30. background-color: red;
  31. }
  1. <div class="container">
  2. <div class="child">
  3. <p>In real life I am an inline img.</p>
  4. <p>I am some content whoop de doo.</p>
  5. <p>I am some content whoop de doo.</p>
  6. <p>I am some content whoop de doo.</p>
  7. <p>I want my sibling to equal my height.</p>
  8. </div>
  9. <div class="child controls-panel">
  10. <div class="controls">
  11. <p>Small controls area. Panel below should scroll vertically.</p>
  12. </div>
  13. <div class="content-wrapper">
  14. <div class="content-item"></div>
  15. <div class="content-item"></div>
  16. <div class="content-item"></div>
  17. <div class="content-item"></div>
  18. <div class="content-item"></div>
  19. <div class="content-item"></div>
  20. <div class="content-item"></div>
  21. <div class="content-item"></div>
  22. <div class="content-item"></div>
  23. <div class="content-item"></div>
  24. <div class="content-item"></div>
  25. <div class="content-item"></div>
  26. <div class="content-item"></div>
  27. <div class="content-item"></div>
  28. <div class="content-item"></div>
  29. <div class="content-item"></div>
  30. </div>
  31. </div>
  32. </div>

我不想在这里修复任何元素的高度.碰巧,我知道img的高度,所以我可以使用CSS来强制右边元素的高度,但我想知道是否有一个’真正’的flexBox方法解决这个问题.

解决方法

通常,对于溢出:滚动(或自动和隐藏)工作,需要以一种方式或另一种方式高度约束,否则元素通常会根据需要增长以适应其内容.

主要有3种方式,其中要么设置实际高度,要么在第一个样本中,我将其添加到容器中.

堆栈代码段1

  1. .container {
  2. display: flex;
  3. height: 100vh;
  4. }
  5.  
  6. .child {
  7. border: 1px solid grey;
  8. background-color: lightgrey;
  9. flex: 1 1 auto;
  10. }
  11.  
  12. .controls-panel {
  13. display: flex;
  14. flex-direction: column;
  15. }
  16.  
  17. .controls {
  18. flex: 0 0 auto;
  19. }
  20.  
  21. .content-wrapper {
  22. flex: 1 1 auto;
  23. width: 400px;
  24. overflow-y: auto;
  25. }
  26. .content-item {
  27. display: inline-block;
  28. width: 100px;
  29. height: 100px;
  30. background-color: red;
  31. }
  1. <div class="container">
  2. <div class="child">
  3. <p>In real life I am an inline img.</p>
  4. <p>I am some content whoop de doo.</p>
  5. <p>I am some content whoop de doo.</p>
  6. <p>I am some content whoop de doo.</p>
  7. <p>I want my sibling to equal my height.</p>
  8. </div>
  9. <div class="child controls-panel">
  10. <div class="controls">
  11. <p>Small controls area. Panel below should scroll vertically.</p>
  12. </div>
  13. <div class="content-wrapper">
  14. <div class="content-item"></div>
  15. <div class="content-item"></div>
  16. <div class="content-item"></div>
  17. <div class="content-item"></div>
  18. <div class="content-item"></div>
  19. <div class="content-item"></div>
  20. <div class="content-item"></div>
  21. <div class="content-item"></div>
  22. <div class="content-item"></div>
  23. <div class="content-item"></div>
  24. <div class="content-item"></div>
  25. <div class="content-item"></div>
  26. <div class="content-item"></div>
  27. <div class="content-item"></div>
  28. <div class="content-item"></div>
  29. <div class="content-item"></div>
  30. </div>
  31. </div>
  32. </div>

或者使用绝对定位来创建绝对元素所做的高度约束.

它只需使用额外的包装器,内容滚动即可完成,并使结构的其余部分保持完全动态.

堆栈代码段2

  1. .container {
  2. display: flex;
  3. }
  4.  
  5. .child {
  6. border: 1px solid grey;
  7. background-color: lightgrey;
  8. flex: 1 1 auto;
  9. }
  10.  
  11. .controls-panel {
  12. display: flex;
  13. flex-direction: column;
  14. }
  15.  
  16. .controls {
  17. flex: 0 0 auto;
  18. }
  19.  
  20. .content-wrapper {
  21. position: relative;
  22. flex: 1 1 auto;
  23. width: 400px;
  24. }
  25. .content-scroll {
  26. position: absolute;
  27. top: 0; left: 0;
  28. right: 0; bottom: 0;
  29. overflow-y: auto;
  30. }
  31. .content-item {
  32. display: inline-block;
  33. width: 100px;
  34. height: 100px;
  35. background-color: red;
  36. }
  1. <div class="container">
  2. <div class="child">
  3. <p>In real life I am an inline img.</p>
  4. <p>I am some content whoop de doo.</p>
  5. <p>I am some content whoop de doo.</p>
  6. <p>I am some content whoop de doo.</p>
  7. <p>I want my sibling to equal my height.</p>
  8. </div>
  9. <div class="child controls-panel">
  10. <div class="controls">
  11. <p>Small controls area. Panel below should scroll vertically.</p>
  12. </div>
  13. <div class="content-wrapper">
  14. <div class="content-scroll">
  15. <div class="content-item"></div>
  16. <div class="content-item"></div>
  17. <div class="content-item"></div>
  18. <div class="content-item"></div>
  19. <div class="content-item"></div>
  20. <div class="content-item"></div>
  21. <div class="content-item"></div>
  22. <div class="content-item"></div>
  23. <div class="content-item"></div>
  24. <div class="content-item"></div>
  25. <div class="content-item"></div>
  26. <div class="content-item"></div>
  27. <div class="content-item"></div>
  28. <div class="content-item"></div>
  29. </div>
  30. </div>
  31. </div>
  32. </div>

单独使用FlexBox,认为它可能无法完全跨浏览器工作,特别是对于旧版本,仍然支持它但是很麻烦.

原始代码中的简单修复是更改flex:1 1 auto;在.content-wrapper中flex:1 1 0px; (对于我的IE版本需要0px,对于Chrome / Firefox / Edge,可以使用0)

堆栈代码段3

  1. .container {
  2. display: flex;
  3. }
  4.  
  5. .child {
  6. border: 1px solid grey;
  7. background-color: lightgrey;
  8. flex: 1 1 auto;
  9. }
  10.  
  11. .controls-panel {
  12. display: flex;
  13. flex-direction: column;
  14. }
  15.  
  16. .controls {
  17. flex: 0 0 auto;
  18. }
  19.  
  20. .content-wrapper {
  21. flex: 1 1 0px;
  22. width: 400px;
  23. overflow-y: auto;
  24. }
  25. .content-item {
  26. display: inline-block;
  27. width: 100px;
  28. height: 100px;
  29. background-color: red;
  30. }
  1. <div class="container">
  2. <div class="child">
  3. <p>In real life I am an inline img.</p>
  4. <p>I am some content whoop de doo.</p>
  5. <p>I am some content whoop de doo.</p>
  6. <p>I am some content whoop de doo.</p>
  7. <p>I want my sibling to equal my height.</p>
  8. </div>
  9. <div class="child controls-panel">
  10. <div class="controls">
  11. <p>Small controls area. Panel below should scroll vertically.</p>
  12. </div>
  13. <div class="content-wrapper">
  14. <div class="content-item"></div>
  15. <div class="content-item"></div>
  16. <div class="content-item"></div>
  17. <div class="content-item"></div>
  18. <div class="content-item"></div>
  19. <div class="content-item"></div>
  20. <div class="content-item"></div>
  21. <div class="content-item"></div>
  22. <div class="content-item"></div>
  23. <div class="content-item"></div>
  24. <div class="content-item"></div>
  25. <div class="content-item"></div>
  26. <div class="content-item"></div>
  27. <div class="content-item"></div>
  28. </div>
  29. </div>
  30. </div>

代码段2和3也回答了您的问题

…with a vertically scrolling list,I cannot see how to make the
height of its parent,the second .child,match the height of the
first .child.

注意,如果右列中的顶部元素可以比左列更大,则底部元素上需要最小高度,以防止它崩溃为0,例如,

  1. .content-wrapper {
  2. ...
  3. min-height: 200px; /* e.g. like this */
  4. }

猜你在找的HTML相关文章