html – 当视口调整大小时,如何强制嵌套的flexbox元素缩小并显示滚动条?

前端之家收集整理的这篇文章主要介绍了html – 当视口调整大小时,如何强制嵌套的flexbox元素缩小并显示滚动条?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用flexBox构建布局,整体而言相当简单.视图有三个部分:顶部的导航栏,左侧的侧边栏和填充剩余空间的内容区域.导航栏具有固定高度,侧栏具有固定宽度.

此外,侧边栏内容区域应单独滚动.如果侧边栏中的内容溢出,则应创建特定于侧边栏的滚动条.内容视图也是如此.重要的是,这意味着整个视口必须永远不会滚动:它应该保持静态(只有元素应该滚动).

使用flexBox构建此布局非常简单:

  1. html,body {
  2. height: 100%;
  3. width: 100%;
  4. }
  5.  
  6. body {
  7. margin: 0;
  8. }
  9.  
  10. #root {
  11. display: flex;
  12. height: 100%;
  13. }
  14.  
  15. #frame {
  16. display: flex;
  17. flex: 1;
  18. flex-direction: column;
  19. }
  20.  
  21. #navigation-bar {
  22. background-color: #bab;
  23. display: flex;
  24. height: 70px;
  25. overflow: hidden;
  26. }
  27.  
  28. #main {
  29. display: flex;
  30. flex: 1;
  31. }
  32.  
  33. #left-bar {
  34. background-color: #aaa;
  35. overflow: auto;
  36. width: 250px;
  37. }
  38.  
  39. #content {
  40. background-color: #ccc;
  41. flex: 1;
  42. }
  1. <div id="root">
  2. <div id="frame">
  3. <div id="navigation-bar">
  4. <h1>Website Name</h1>
  5. </div>
  6. <div id="main">
  7. <div id="left-bar">
  8. <h1>Some content</h1>
  9. <h1>Some content</h1>
  10. <h1>Some content</h1>
  11. <h1>Some content</h1>
  12. <h1>Some content</h1>
  13. <h1>Some content</h1>
  14. <h1>Some content</h1>
  15. <h1>Some content</h1>
  16. <h1>Some content</h1>
  17. </div>
  18. <div id="content">
  19. </div>
  20. </div>
  21. </div>
  22. </div>

但是,请注意侧边栏不会单独滚动.而是整个视口扩展和滚动.有趣的是,我想要实现的是没有嵌套的正常工作 – 如果我删除导航栏,侧边栏会独立滚动.

如何阻止flexBox本身拉伸以包含其内容,以便显示特定于元素的滚动条,而不是视口的滚动条?

解决方法

添加这个:
  1. #main {
  2. min-height: 0;
  3. flex: 1 1 0;
  4. }
  1. html,body {
  2. height: 100%;
  3. width: 100%;
  4. }
  5. body {
  6. margin: 0;
  7. }
  8. #root {
  9. display: flex;
  10. height: 100%;
  11. }
  12. #frame {
  13. display: flex;
  14. flex: 1;
  15. flex-direction: column;
  16. }
  17. #navigation-bar {
  18. background-color: #bab;
  19. display: flex;
  20. height: 70px;
  21. overflow: hidden;
  22. }
  23. #main {
  24. display: flex;
  25. flex: 1 1 0;
  26. min-height: 0;
  27. }
  28. #left-bar {
  29. background-color: #aaa;
  30. overflow: auto;
  31. width: 250px;
  32. }
  33. #content {
  34. background-color: #ccc;
  35. flex: 1;
  36. }
  1. <div id="root">
  2. <div id="frame">
  3. <div id="navigation-bar">
  4. <h1>Website Name</h1>
  5. </div>
  6. <div id="main">
  7. <div id="left-bar">
  8. <h1>Some content</h1>
  9. <h1>Some content</h1>
  10. <h1>Some content</h1>
  11. <h1>Some content</h1>
  12. <h1>Some content</h1>
  13. <h1>Some content</h1>
  14. <h1>Some content</h1>
  15. <h1>Some content</h1>
  16. <h1>Some content</h1>
  17. </div>
  18. <div id="content"></div>
  19. </div>
  20. </div>
  21. </div>

您需要min-height:0,因为如How can I get FF33.x Flexbox behavior in FF34.x?中所述,FlexBox模块会更改min-height的初始值:

07001

To provide a more reasonable default minimum size for 07002,
this specification introduces a new 07003 value as the initial
value of the 07004 and 07005 properties defined in
CSS 2.1.

我还添加了flex:1 1 0因为flex:1变为flex:1 1 0%,但0%在列布局中Chrome上有问题.但是0效果很好.

猜你在找的HTML相关文章