Flexbox在其子元素全屏时如何工作

huangapple go评论50阅读模式
英文:

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 -->

&lt;div class=&quot;flexParent&quot;&gt;
  &lt;div class=&quot;flexChild&quot;&gt;Some content 1&lt;/div&gt;
  &lt;div class=&quot;flexChild&quot;&gt;Some content 2&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 11:53:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定