【发布时间】:2016-06-01 08:03:09
【问题描述】:
我有一个div,我必须在其中放置一个图标和一些文本到它的右侧,并安排整个事情,使它们都保持居中对齐(图像和文本)。问题是,文本长度可能会有所不同。所以如果是Libya这样的短文本,整个元素会靠近中心挤成一团,而如果是Bosnia and Herzigovina这样的大文本,就会散开(虽然仍然居中),图像向左靠近。我试过这个:
<div class='container'>
<div class='imagetext'>
<img class='location-icon' src="http://images.clipartpanda.com/blue-location-icon-Location-Icon-Blue.png" />
<span class='location-text'>
Bosnia and Herzigovina
</span>
</div>
</div>
还有CSS:
.container {
width: 260px;
height: 298px;
background: yellow;
}
.imagetext {
width: 100%;
height: 20px;
background: green;
position: relative;
top: 50px;
text-align: center;
}
.location-icon {
width: 20px;
display: inline-block;
margin-right: 5px;
float: left;
}
.location-text {
font-size: 12px;
display: inline-block;
float: left;
position: relative;
top: 5px;
}
body {
background: white;
font-family: sans-serif;
}
这是小提琴 - https://jsfiddle.net/d8t9e0p6/3/。即使将text-align 设置为center,我也无法将其居中。如何实现正确的中心对齐?
【问题讨论】:
-
float:left覆盖居中对齐。
标签: html css center-align