首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么当我们有flex-direction: column时,这些项不会自动缩小?

为什么当我们有flex-direction: column时,这些项不会自动缩小?
EN

Stack Overflow用户
提问于 2021-10-26 11:41:48
回答 1查看 56关注 0票数 0

我有以下HTML和CSS:

代码语言:javascript
复制
.box-1 {
  background: #e51400;
}
.box-2 {
  background: #fa6800;
}
.box-3 {
  background: #f0a30a; 
}
.box-4 {background: #e3c800; }
.box-5 {background: #a4c400;}
.box-6 {background: #60a917;}
.box-7 {background: #00aba9;}
.box-8 {background: #1ba1e2;} 
.box-9 {background: #aa00ff;}

.container {
  display: flex;
  border: 8px solid black;
  height: 500px;
  display: flex;
  flex-direction: column;
}

.box {
    font-size: 50px;
    color: white;
    text-align:center;
    font-family: lato;
    font-weight: 300;
    box-sizing: border-box;
    width: 300px;
}
代码语言:javascript
复制
<section class="container">
        <div class="box box-1">1</div>
        <div class="box box-2">2</div>
        <div class="box box-3">3</div>
        <div class="box box-4">4</div> 
        <div class="box box-5">5</div>
        <div class="box box-6">6</div>
        <div class="box box-7">7</div>
        <div class="box box-8">8</div>
        <div class="box box-9">9</div>  
</section>

当我们在flex-items上设置了flex-direction: row (默认值)和width时,它们会在父元素中缩小,所以现在它们的宽度不是300px,它们会尝试缩小以适应父元素的宽度,因为flex-shrink属性在flexbox中的默认值为1。

但是,当我将flex-direction: row更改为flex-direction: column时,flex项将移出父级的height。为什么会这样呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-26 12:19:04

您有9个高度为60px的.box元素,它们试图适应高度为500px的.container

如果你计算一下: 9*60px = 540px,它比你的.container高度还大。

你的.boxes的内容不会因为你设置了一个font-size: 50px而缩小,总的行高是60px。这是因为the CSS line-height property is by default at 110% or 120% depending on your browser

在本例中,元素的总高度为

代码语言:javascript
复制
font-size * line-height => 50px * 1.2 => 60px

这不是.box中的空闲空间,因为flexbox将能够收缩以适应容器中的盒子。

正如teefars提到的,我向read a guide about flexbox推荐你

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69722458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档