Let's conduct a simple test to illustrate the situation:
HTML
/* This form will have a width of 200px and will be centered within its parent element */
<form></form>
/* #formsection is 300px wide and not centered. The form inside it will be
200px wide and centered within #formsection */
<div id="formsection">
<form></form>
</div>
CSS
/* All forms will have a width of 200px and will be centered within their container */
form {
width: 200px;
margin: 0 auto;
border: 1px solid red;
}
/* #formsection is 300px wide and not centered */
#formsection {
width: 300px;
border: 1px solid blue;
}
#formsection form {
border: 1px solid green;
}
You can view a demonstration here.
You brought up that changing the width
property does not affect the element's width. This may occur if your <form>
has a child element wider than the specified width (e.g., in your case with 5px
). To check, add a border
or background-color
to your child elements and see if any are overflowing the form.
Alternatively, you can use the Web Developer addon for Firefox for a quicker solution. Once installed, pressing CTRL+SHIFT+Y
will display the style information of the element you hover over.