为什么保证金:自动; 不工作?

Dhr*_*dha 2 html javascript css

即使我添加了保证金:auto; .content类实际上是模态的内容,它仍然位于左上角.为什么会这样?我怎样才能将它对齐?

var mod=document.getElementById("myModal");
var img= document.getElementById("image");
img.addEventListener("click",animate);
function animate() {
	mod.style.display = "block";
}
Run Code Online (Sandbox Code Playgroud)
#image {
    width: 400px;
	cursor: pointer;
}
.modal {
	display: none;
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	z-index: 1;
    background-color: rgba(0,0,0,0.9);
}
.content {
	margin: auto;
	width: 800px;
	animation-name: zoom;
	animation-duration: 0.6s;
}
@keyframes zoom {
	from {transform: scale(0.1);} 
    to {transform: scale(1);}
}
Run Code Online (Sandbox Code Playgroud)
<html>
    <head>
	    <link rel="stylesheet" href="style.css">
	</head>
	<body>
	    <img id="image" src="http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg">
		<div id="myModal" class="modal">
			<img class="content" id="image01" src="http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg">
		</div>
        <script src="script.js"></script>		
	</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Lal*_*Lal 8

默认情况下,<img>replaced inline element这样的话你就必须将其转换为block.

因此,添加

display: block;
Run Code Online (Sandbox Code Playgroud)

对你的CSS来说也是如此 .content

也就是说,更新后的CSS .content就像是,

.content{
    display: block;
    margin: auto;
    width: 800px;
    animation-name: zoom;
    animation-duration: 0.6s;
}
Run Code Online (Sandbox Code Playgroud)