【问题标题】:Center align text and image horizontally where text length can vary在文本长度可能变化的地方水平居中对齐文本和图像
【发布时间】: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


【解决方案1】:

首先,正如 JoostS 所说,摆脱你的 float:lefts 然后,您将有一个未对齐的文本字符串。要解决此问题,请删除 .location_text 上的 top:5px; 顶部并将 vertical-align:middle 添加到 .location-icon

.container {
  width: 260px;
  height: 298px;
  background: yellow;
}

.imagetext {
  width: 100%;
  height: 20px;
  background: green;
  position: relative;
  top: 50px;
  text-align: center;
}

.location-icon {
  vertical-align:middle;
  width: 20px;
  display: inline-block;
  margin-right: 5px;
}

.location-text {
  font-size: 12px;
  display: inline-block;
  position: relative;
}

body {
  background: white;
  font-family: sans-serif;
}
<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>

更新添加

如果你使用伪元素,你也可以大大减少你的标记:

.container {
  width: 260px;
  height: 298px;
  background: yellow;
}

.imagetext {
  width: 100%;
  height: 20px;
  background: green;
  position: relative;
  top: 50px;
  text-align: center;
  font-size: 12px;
}

.imagetext::before{
  display:inline-block;
  content:'';
  background-image:url(http://images.clipartpanda.com/blue-location-icon-Location-Icon-Blue.png);
  background-size:contain;
  width:20px;
  height:20px;
  background-repeat:no-repeat;
  vertical-align:middle;
  margin-right:5px;
}

body {
  background: white;
  font-family: sans-serif;
}
<div class='container'>
  <div class='imagetext'>
    Bosnia and Herzigovina
  </div>
</div>

【讨论】:

  • 查看更新,假设你有很多,这应该会大大减少你的标记。
【解决方案2】:

我更新了https://jsfiddle.net/d8t9e0p6/4/

您必须将文本对齐中心放在容器上, 并且 imagetext 容器需要自动宽度并显示 inline-block;

.container {
  width: 260px;
  height: 298px;
  background: yellow;
  text-align:center;
}

.imagetext {
  width:auto;
  height: 20px;
  background: green;
  position: relative;
  top: 50px;
  text-align: center;
  display:inline-block;
}

【讨论】:

    【解决方案3】:

    删除float:left css 语句。

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2019-04-25
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多