Back in 2009, Chris Coyier shared a fantastic solution that I stumbled upon here. What sets this solution apart is its ability to center content both vertically and horizontally, while also accommodating dynamic height. Unlike other answers that rely on fixed heights, which can be limiting, this one allows for scrolling on smaller screens, making it perfect for creating centered inline forms as an alternative to modal pop-ups.
I took the liberty of refining and converting the code into a Sass mixin for your convenience. Now, you can easily adjust the form width by incorporating form element CSS and related variables into the Sass mixin. This ensures that your form element widths remain within the constraints of the centered inline form.
If you want to see it in action, check out the jsFiddle demo.
Sass (SCSS) Code
html,
body,
form {
height: 100%;
margin: 0;
padding: 0;
}
@mixin CenteredInlineForm( $ContainerSetName, $formWidth) {
div.#{$ContainerSetName}_CenteredForm {
display: table;
overflow: hidden;
margin: 0 auto;
height: 100%;
width: #{($formWidth + 15)}px;
div.#{$ContainerSetName}_CenteredFormContentContainer {
display: table-cell;
vertical-align: middle;
div.#{$ContainerSetName}_CenteredFormContent {
background-color: lightgrey;
border: 3px solid darkgrey;
border-radius: 15px;
padding: 15px;
}
}
}
*:first-child + html div.#{$ContainerSetName}_CenteredForm {
position: relative;
}
/*ie6 Support: */
/* * html div.#{$ContainerSetName}_CenteredForm{position:relative;} */
*:first-child + html div.#{$ContainerSetName}_CenteredFormContentContainer {
position: absolute;
top: 50%;
}
/*ie6 Only: */
/* * html div.#{$ContainerSetName}_CenteredFormContentContainer{position:absolute;top:50%;} */
*:first-child + html div.#{$ContainerSetName}_CenteredFormContent {
position: relative;
top: -50%;
}
}
@include CenteredInlineForm(UserInfoInput, 400);
HTML Markup
<div class="UserInfoInput_CenteredForm">
<div class="UserInfoInput_CenteredFormContentContainer">
<div class="UserInfoInput_CenteredFormContent">
<p>
Content or form labels/inputs
</p>
...
</div>
</div>
</div>