Vertical align block level element inside a block level element

前端 未结 1 984
傲寒
傲寒 2020-12-20 02:16

I need to center align images (variable width and height) inside block level elements of fixed width and height. The css markup looks something like this:

&l         


        
1条回答
  •  遥遥无期
    2020-12-20 03:05

    Yes, vertical margins are calculated in a fundamentally different way to horizontal ones; ‘auto’ doesn't mean centering.

    Setting ‘vertical-align: middle’ on the image elements sort of works, but it only aligns them relative to the line box they're currently on. To make the line box the same height as the float, set ‘line-height’ on the container:

    
    

    You have to be in Standards Mode for this to work, because otherwise browsers render images-on-their-own as blocks instead of inline replaced elements in a text line box.

    Unfortunately, IE (up to 7 at least) still keeps the block behaviour even in its attempt at a Standards Mode. There is a technical reason for this, namely that IE is pants.

    To persuade IE that you really mean it about the images being part of a text line, you have to add some text inside the div — even a normal space will do it, but you could also try a zero-width-space:

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