英文:
How does flexbox work when its children are fullscreen
问题
我想制作一个水平滚动的网站
所以我使用了以下代码
<div class="flexParent">
<div class="flexChild"></div>
<div class="flexChild"></div>
</div>
.flexParent {
display: flex;
}
.flexChild {
height: 100vh;
width: 100vw;
}
但是这会导致屏幕分成两部分,每个div都获得了50vw。
但如果我将.flexParent
更改为
.flexParent {
display: flex;
flex-direction: column
overflow-x: scroll
}
然后另一个div将呈现在下一页,每个子元素保留了100vh和100vw的高度和宽度,需要我向下滚动。
为什么在flex-direction: row
中不会发生这种情况?
js fiddle: http://jsfiddle.net/oenkraLt/16/
英文:
I want to make a horizontal scrolling website
So I used the following
<div class="flexParent">
<div class="flexChild"></div>
<div class="flexChild"></div>
</div>
.flexParent {
display: flex;
}
.flexChild {
height: 100vh;
width: 100vw;
}
But this causes the screen to divide in two parts and each div getting a 50vw.
But if I change .flexParent
to
.flexParent {
display: flex;
flex-direction: column
overflow-x: scroll
}
then the other div is rendered on the next page, each child retaining the 100vh and 100vw height and width respectively, requiring me to scroll down.
Why does this not happen in flex-direction: row
js fiddle: http://jsfiddle.net/oenkraLt/16/
答案1
得分: 1
你需要通过flex-basis
来设置宽度,并将flex-shrink
定义为零。可以使用flex
缩写来完成此操作,它还设置了flex-grow
。因此,请声明 flex: 0 0 100%;
。
英文:
You need set the width via flex-basis
and also define the flex-shrink
to be zero. This can be done using the flex
shorthand that also sets the flex-grow
. So, declare flex: 0 0 100%;
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
body {
margin: 0;
}
.flexParent {
display: flex;
column-gap: 1px;
}
.flexChild {
flex: 0 0 100%;
height: 98vh;
background-color: lightblue;
}
<!-- language: lang-html -->
<div class="flexParent">
<div class="flexChild">Some content 1</div>
<div class="flexChild">Some content 2</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论