2

I want to vertically align 3 divs (that will contain icons) with input fields of a form. Is there any explanation why are form elements acting like they have top margins even after i set it to 0?

<form>
    <div></div>
    <div></div>
    <div></div>
    <input type="text" placeholder="username">
    <input type="password" placeholder="password">
</form>

* {
    margin: 0;
    padding: 0;
}
form {
    border: 1px solid black;
    display: inline-block;
}
div {
    display: inline-block;
    height: 32px;
    width: 32px;
    background: red;
}

http://jsfiddle.net/pnW4C/1/

Thank you.

Nightwhistle
  • 197
  • 1
  • 11

1 Answers1

1

You would add vertical-align:top to the elements in order to solve the alignment issues.

EXAMPLE HERE

div {
    display: inline-block;
    height: 32px;
    width: 32px;
    background: red;
    vertical-align:top;  /* It works because they are inline-block.. */
}

Alternatively, the values middle and bottom would work too. It just needs to be something other than the default value, which is baseline.

If you're wondering why the value baseline was behaving as it was, see this answer.

Community
  • 1
  • 1
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • how does changing the vertical position of the `
    ` move the input up and down? O_o
    – Phil Mar 18 '14 at 21:44
  • I was using central, didn't remember middle... It works with middle, thank you very much man! – Nightwhistle Mar 18 '14 at 21:45
  • @Phil *That*, I can't explain. [This answer](http://stackoverflow.com/questions/5804256/why-an-image-inside-a-div-has-an-extra-space-below-the-image/5804278#5804278) is relevant though. – Josh Crozier Mar 18 '14 at 21:51