gpt4 book ai didi

css - 制作具有相同高度的flexbox child

转载 作者:行者123 更新时间:2023-11-28 01:54:06 25 4
gpt4 key购买 nike

我有一个 flexbox parent 设置了 flex-direction: row。在这个 parent 里面,我有两个 child 。我希望他们有相同的高度!

在这个 child 里面我有动态内容(高度可变)。

我的做法是,如果我在右边的 child 上添加文字,左边的 child 就会增长。但是如果左边的 child 长大了,右边的 child 就会变小。

他们不应该以同样的方式行事吗?

这是一个 fiddle :https://jsfiddle.net/4g6uevok/8/

HTML:

<div id="main">
<div class="left">
<div class="title">MY TITLE:</div>
<div class="left-area">

<div class="left-area-row">
<div class="left-area-row-titulo">#1</div>
<div class="left-area-row-info">A</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">#2</div>
<div class="left-area-row-info">B</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">#3</div>
<div class="left-area-row-info">AC</div>
</div>

</div>
</div>

<div class="right">
<div class="title">SECOND TITLE:</div>
</div>
</div>

CSS:

#main {
width: 100%;
height:auto;
margin-top: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
background-color: red;
}

.left{
width: 400px;
display: flex;
flex-direction: column;
background:lime;
align-items: stretch;
}

.title {
width: 100%;
font-size: 1.5em;
color:#525252;
display: flex;
justify-content: center;
margin-bottom: 20px;
font-weight: bold;
font-family: "emir-bold";
}

.left-area {
width: 100%;
display: flex;
flex-direction: column;
}
.left-area-row {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.left-area-row-titulo {
width: 49.5%;
display: flex;
align-items: center;
justify-content: flex-start;
background-color: #819196;
color: white;
padding: 6px;
margin:0 2px 4px 0;
}
.left-area-row-info {
width: 49.5%;
display: flex;
align-items: center;
justify-content: center;
background: #CCCCCC;
padding: 6px;
margin:0 0 4px 2px;
}

.right {
width: calc(100% - 430px);
height: 100%;
display: flex;
flex-direction: column;
background:orange;
align-items: stretch;
}

最佳答案

Flex 元素默认与 strech 对齐。 .right 类中的 height:100% 值阻止它占用整个高度,因此请尝试将 height:100% 删除到 .right 元素

#main {
width: 100%;
height: auto;
margin-top: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
background-color: red;
}

.left {
width: 400px;
display: flex;
flex-direction: column;
background: lime;
align-items: stretch;
}

.title {
width: 100%;
font-size: 1.5em;
color: #525252;
display: flex;
justify-content: center;
margin-bottom: 20px;
font-weight: bold;
font-family: "emir-bold";
}

.left-area {
width: 100%;
display: flex;
flex-direction: column;
}

.left-area-row {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
}

.left-area-row-titulo {
width: 49.5%;
display: flex;
align-items: center;
justify-content: flex-start;
background-color: #819196;
color: white;
padding: 6px;
margin: 0 2px 4px 0;
}

.left-area-row-info {
width: 49.5%;
display: flex;
align-items: center;
justify-content: center;
background: #CCCCCC;
padding: 6px;
margin: 0 0 4px 2px;
}

.right {
width: calc(100% - 430px);
display: flex;
flex-direction: column;
background: orange;
align-items: stretch;
}
<div id="main">
<div class="left">
<div class="title">MY TITLE:</div>
<div class="left-area">
<div class="left-area-row">
<div class="left-area-row-titulo">
#1
</div>
<div class="left-area-row-info">A</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#2
</div>
<div class="left-area-row-info">BA</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#3
</div>
<div class="left-area-row-info">C</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#4
</div>
<div class="left-area-row-info">D</div>
</div>
</div>
</div>
<div class="right">
<div class="title">SECOND TITLE:</div>
</div>
</div>

关于css - 制作具有相同高度的flexbox child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com