简体   繁体   中英

display: inline-block, span does not align properly

I've tried the vertical-align property, but failed to align the span in the middle:

 .foo { width: 500px; height: 50px; background-color: powderblue; display: inline-block; } .img { width: 50px; height: 50px; } .bar { vertical-align: middle; } 
 <div class="foo"> <img class="img" src="http://dmrctt.com/wp-content/uploads/2012/10/Address-FTN-Image_1322320725.jpg" alt=""> <span class="bar"> Lorem ipsum dolor sit amet </span> </div> 

I've also tried with the display: inline-block and expected that I could now align the span in the middle of the div . Why is this not working?

The default veritcal alignment of inline elements is baseline. So change it to something that fits what you need, like middle.

 .foo { width: 500px; height: 50px; background-color: powderblue; display: inline-block; } .img { width: 50px; height: 50px; vertical-align: middle; } .bar { vertical-align: center; } 
 <div class="foo"> <img class="img" src="http://dmrctt.com/wp-content/uploads/2012/10/Address-FTN-Image_1322320725.jpg" alt=""> <span class="bar"> Lorem ipsum dolor sit amet </span> </div> 

"center" is not a valid value for vertical-align, I think you mean "middle".

https://developer.mozilla.org/es/docs/Web/CSS/vertical-align

Also, display:inline-block is doing nothing for you there, as it should be on the child elements (if at all), not the container.

All you need to solve this is use vertical-align:middle on the img.

 .foo { width: 500px; height: 50px; background-color: powderblue; } .img { width: 50px; height: 50px; vertical-align:middle; } 
 <div class="foo"> <img class="img" src="http://dmrctt.com/wp-content/uploads/2012/10/Address-FTN-Image_1322320725.jpg" alt=""> <span class="bar"> Lorem ipsum dolor sit amet </span> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM