Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Vertically Center Inline Image

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #30676
    akeenlabs
    Participant

    Maybe I’m missing something simple, but I can’t seem to find a good way to vertically center an inline image with the rest of the line.

    Take this code for example:


    This is a regular line of text with a textbox: and maybe a link with an image in it: . And maybe there's a little more text after.


    The bottom/baseline of the image lines up with the baseline of the text, as if the image was “sitting” on the same surface as the text itself. I would like the vertical center of the image to match the vertical center of the text (i.e., the middle between the text’s baseline and it’s “text-top”).

    It seems like I’ve seen this solution somewhere before, but I’m coming up short finding it again. Any thoughts?

    #79296
    Bob
    Member

    They invented vertical-align for that.
    Use it like this in your CSS:

    img { vertical-align:middle; }
    #79032
    akeenlabs
    Participant

    Yes, I’m well aware of vertical-align, but unfortunately it doesn’t actually center it. Sure, it’s centered based on the line’s x-height, but that’s almost always a couple of pixels off. Try it, you’ll see what I’m talking about.

    #78880
    Chris Coyier
    Keymaster

    I usually use vertical-align: middle;, then position: relative; and top or bottom to adjust it whatever few pixels need jacking.

    #116786
    wholenotes
    Member

    Vertical-align: middle; works on inline elements. We had to use a negative margin:top.

    #117759
    trick
    Member

    use display :table property

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘CSS’ is closed to new topics and replies.