简体   繁体   English

拉伸或滚动父级伸缩框

[英]Stretch or scroll parent flex box

I have the following HTML and CSS that I'm trying to get to either stretch or scroll the parent flex box without a static width. 我尝试使用以下HTML和CSS来拉伸或滚动父级flex框而没有静态宽度。 I can't seem to get either result. 我似乎无法获得任何结果。

No matter how many box classes I add, it doesn't make the flex box stretch or scrollable. 无论我添加多少个box类,它都不会使弹性框伸展或滚动。 I want to be able to have a box of 300 width even if it means to be able to scroll but all it does is make box smaller and doesn't respect the width of it. 我希望能够有一个300宽度的box ,即使这意味着可以滚动,但是它所做的只是使box变小并且不尊重它的宽度。

JSFiddle here: http://jsfiddle.net/r4j778xg/2/ JSFiddle在这里: http : //jsfiddle.net/r4j778xg/2/

<div class="flex">
<div class="box">stuff</div>
<div class="box">stuff</div>
<div class="box">stuff</div>
...many more
</div>

.flex
{
    width: 100%;
    display: flex;
    flex-direction: row;
}

.box
{
    width: 300px;
    height: 50px;
    border: solid black 1px;
}

Try with, with my thinking 100% its means fill parent container which is window width so you cant get wider than that 尝试一下,我的想法是100%的意思是填充父容器,该容器是窗口宽度,所以您不能比它宽

.flex{
    width: auto;
    position: absolute;
}

JSFIDDLE JSFIDDLE

Plus if you want keep this position: relative; 另外,如果您想保持这个position: relative; to keep height value so next element show after this one you can wrap it like this 保持高度值,以便在此元素之后显示下一个元素,您可以像这样包装它

JFSIDDLE JFSIDDLE

The problem here is that you are setting a relative width:100% to you flex div, and its parent doesn't even have a fixed width so the problem is 100% of what? 这里的问题是,您要为flex div设置相对 width:100% ,并且其父级甚至没有固定的宽度,因此问题出在100%的范围内? , so when you make a fixed width to your parent div (body for example) the problem will be solved: ,因此当您对父div(例如body)设置固定宽度时,该问题将得到解决:

body{
  width:1096px;
  height:960px;
}

And Give your flex div a display: inline; 并给您的flex div一个display: inline; or display: table; display: table; , because display: flex; ,因为display: flex; will force the div to be a block-level flex container. 将强制div为块级flex容器。

This is a working fiddle . 这是一个工作的小提琴

I think that should do it. 我认为应该这样做。

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

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