将图像垂直和水平居中

Dip*_*Dam 2 html css image

我发现了关于水平和垂直居中图像的问题.但它没有用.我的代码是

#parent {
    position : relative;
    float : left;
    width : 700px;
    height : 400px;
    overflow : hidden;
    background-color: black;
}


#parent img {
    max-height :400px;
    max-width : 700px;
}
Run Code Online (Sandbox Code Playgroud)

图像的高度和宽度是动态的.

san*_*eep 8

像这样写:

#parent {
    position : relative;
    float : left;
    width : 700px;
    height : 400px;
    overflow : hidden;
    background-color: black;
    text-align:center;
    line-height:400px;
}


#parent img {
    vertical-align:middle;
}
Run Code Online (Sandbox Code Playgroud)

请查看 http://tinkerbin.com/XYKUzvXu

更新

请查看http://tinkerbin.com/GL4GaNfo

  • @anonymousdownvotingislame首先尝试自己. (2认同)