how to vertically align text next to a floated image?

前端 未结 6 1886
谎友^
谎友^ 2020-12-31 02:09

I want to vertically align a span after a floated image. I searched for it in stack overflow and find this post. but my image is floated.

<
相关标签:
6条回答
  • 2020-12-31 02:44

    First remove float from it. Write like this:

    <img style="width:30px;height:30px;vertical-align:middle" src="http://lorempixel.com/output/abstract-q-c-640-480-8.jpg">
        <span>Doesn't work.</span>
    

    Check this http://jsfiddle.net/ws3Uf/

    0 讨论(0)
  • 2020-12-31 02:45

    Add line-height (equal to picture height):

    <div>
        <img style="width:30px;height:30px; float:left">
        <span style="vertical-align:middle; line-height: 30px;">Works!</span>
    </div>
    

    See example.

    0 讨论(0)
  • 2020-12-31 02:45

    You can manually change as well

    <div>
        <img style="width:30px;height:30px float:left">
        <span style="float:left;padding-top:15px;">Will work.</span>
    </div>
    

    Demo

    Or you can use a table

    0 讨论(0)
  • 2020-12-31 02:45

    A <span> is an inline element, try adding display:block to the span, give it the same height as the image and a line height to match. Float it left as well. That should work

    0 讨论(0)
  • 2020-12-31 03:02

    Even though this is an extremely old post, you can achieve this using Flexbox:

    div {
     display: flex;
     align-items: center;
    }
    <div>
    <img style="width:30px;height:30px;" src="http://lorempixel.com/output/abstract-q-c-640-480-8.jpg" />
    <span>Doesn't work.</span>
    </div>

    JsFiddle example

    0 讨论(0)
  • 2020-12-31 03:05

    You could do the following:

      div:after {
            content:"";
            clear:both;
            display:block;
        }
    
    0 讨论(0)
提交回复
热议问题