Css 图像居中不使用边距:0自动 .imgw{ 边距:0自动0自动; } img{ 边距:0自动0自动; }

Css 图像居中不使用边距:0自动 .imgw{ 边距:0自动0自动; } img{ 边距:0自动0自动; },css,Css,未使用文本对齐,已测试。我甚至尝试使用img元素本身,但都不起作用。margin:0 auto适用于块元素 尝试: 文本对齐:居中将起作用。我在下面展示了一个例子 HTML: 将css添加到: .imgw { text-align: center; } 这就是我在div中居中对齐地图所做的 .imgw{ width:96%; /* to give some width, is helpful in centering*/ margin: 0 auto; /* this

未使用文本对齐,已测试。我甚至尝试使用img元素本身,但都不起作用。

margin:0 auto
适用于块元素

尝试:


文本对齐:居中将起作用。我在下面展示了一个例子

HTML:

将css添加到:

.imgw {
    text-align: center;
}

这就是我在div中居中对齐地图所做的

.imgw{
    width:96%; /* to give some width, is helpful in centering*/
    margin: 0 auto; /* this should do */
}

.imgw > img{
    margin: 0 auto;
}
HTML结构:

.map{
  display: block;
  margin-right: auto;
  margin-left: auto;
}

好的,+1,但只有当
div中只有图像时,这才有效,否则OP必须将文本嵌套在另一个元素中,并将
文本对齐设置为left@Era不,你不需要——即使这是一个更好的解决方案,所以+1,但只要确保你使用
或至少
.imgw img{}
否则,您的选择器会将所有图像设置为块级别
.imgw {
    text-align: center;
}
.imgw{
    width:96%; /* to give some width, is helpful in centering*/
    margin: 0 auto; /* this should do */
}

.imgw > img{
    margin: 0 auto;
}
.map{
  display: block;
  margin-right: auto;
  margin-left: auto;
}
<div class='item-detail cf'>
    <img alt='Google Map' class='map' src= "some_url">
</div>