I use a table view to align my form. At the same time, I hope that the submission button is in the middle of the form, but the browser always seems to put it in the middle of the table cell.
form {
display: table;
}
form > div {
display: table-row;
}
form > div > label {
display: table-cell;
padding-right: 10px;
}
form > input[type="submit"] {
display: block;
margin: auto;
}
<form>
<div class="user-name">
<label for="user-name">User Name</label>
<input type="text" name="user-name" id="user-name" />
</div>
<div class="password">
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</div>
<input type="submit" />
</form>
3
Answers
Here is a solution using Flex instead of Table! I believe flex may be a more viable option for what you are trying to achieve because it offers more flexibility as far as orienting & aligning content.
This would be
display: table
version. Yourmargin
does not work in a table. Additionally, there’s nocolspan
available. Therefore you have toposition: absolute
a child and span it the whole width (or in your case, center it withleft
andtransform
).So either use a real
<table>
OR – as it’s 2023 – use flexbox or the newer css grid.As the code above you can change your display style and add more form elements just like this and this will give the perfect look, hope this helps, comment if you need anything else.