If you can use the flexbox layout, this is the only way to center elements without resorting to tables, text alignment, or fixed width.
http://jsfiddle.net/tuu4y/
HTML
<form> <label>Hello <input /> </label> </form>
CSS
form { display: inline block; display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; -webkit-box-align: center; display: -moz-box; -moz-box-orient: horizontal; -moz-box-pack: center; -moz-box-align: center; display: box; box-orient: horizontal; box-pack: center; box-align: center; }
If you need to support older browsers and you can hardcode the width of http://jsfiddle.net/tuu4y/4/
form { width: 300px; margin: 5px auto; }
If you cannot set the width hard, you can use any of the three tricks, some do not check, and some avoid. All of them require an inline-block
form.
Use text-align: center in container container
If you do not want the text to be centered, you should put text-align:left
in the form itself http://jsfiddle.net/tuu4y/2/
body { text-align: center; } form { display: inline-block; text-align: left; }
Place the wrapper <form align="center">...</form>
,
Doesn't check though http://jsfiddle.net/tuu4y/1/
<form align="center"> <label>Hello <input /> </label> </form>
source share