CSS: Why “vertical-align: middle” does not work?

前端 未结 8 1065
无人及你
无人及你 2021-02-13 06:24

Consider the following example: (live demo here)

HTML:



        
相关标签:
8条回答
  • 2021-02-13 07:07

    I had the same problem. This works for me:

     <style type="text/css">
        div.parent {
             position: relative;
        }
    
        /*vertical middle and horizontal center align*/
        img.child {
            bottom: 0;
            left: 0;
            margin: auto;
            position: absolute;
            right: 0;
            top: 0;
        }
        </style>
    
        <div class="parent">
            <img class="child"> 
        </div>
    
    0 讨论(0)
  • 2021-02-13 07:09

    Not sure what's the cause.

    Removing the line-height and adding margins to the image does the trick.

    a {
      display: block;
      background: #f00;
    }
    img {
      margin: .3em auto;
      vertical-align: middle;
    }
    <a>
     <img src="https://placeimg.com/30/30/any">
    </a>

    0 讨论(0)
提交回复
热议问题