我在嵌套div .box
中附上了一个图像。如果它没有浮动,图像可以精确居中到.box
,但当我float
它left
或right
中心对齐消失!我有4个不同大小的图像,需要将它们集中在自己的.box
中。我该如何解决它?为什么不能在“div”中将图像置于“float”的右侧或左侧?
HTML
<div class="con">
<div class="box">
<img src="../_images/icon-test.png">
</div>
</div>
CSS
.con {
width:300px;
height:200px;
background: #996600;
}
.box {
position:relative;
width:150px;
height:150px;
background-color: #333333;
display:table-cell;
text-align:center;
vertical-align:middle;
float:right;
}
你为什么用'如果你想中心元素float'?你要么浮动,要么居中,而不是两者。 – Justinas
@Justinas,我想将图像集中到.box div而不是div元素, – Manoj