CSS:图片不拉伸,垂直居中显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <img src="" />
    </div>
    <div class="container">
        <img src="" />
    </div>
 
    <style>
    img{
        max-height: 100%;
        max-width: 100%;
        vertical-align: middle;
    }
 
    .container{
        height: 500px;
        width: 500px;
        margin-top: 20px;
        line-height: 498px;
        text-align: center;
        border: 1px solid #05a;
    }
    </style>
</body>
</html>