I have a form with the bottom-aligned behaviour, I need all the inputs in a row to be aligned to the bottom and be on one line visually. The reason for this is that the label can include instructions or not include them. The controls can be of different hight and type (text input, select, radio group, checkbox group, fieldset).
I use the align-self: end;
to make the control div to be aligned to the bottom. It works fine when I have one-line error
But it doesn’t look good when I have multi-line error.
Can anyone suggest how to align the controls properly using the CSS (maybe grid…)?
The desired result (the right input is located one the same line as the left one):
Thank you!
body {
padding: 50px;
font: 1em Helvetica Neue, Helvetica, Arial, sans-serif;
}
.form {
display: flex
}
.error {
color: red;
padding: 5px 0
}
.form-item {
border: 5px solid rgb(111, 41, 97);
display: grid;
inline-size: 300px;
}
.label {
background: orange;
}
.control {
background: #eee;
padding: 10px;
/* display: grid; */
align-self: end;
}
.input {}
<form class="form">
<div class="form-item">
<div class="label">
<div>Label </div>
<div style="color: grey">Long instructionsLong instructionsLong instructionsLong instructions Long instructions Long instructionsLong instructionsLong instructionsLong instructions Long instruction </div>
</div>
<div class="control">
<div class="input"><input type="text"></div>
<div class="error">Long error Long error Long errorLong errorLong errorLong errorLong errorLong errorLong error Long error Long errorLong errorLong errorLong errorLong errorLong er</div>
</div>
</div>
<div class="form-item">
<div class="label">Label</div>
<div class="control">
<div class="input"><input type="text"></div>
<div class="error">Short error</div>
</div>
</div>
</form>
3
Answers
Depending on the rest of your layout or other requirements you could use css-grid in order to get your desired layout.
If you remove the divs around each input and label, you could just use a grid, which flows in the column direction. The height of the rows will be defined by the largest item and therefore affect the other control-divs as well.
you could use that site for grid helper: for more https://cssgrid-generator.netlify.app/
alternative solution are:
Based on your html, you can apply
.form-item { grid-template-rows:subgrid; }
. Here is a simple example: