为什么没有保证金:自动居中图像?

Joe*_*ips 91 html css

<html>
<head>
<title>Test</title>
</head>
<body>
    <div>
        <img src="queuedError.jpg" style="margin:auto; width:200px;" />
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

div100%扩展,因为它应该,但图像没有中心本身.为什么?

Kel*_*tex 188

因为您的图像是内联块元素.您可以将其更改为块级元素,如下所示:

<img src="queuedError.jpg" style="margin:auto; width:200px;display:block" />
Run Code Online (Sandbox Code Playgroud)

它会集中在一起.


The*_*dic 10

您需要将其渲染为块级别;

img {
   display: block;
   width: auto;
   margin: auto;
}
Run Code Online (Sandbox Code Playgroud)

  • 垂直对齐并不像您希望的那样直截了当.这完全取决于CSS规范 - 查看这篇关于此事的精美文章 - http://phrogz.net/css/vertical-align/index.html (4认同)

Pra*_*ana 8

style="text-align:center;"

尝试以下代码

<html>
<head>
<title>Test</title>
</head>
<body>
    <div style="text-align:center;vertical-align:middle;">
        <img src="queuedError.jpg" style="margin:auto; width:200px;" />
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)