I have a page to fill some values. I'm trying to use CSS to break the lines of the texts correctly without the need of <br>
elements.
It's suppost to work like this:
- The content is suppost to completely fill (horizontally) the top
<div>
container but not overflow it. <select>
and<input>
elements stretch to fill the available space and have a minimum width.- The width of the
<span>
elements should be the shortest possible while avoiding breaking lines unless needed. <span>
elements should break line only when in case they don't the other elements in the same line (<select>
or<input>
) can't have the minimum width and not overflow.- When a
<span>
element is broken into more than one line it should distribute the text evenly so that it has the shortest width (while still not recurring to breaking more lines unnecessarily). Example:
Correct:
One two
three
Incorrect:
One
two three
One
two
three
Code:
body {
background-color: rgb(33, 38, 45);
color: rgb(255, 255, 255);
font-family: sans-serif;
font-size: 20px;
font-weight: bold;
margin: 0;
}
body > div {
background-color: rgb(32, 32, 32);
margin: 0 auto;
white-space: nowrap;
width: 400px;
}
div > div {
align-items: center;
display: flex;
margin-top: 10px;
}
span {
text-align: center;
}
span.green {
color: rgb(0, 128, 0);
}
span.red {
color: rgb(196, 0, 0);
}
span.blue {
color: rgb(64, 64, 255);
}
span.yellow {
color: rgb(255, 128, 0);
}
select {
background-color: rgba(0, 0, 0, 0.2);
border: 2px solid rgb(196, 196, 196);
border-radius: 5px;
color: rgb(228, 167, 105);
font-size: 20px;
margin-left: 10px;
min-width: 200px;
outline: none;
padding: 10px;
width: 100%;
}
option {
color: rgb(0, 0, 0);
}
option:first-child {
display: none;
}
input {
background-color: rgba(0, 0, 0, 0.2);
border: 2px solid rgb(196, 196, 196);
border-radius: 5px;
color: rgb(228, 167, 105);
font-size: 20px;
margin-left: 10px;
min-width: 200px;
outline: none;
padding: 10px;
width: 100%;
}
<body>
<div>
<div>
<span class="green">Short text:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="green">Slightly longer text:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="red">Text suppost to break into three lines:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="blue">Text broken into<br>two lines manually:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="green">This text is<br>corretly broken<br>into three lines:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="yellow">Text<br>incorrectly<br>broken:</span><select><option>Select an option</option><option>Yes</option><option>No</option></select>
</div>
<div>
<span class="green">Remarks:</span><input type="text">
</div>
<div>
<span class="red">No no no no no<br>no:</span><input type="text">
</div>
<div>
<span class="green">Yes yes yes<br>yes yes:</span><input type="text">
</div>
</div>
</body>
I'm using Chrome and it looks like this:
This is how it's suppost to look:
I thought the first red one would fit in two lines but it doesn't (it overflows) so I edited it to three: