If you're working with modern browsers you can use the flexbox layout module: http://caniuse.com/#feat=flexbox.
Flexbox documentation: developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
Note: Can't post more than two links due to my rep.
JSFiddle.
(Using a footer
tag instead of a div#footer
as it's simpler.)
<div id="footer-container">
<footer>hello world</footer>
<div>
#footer-container {
bottom: 20px;
position: fixed;
display: flex;
justify-content: center;
width: 100%;
}
footer {
width: 500px;
background-color: red;
}
justify-content: center;
'centers' #footer-container
's children, which is just the footer
element in this case.
This is very similar to Nick N.'s solution, except that you don't have to reset the text-align
property on the footer, and that this is probably the non-'trick' way that you wanted.
The accepted solution is slightly off because the footer's width in that case is variable (80%) instead of at 500px.
To other readers, if your parent is a form
element, and the child is an input
element, use flex: 1;
on the input
(child) element, and use max-width: 500px;
instead of width: 500px;
. Using flex: 1;
should make the input
element expand to fill the form
element's width, which it might not otherwise do.