How can I have a box that's automatically sized to its content (including form fields) and centered without using role="presentation"
tables for layout? (I'm trying to modernize, and to reduce markup clutter.)
As you can see, I've got most of it (I think), but the form fields stick out of their container, which I suspect is down to fundamental errors in my CSS.
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
font-family: sans-serif;
}
.app {
height: 100%;
padding-top: 20px;
}
.welcome {
text-align: center;
}
/* Center the box horizontally */
.center-wrapper {
display: flex;
justify-content: center;
}
/* The box I'm trying to center and size to content */
.login {
display: inline-block;
border: 1px solid black;
padding: 8px;
line-height: 1.8rem;
}
.login .fields {
margin-top: 8px;
display: grid;
grid-template-columns: 40% auto;
}
<div class="app">
<div class="center-wrapper">
<div class="login">
<div class="welcome"><strong>Welcome!</strong></div>
<div class="welcome">Please sign in</div>
<div class="fields">
<label for="user-email">Email address:</label>
<input id="user-email" type="email" size="25" autofocus>
<label for="user-pass">Password:</label>
<input id="user-email" type="password" size="25">
</div>
</div>
</div>
</div>
The form fields escape their container:
I don't want to assign fixed sizes to the elements and just make sure they fit, I want the box sized to its content and want it to handle the user zooming, different device sizes, etc.
The below gives the effect I'm looking for (very basically), but it uses a presentation table:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
font-family: sans-serif;
}
.app {
height: 100%;
padding-top: 20px;
}
.welcome {
text-align: center;
}
/* Center the box horizontally */
.center-wrapper {
display: flex;
justify-content: center;
}
/* The box I'm trying to center and size to content */
.login {
display: inline-block;
border: 1px solid black;
padding: 8px;
line-height: 1.8rem;
}
.login th {
font-weight: normal;
}
<div class="app">
<div class="center-wrapper">
<table class="login" role="presentation">
<tbody>
<tr>
<th colspan="2"><strong>Welcome!</strong></th>
</tr>
<tr>
<th colspan="2">Please sign in</th>
</tr>
<tr>
<td>
<label for="user-email">Email address:</label>
</td>
<td>
<input id="user-email" type="email" size="25" autofocus>
</td>
</tr>
<tr>
<td>
<label for="user-pass">Password:</label>
</td>
<td>
<input id="user-email" type="password" size="25">
</td>
</tbody>
</table>
</div>
</div>
Aside from not being great semantically (it's not really a table), I don't like the markup burden.