简体   繁体   English

溢出下的 Flexbox 未占用全宽

[英]Flexbox under overflow not taking full width

I've simplified my HTML/CSS in a jsfiddle of what I'm trying to achieve without success.我已经在我试图实现但没有成功的jsfiddle中简化了我的 HTML/CSS。

I cannot make the .problem take full width.我不能让.problem占据全角。 The content is represented by the blue background while the box itself is red.内容由蓝色背景表示,而框本身为红色。

I'm trying to make all the scrollable content have full width and a blue background since I guess the background not appearing after the scrollable content is a problem of width.我正在尝试使所有可滚动内容都具有全宽和蓝色背景,因为我猜在可滚动内容之后没有出现背景是宽度问题。


What I've taken a look so far:到目前为止我看过的内容:

Flexbox not full width Flexbox 不是全宽

Flexbox: how to get divs to fill up 100% of the container width without wrapping? Flexbox:如何让 div 在不换行的情况下填充 100% 的容器宽度?

Fill 100% width of scrolling flex container 填充滚动 flex 容器的 100% 宽度


To clarify: giving .problem a fixed width does give me the effect I want but not the solution since the content is dynamic and I cannot know its width.澄清一下:.problem一个固定的宽度确实给了我想要的效果,但不是解决方案,因为内容是动态的,我不知道它的宽度。

 * { margin: 0; padding: 0; box-sizing: border-box; position: relative; } html, body { width: 100%; height: 100%; }.bg { display: flex; background-color: gray; width: 100%; height: 100%; }.usable { display: flex; height: 90%; width: 100%; background-color: white; }.box { position: absolute; top: 0; left: 0; height: 200px; width: 300px; display: flex; align-items: flex-start; align-content: flex-start; flex-direction: column; background-color: red; }.top { display: flex; height: 20px; background-color: yellow; width: 100%; }.bottom { width: 100%; flex: 0 1 100%; overflow: auto; }.contents { height: 100%; display: flex; flex-direction: column; }.forms { flex: 0 1 100%; }.problem { display: flex; height: 100%; background-color: blue; }.row { display: flex; align-items: center; }.cell { align-self: stretch; flex: 1 0 100px; }
 <div class="bg"> <div class="usable"> <div class="box"> <div class="top"> Top </div> <div class="bottom"> <div class="contents"> <div class="forms"> <div class="problem"> <div class="data"> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 00:21:06</div> <div class="cell">test1</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:26</div> <div class="cell">test2</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:26</div> <div class="cell">test3</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:37</div> <div class="cell">test4</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:37</div> <div class="cell">test5</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:37</div> <div class="cell">test6</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:37</div> <div class="cell">test7</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:58</div> <div class="cell">test8</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:42:58</div> <div class="cell">test9</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> <div class="row"> <div class="cell">2020-06-28 01:34:46</div> <div class="cell">2020-08-13 13:43:11</div> <div class="cell">test10</div> <div class="cell"></div> <div class="cell">Basic User</div> </div> </div> </div> </div> </div> </div> </div> </div> </div>

The problem is that the container with the blue background is taking the width of the parent and not the content, to solve this you can change its property "display" to "inline-flex".问题是具有蓝色背景的容器占用了父容器的宽度而不是内容,要解决这个问题,您可以将其属性“display”更改为“inline-flex”。

Then if you change the "flex" property of the.cell class to a "width" property the width you set will count for the parent and it will reach the blue container making it fill all the content.然后,如果您将.cell class 的“flex”属性更改为“width”属性,您设置的宽度将计入父级,它将到达蓝色容器,使其填充所有内容。

      .problem {
        display: inline-flex;
        height: 100%;
        background-color: blue;
      }

      .cell {
        align-self: stretch;
        width: 100px;
      }

To allow elements to grow according to content, you can replace 'width' by 'min-width'.要允许元素根据内容增长,您可以将 'width' 替换为 'min-width'。 This makes sure your box is a certain size for styling purposes but allows it to grow.这可以确保您的盒子有一定的尺寸以用于造型目的,但允许它增长。 Then you can use 'max-width' to limit the amount it can grow.然后你可以使用'max-width'来限制它可以增长的数量。

So, change 'width: 100%' by 'min-width: 100%'.因此,将 'width: 100%' 更改为 'min-width: 100%'。

You can find more here: https://css-tricks.com/boxes-fill-height-dont-squish/你可以在这里找到更多: https://css-tricks.com/boxes-fill-height-dont-squish/

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

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