3

I have some troubles to center items in html form. A very simple example here:

https://jsfiddle.net/a9cnuypz/

#rectLogin {
  margin: auto;
  width: 60%;
  border-style: solid;
  border-width: 1px;
}
#formLogin {
  margin: auto;
  width: 60%;
  display: inline-block;
}
#btn {
  margin: auto;
  display: inline-block;
  width: 200px;
}
<div id="rectLogin">
  <form id="formLogin">
    <div>
      <label>Username</label>
      <fieldset>
        <input type="text" name="username" required>
      </fieldset>
      <label>Password</label>
      <fieldset>
        <input type="password" name="password" required>
      </fieldset>
      <input id="btn" type="submit" value="Login">
    </div>
  </form>
</div>

Here is how appears now:

actual

and here how I want:

desired

I don't understand my mistake. I set for the relevant items (form and button) both a width and margins=auto.

chirag
  • 1,761
  • 1
  • 17
  • 33
Mark
  • 4,338
  • 7
  • 58
  • 120

2 Answers2

7

Set display: block; to #formLogin and #btn

#formLogin {
    margin: auto;
    width: 60%;
    display: block;
}

#rectLogin {
  margin: auto;
  width: 60%;
  border-style: solid;
  border-width: 1px;
}

#formLogin {
  margin: auto;
  width: 60%;
  display:block;
}

#btn {
  margin: auto;
  display:block;
  width: 200px;
}
<div id="rectLogin">
  <form id="formLogin">
    <div>
      <label>Username</label>
      <fieldset>
        <input type="text" name="username" required>
      </fieldset>
      <label>Password</label>
      <fieldset>
        <input type="password" name="password" required>
      </fieldset>
      <input id="btn" type="submit" value="Login">
    </div>
  </form>
</div>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
  • Quick and easy. Thanks for the clarification. Need to wait few minutes to accept the answer :) – Mark Oct 25 '16 at 05:59
  • Sure!! For more info: http://stackoverflow.com/questions/19076919/marginauto-with-inline-block – Abhishek Pandey Oct 25 '16 at 06:06
  • 1
    This link is very useful. Now I understand the difference between block and inline-block - and it make sense, indeed! – Mark Oct 25 '16 at 13:58
0

text-align center in

 #rectLogin 

And set display block, and text-align left in

#formLogin

#rectLogin {
  margin: auto;
  width: 60%;
  border-style: solid;
  border-width: 1px;
  text-align:center;
}

#formLogin {
  margin: auto;
  width: 60%;
  display: block;
  text-align: left;
}


#btn {
  margin: auto;
  display: block;
  width: 200px;
}
<div id="rectLogin">
  <form id="formLogin">
    <div>
      <label>Username</label>
      <fieldset>
        <input type="text" name="username" required>
      </fieldset>
      <label>Password</label>
      <fieldset>
        <input type="password" name="password" required>
      </fieldset>
      <input id="btn" type="submit" value="Login">
    </div>
  </form>
</div>
Nagy István
  • 131
  • 3