如何消除垂直溢出的父元素,其中使用flexbox

2022-01-27 23:20:14 标签 htmlcss

我正在努力与一个样式元素,其中包装元素,包含项目列表。

html结构

<div id="root">
<div class="app"> <!-- Causes overflow - unexpected -->
  <nav class="appTopbar"></nav>
  <main class="page">
    <nav class="pageTopbar"></nav>
    <div class="container">
      <div class="left">
        <nav class="pageTopbar"></nav>
        <div class="list"> <!-- Causes overflow - expected -->
          <div class="item">Item 1</div>
          ...
        </div>
      </div>
      <div class="right"></div>
    </div>
  </main>
</div>
</div>

当。list累积了很多项时,就会出现这个问题。

。app 。page 。container 。left元素使用带有blox轴方向的flexbox。

display: flex;
flex-direction: column;

除了。left设置height: 100%或height: inherit

一种(可怕的)解决方法是在。page中添加overflow: hidden;让它过去,但也许有更好的方法。

谢谢你不辞辛劳地帮助我。

下面是导致溢出的html代码示例。

const list = document.querySelector(".list");
                         
let index = 0;
for(let it of new Array(100)) {
  const node = document.createElement("div");         
  const textnode = document.createTextNode(`Element ${index}`); 
  node.appendChild(textnode);
  list.appendChild(node); 
  
  index++;
}
html, body {
  height: 100%;
  margin: 0;
}
#root {
  height: inherit;
}
* {
    box-sizing: border-box;
}
.app {
  display: flex;
  flex-direction: column;
  justify-content: stretch;
  align-items: stretch;
  height: inherit;
}
.appTopbar {
  display: flex;
  align-items: center;
  flex: 0 0 50px;
  
  background: #00308F;
}
.page {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.container {
  flex: 1 1;
  display: flex;
  height: 100%;
}
.left {
  display: flex;
  flex-direction: column;
  flex: 0 1 100px;
  
  background: #7FFFD4;
}
.right {
  flex: 1 1;  
  background: #0066b2;
}
.list {
  padding: 10px;
  overflow-y: auto;
  flex: 1 1;
}
<div id="root">
  <div class="app">
    <nav class="appTopbar"></nav>
    <main class="page">
      <nav class="pageTopbar"></nav>
      <div class="container">
        <div class="left">
          <nav class="pageTopbar"></nav>
          <div class="list"></div>
        </div>
        <div class="right"></div>
      </div>
    </main>
  </div>
</div>

###如果我是正确的理解,你需要删除右侧滚动条。在这种情况下,改变类。page的高度height:100% to height: calc(100% - 50px) where 50px is your height of the .appTopbar class.

height: calc(100% - 50px) whe50px是。 apptopbar类的高度。

.page {
  display: flex;
  flex-direction: column;
  height: calc(100% - 50px); /* changed */
}

const list = document.querySelector('.list');
let index = 0;
for (let it of new Array(100)) {
  const node = document.createElement('div');
  const textnode = document.createTextNode(`Element ${index}`);
  node.appendChild(textnode);
  list.appendChild(node);
  index++;
}
html,
body {
  height: 100%;
  margin: 0;
}
#root {
  height: inherit;
}
* {
  box-sizing: border-box;
}
.app {
  display: flex;
  flex-direction: column;
  justify-content: stretch;
  align-items: stretch;
  height: inherit;
}
.appTopbar {
  display: flex;
  align-items: center;
  flex: 0 0 50px;
  background: #00308f;
}
.page {
  display: flex;
  flex-direction: column;
  height: calc(100% - 50px);
}
.container {
  flex: 1 1;
  display: flex;
  height: 100%;
}
.left {
  display: flex;
  flex-direction: column;
  flex: 0 1 100px;
  background: #7fffd4;
}
.right {
  flex: 1 1;
  background: #0066b2;
}
.list {
  padding: 10px;
  overflow-y: auto;
  flex: 1 1;
}
<div id="root">
  <div class="app">
    <nav class="appTopbar"></nav>
    <main class="page">
      <nav class="pageTopbar"></nav>
      <div class="container">
        <div class="left">
          <nav class="pageTopbar"></nav>
          <div class="list"></div>
        </div>
        <div class="right"></div>
      </div>
    </main>
  </div>
</div>

###删除滚动条的主体,就像overflow:hidden; to id=root that is
id=root tha这是< br / >

#root {height: inherit;overflow: hidden;}

结果如下面的代码片段所示

const list = document.querySelector(".list");
let index = 0;
for (let it of new Array(100)) {
  const node = document.createElement("div");
  const textnode = document.createTextNode(`Element ${index}`);
  node.appendChild(textnode);
  list.appendChild(node);
  index++;
}
html,
body {
  height: 100%;
  margin: 0;
}
#root {
  height: inherit;
  overflow: hidden;
}
* {
  box-sizing: border-box;
}
.app {
  display: flex;
  flex-direction: column;
  justify-content: stretch;
  align-items: stretch;
  height: inherit;
}
.appTopbar {
  display: flex;
  align-items: center;
  flex: 0 0 50px;
  background: #00308F;
}
.page {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.container {
  flex: 1 1;
  display: flex;
  height: 100%;
}
.left {
  display: flex;
  flex-direction: column;
  flex: 0 1 100px;
  background: #7FFFD4;
}
.right {
  flex: 1 1;
  background: #0066b2;
}
.list {
  padding: 10px;
  overflow-y: auto;
  flex: 1 1;
}
<div id="root">
  <div class="app">
    <nav class="appTopbar"></nav>
    <main class="page">
      <nav class="pageTopbar"></nav>
      <div class="container">
        <div class="left">
          <nav class="pageTopbar"></nav>
          <div class="list"></div>
        </div>
        <div class="right"></div>
      </div>
    </main>
  </div>
</div>

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码