简体   繁体   English

使CSS FlexBox采用固定的列宽

[英]Make CSS FlexBox take fixed column width

I would like for the form below to have a fixed width and when a user clicks on the input box, this input box should expand and the other input boxes should shrink but the overall column width of the form should always remain constant. 我希望下面的表单具有固定的宽度,并且当用户单击输入框时,此输入框应扩展,其他输入框应收缩,但表单的总列宽应始终保持恒定。

Same behavior, if the user clicks on the other input boxes. 如果用户单击其他输入框,则行为相同。

Is this possible with CSS and Flexbox or some Javascript is required? CSS和Flexbox是否可能这样做,还是需要某些Javascript?

Here is a fiddle and a snippet with and example: 这是带有和示例的小提琴和摘录:

 #testForm { padding: 0; display: flex; max-width: 600px; } #input1 { background: red; transition: all .5s ease; } #input2 { background: blue; transition: all .5s ease; } #input1:focus { flex: 1 1 auto; } #input2:focus { flex: 1 1 auto; } 
 <form id="testForm"> <input type="text" id="input1"> <input type="text" id="input2"> </form> 

You can give both input elements flex: 1 to make them fill the parent's width and on :focus make them flex: 2 or whatever you want: 您可以将两个input 元素都设置为 flex: 1以使其填充父对象的 width并在:focus上为其设置flex: 2或您想要的任何值:

 #testForm { padding: 0; display: flex; max-width: 600px; } #input1 { background: red; } #input2 { background: blue; } #input1, #input2 { flex: 1; transition: all .5s ease; } #input1:focus, #input2:focus { flex: 2; } 
 <form id="testForm"> <input type="text" id="input1"> <input type="text" id="input2"> </form> 

just increase the flex on the focused input 只需增加针对性输入的灵活性

 #testForm{ padding: 0; display: flex; max-width: 600px; } #testForm input{ flex: 1; transition: all .5s ease; } #input1{ background: red; } #input2{ background: blue; } #testForm input:focus{ flex: 3; } 
 <form id="testForm"> <input type="text" id="input1"> <input type="text" id="input2"> </form> 

It is but you have to tweak some things. 是的,但是您必须调整一些东西。

Firstly, you have to set a width on your form not a min-width then set your :focus size to whatever is required. 首先,您必须在表单上设置width而不是min-width然后将:focus大小设置为所需的大小。

However, input elements have a minimum size of 20 that has to be overridden in the HTML for the true effect to work. 但是, input元素的最小大小为20,必须在HTML中覆盖该大小 ,才能发挥真正的效果。

An input element without a set width get its size from its size attribute, which defaults to 20. 没有设置宽度的输入元素从其size属性获取其大小,默认为20。

 * { margin: 0; padding: 0; box-sizing: border-box; } #testForm { padding: 0; display: flex; width: 600px; border: 1px solid grey; } input { flex: 1; min-width: 0; margin: 0; border: none; } #input1 { background: red; transition: all .5s ease; } #input2 { background: blue; transition: all .5s ease; } #input1:focus { flex: 20; } #input2:focus { flex: 20; } 
 <form id="testForm"> <input type="text" id="input1" size="5"> <input type="text" id="input2" size="5"> </form> 

Is there a reason your form is set to max-width over simply width ? 将表单设置为max-width不是简单的width是否有原因?

By setting the static width to 600px and adding a flex-grow rule to all the inputs it seems to be working as you describing. 通过将静态宽度设置为600px并向所有输入添加flex-grow规则,它似乎可以按照您的描述工作。

See snippet below. 请参见下面的代码段。

 #testForm { padding: 0; display: flex; width: 600px; overflow: hidden; } input { flex: 1; } #input1 { background: red; transition: all .5s ease; } #input2 { background: blue; transition: all .5s ease; } #input1:focus { flex: 1 1 auto; } #input2:focus { flex: 1 1 auto; } 
 <form id="testForm"> <input type="text" id="input1"> <input type="text" id="input2"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM