简体   繁体   English

flex 容器中溢出滚动元素的宽度为 100%

[英]width 100% on an overflow scroll element in flex container

Because a picture is worth a thousand words, I'll show it first:因为一张图值一千字,我先给大家展示一下:

In red is my flex container, in blue is an element with a fixed width of 50px, in green is a section with a 100% width, in yellow is a list also with a 100% width and an overflow scroll, and finally in purple are my list elements width a fixed width of 100px.红色是我的 flex 容器,蓝色是一个固定宽度为 50px 的元素,绿色是一个 100% 宽度的部分,黄色是一个列表也是 100% 宽度和一个溢出滚动,最后是紫色我的列表元素宽度是 100px 的固定宽度。

I want the green element to fit in my red flex container.我希望绿色元素适合我的红色 flex 容器。 How can I do that?我怎样才能做到这一点?

Here's the codesandbox used to make the screenshot above (it's made in React but the html is in App.js and the css in styles.css ): https://codesandbox.io/s/rough-moon-0rsfc?file=/src/styles.css这是用于制作上面屏幕截图的代码和框(它是在 React 中制作的,但 html 在App.js ,css 在styles.css ): https : App.js =/源代码/样式.css

You can add overflow: hidden to the green box to make it stay in the bounds of it's parent.您可以将overflow: hidden添加到绿色框以使其保持在其父级的边界内。

section {
    width: 100%;
    padding: 5px;
    box-sizing: border-box;
    background: green;
    overflow-x: hidden;
}

You had set widths that I removed.你设置了我删除的宽度。 Also, I see that You may not need the div container.另外,我看到您可能不需要div容器。 I left it commented out in case you decide you want to keep the blue div.如果您决定保留蓝色 div,我将其注释掉。

main {
  height: 100px;
  padding: 5px;
  background: red;
  display: flex;
}
/* 
div {
  width: 100%;
  background: blue;
} */

section {
  width: 100%;
  box-sizing: border-box;
  background: green;
  margin: 0;
}

ul {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 5px;
  box-sizing: border-box;
  background: yellow;
  display: flex;
  overflow-x: auto;
  list-style-type: none;
}

li {
  flex: 0 0 auto;
  background: purple;
  width: 100px;
  margin: 0 2px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM