gpt4 book ai didi

css - 当 flex 元素包含输入元素时,不遵守 Flexbox flex-grow 属性

转载 作者:行者123 更新时间:2023-11-28 19:16:13 25 4
gpt4 key购买 nike

我正在使用 flexbox 制作聊天 UI。首先使用 flex-grow 属性,我将布局分成 2 列,比例为 2/5,然后我将这 2 列分成行,第一列的比例为 1/9,第二列的比例为 5/1。当我没有将输入元素添加到 HTML 代码时,这是我所拥有的:

https://codepen.io/thenr/pen/PoooyEN

#chat {
display: flex;
width: 100%;
height: calc(100vh - 2em);
overflow-y: hidden;
}

#friends {
flex-grow: 2;
display: flex;
flex-direction: column;
border: 1px solid black;
}

#messages {
flex-grow: 5;
display: flex;
flex-direction: column;
border: 1px solid black;
}

#searchfriend {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 120px;
border: 1px solid black;
background-color: blue;
}

#searchfriend input[type="text"] {
margin: 0;
padding: 1em;
border: 0;
border-radius: 0px;
background-color: #d1d1d1;
}

#friendslist {
flex-grow: 9;
border: 1px solid black;
overflow-y: auto;
}

.friend {
height: 100px;
border: 1px solid black;
}

#chatmessages {
flex-grow: 5;
border: 1px solid black;
}

#writemessage {
flex-grow: 1;
border: 1px solid black;
}
<div id="chat">
<!-- friends -->
<div id="friends">
<div id="searchfriend">

</div>
<div id="friendslist">
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>

</div>
</div>

<!-- chat -->
<div id="messages">
<div id="chatmessages">



</div>
<div id="writemessage">

</div>
</div>
</div>

问题是,当我将输入元素添加到 HTML 代码时,我设置的比例没有得到遵守,并且包含这些输入元素的 flex 元素变得更大,我不希望这种情况发生。相同的 HTML 代码,但添加了输入元素:

https://codepen.io/thenr/pen/RwwweQN

#chat {
display: flex;
width: 100%;
height: calc(100vh - 2em);
overflow-y: hidden;
}

#friends {
flex-grow: 2;
display: flex;
flex-direction: column;
border: 1px solid black;
}

#messages {
flex-grow: 5;
display: flex;
flex-direction: column;
border: 1px solid black;
}

#searchfriend {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 120px;
border: 1px solid black;
background-color: blue;
}

#searchfriend input[type="text"] {
margin: 0;
padding: 1em;
border: 0;
border-radius: 0px;
background-color: #d1d1d1;
}

#friendslist {
flex-grow: 9;
border: 1px solid black;
overflow-y: auto;
}

.friend {
height: 100px;
border: 1px solid black;
}

#chatmessages {
flex-grow: 5;
border: 1px solid black;
}

#writemessage {
flex-grow: 1;
border: 1px solid black;
}
<div id="chat">
<!-- friends -->
<div id="friends">
<div id="searchfriend">
<input type="text" name="" id="searchtext" placeholder="Search friend...">

</div>
<div id="friendslist">
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>
<div class="friend"></div>

</div>
</div>

<!-- chat -->
<div id="messages">
<div id="chatmessages">



</div>
<div id="writemessage">
<form action="" id="formchat">
<input type="text" name="msgtext" id="msgtext" autocomplete="off">
<button id="msgsend">Send</button>
</form>
</div>
</div>
</div>

最佳答案

您可以使用 flex 简写 flex:2/flex: 5 代替 flex-grow:2/flex-grow :5

这将避免同时设置 flex-shrinkflex-basis

关于css - 当 flex 元素包含输入元素时,不遵守 Flexbox flex-grow 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259781/

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