I’m trying to align a label
inline with a text input
and have them stay on the same line in a flex container that will change widths. If I make the container smaller, like 200px, it wraps the input. Make the container 400px width, and it doesn’t wrap. I can’t figure out why the input wraps, but if I set its width smaller to say 100px, it stops wrapping. I can’t do that. The reason I need wrapping is for the third element that needs to always wrap to the following row.
I can’t change the HTML structure.
#test {
display: flex;
flex-wrap: wrap;
width: 200px;
}
#test label {
border-bottom: 1px solid black;
}
#test input {
background-color: transparent;
border: 0;
border-bottom: 1px solid black;
flex-grow: 1;
line-height: 18px;
min-width: 0;
/* width: 100px; */
}
<div id="test">
<label>Label text:</label><input type="text" value="Stay inline!" />
<span>Third element to wrap to next line.</span>
</div>
4
Answers
You can try CSS grid.
You have 3 elements and flex wrap applies to all of them, letting all of them move around. If you want input to stay inline with label you will have to group it.
In some point label and input will forced by size of size of
#test div
to stack, to avoid this and force them to overflow#test
, you can applydisplay: flex
without allowing wrap. Here is my implementation:Use
display: flex
as normal, but for thespan
, you can use flexflex: 1 1 100%;
. For the input, give it a width, saywidth: 100px;
, or less withflex-grow: 1;
.The problem with your
input
was that it has a fixed width by default, so you need to set a smaller width to it.I found a non-grid solution: You can set
width: 0
forinput
while keeping itsflex-grow: 1
. To make the third element wrap, set aflex-basis
of100%
for it and prevent it from shrinking/growing.Do note that this won’t work if
#test
‘s width is smaller than that oflabel
.Try it: