\$\begingroup\$
\$\endgroup\$
I am trying to create stacked inputs using flexbox, with the additional ability to also set rows. Here is the code:
.stacked-inputs,
.stacked-inputs .row {
display: -ms-flexbox;
display: flex;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
-ms-flex-align: center;
align-items: center;
}
.stacked-inputs .row {
width: 100%;
margin-bottom: 10px;
}
.stacked-inputs > input,
.stacked-inputs .row > input {
position: relative;
-ms-flex: 1 1 0%;
flex: 1 1 0%;
min-width: 0;
}
<div class="stacked-inputs">
<input type="text" placeholder="First name">
<input type="text" placeholder="Last name">
</div>
<br /><br />
<div class="stacked-inputs">
<div class="row">
<input type="text" placeholder="First name">
<input type="text" placeholder="Last name">
</div>
<div class="row">
<input type="text" placeholder="Job title">
<input type="text" placeholder="Year started">
</div>
</div>
When inputs are stacked without using rows, they will all be stacked horizontally. However, if a row is used, then obviously the rows of stacked inputs can be divided up nicely. Would love a review of this code from someone. Thanks in advance.
asked May 20, 2020 at 11:28
1 Answer 1
\$\begingroup\$
\$\endgroup\$
0
The following is my opinion on how to improve your code.
- You don't need write
flex-flow: row-wrap
becauseflex
by default set row, then you can use another property of cssflex-wrap: wrap
. - If you want to use flex in the whole of your code, you can set flex properties for
.row
class. - Care about responsiveness, because flex is very good for making your page responsive. Due to this, we use
flex-wrap
andmin-width
to our input.
.stacked-inputs,.stacked-inputs .row {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.stacked-inputs .row {
display: flex;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 100%;
margin-bottom: 10px;
}
.stacked-inputs > input,
.stacked-inputs .row > input {
-ms-flex: 1 1 0%;
flex: 1 1 0%;
min-width: 200px;
}
answered Jun 1, 2020 at 7:57
lang-css