I’m working on a basic search form and the visual design requires a graphic button instead of the browser-generated input. I’m using the button tag instead of an input type=”submit”. While putting the page together, I had a nagging issue with the button not aligning with the label and input. I tried various combinations of margins, negative-margins, padding, and even floated the elements. All of these techniques eventually worked, but the were too klunky and I knew there had to be a better way.

I remembered the vertical-align:middle style while working on a footer paragraph that included inline links and RSS buttons. I tried it with the submit button and it also worked perfectly. I’ve tested this in FF 1.5 and IE6. I have not tested it in Safari yet.

Code Examples

CSS:

  1. form#foo button {vertical-align:middle; border:none; padding:0; background:none; cursor:pointer; *cursor:hand; /*alternate cursor for IE*/}

HTML

  1. <button name=“submit” type=“submit”>
  2. <img src=“/images/btn-form-submit.png” alt=“submit search form” />
  3. </button>

Resources


4 Responses to “Using vertical-align for images and buttons”  

  1. 1 JTMS

    Great solution to a very common problem. Using a button rather than an input i was successful in lining up my custom image based submit button with the input field, but still had some issues with the “label” text. I opted to put everything into an unordered list which i then floated to the left. I then applied the vertical align to everything inside the list and was able to get nice vertical alignment on my small one line form. I did have to nudge the label text a bit for ie6, but other than that it turned out very clean!

  2. 2 OracleHome

    Thanks for solving this for me, it was driving me crazy.

    I had forgotten all about the vertical-align in CSS.

    Daniel

  3. 3 Ad Server

    I tried but the image is still above the text next to it. Do you have an example for this?

  4. 4 Eric

    Worked great. Thanks!

Leave a Reply