Vertically aligning an Image

Discussion in 'HTML & Website Design' started by MelogKnaj, Feb 7, 2008.

  1. #1
    What is the best way to vertically align a single image so that it works in all browsers? I tried googling it and found several different ways which all seemed to have various issues. What is the best way to do this?
     
    If someone posts a solution, use the "Best Answer" link in their post to pick it as the best answer.
    MelogKnaj, Feb 7, 2008 IP
  2. amazingronit

    amazingronit Well-Known Member

    Messages:
    1,334
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #2
    just put valign="middle" inside the cell, e.g. <td valign="middle"><img src="path-to-the-image" alt="something" /></td>

    It will keep the image floating in the vertical middle of the cell, regardless of the table or cell size.

    Hope it helps
     
  3. soulscratch

    soulscratch Well-Known Member

    Messages:
    964
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    155
    #3
    ^ Why are you assuming he's using tables?
     
  4. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #4
    The "valign" attribute is deprecated in all current recommendations. Use the style property, "vertical-align". The property is supported x-browser for elements displayed as "table-cell" (eg. td or th—IE doesn't support "table-cell" on other elements), and in the anonymous inline-box.

    Test case:
    
    <p><img src="som.png" alt="" />qypgj</p>
    ===========
    p {
      border: 1px dotted black;
      }
    
    img {
      vertical-align: top || middle || base-line  || bottom;
      }
    Code (markup):
    See also, mysterious gaps under images.

    cheers,

    gary
     
    kk5st, Feb 7, 2008 Set Best Answer IP
  5. MelogKnaj

    MelogKnaj Guest

    Best Answers:
    0
    #5
    Thanks for the help, I figured out a way to center it with the code below. It seems to work fine but there might be issues with it, please let me know if know of any.
    
    <table height="90%" align="center" valign="middle"><tr><td align="center">
    <img src="logo.jpg"/>
    </table>
    
    Code (markup):
     
  6. amazingronit

    amazingronit Well-Known Member

    Messages:
    1,334
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #6
    @soulscratch,

    See! he's using tables! it's kinda intutions u know ;-)