CSS margin auto not centering

Crispy picture Crispy · May 29, 2013 · Viewed 14k times · Source

I have simplified the code as much as possible, but still can't get it to center. I believe my inexperience with CSS is not allowing me to see something extremely simple.

If anyone also knows some good resources for css, that'll be great. I have read sections of many CSS books but there all very basic and cover the same thing.

CSS Markup:

/* MAIN ––––––––––––––––––––– */
html {
    /* overflow-y:scroll; */
}
body {
    /* margin:0; */
    /* position:relative; */
}
/* Form ––––––––––––––––––––– */
#form {
    margin:0 auto;
    width: 300px;
    height: 200px;
    display: inline-block;
    border: 2px solid black;
}

index.html

<html>
  <body>

    <form id='form'></form>

  </body>
</hml>

Answer

Peter Wooster picture Peter Wooster · May 29, 2013

Change the display to block if you want to use margin:auto for centering. That does not work with inline elements.