html - 绝对跨度以下的居中文本

标签 html css

在下面的代码中,我有一个包含数字及其单位(在示例中为 1000 英里)的 div。数字在 div 中居中 OK,但我需要单位在数字的下方 居中。这怎么可能?

div.div1 {
  height: 200px;
  width: 200px;
  background-color: orange;
  position: relative;
}

span.number {
  font-family: Verdana;
  font-size: 36px;
  font-weight: bold;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

span.units {
  margin: 0 auto;
}
<div class="div1">
  <span class="number">1,000</span>
  <br/>
  <span class="units">miles</span>
</div>

最佳答案

使用 flexbox

.div1 {
  height: 200px;
  width: 200px;
  background-color: orange;

  display: flex;
  flex-direction: column;
  justify-content: center;    /* Vertical center */
  align-items: center;    /* Horizontal center */
}

.number {
  font-family: Verdana;
  font-size: 36px;
  font-weight: bold;
}
<div class="div1">
  <span class="number">1,000</span>
  <span class="units">miles</span>
</div>

关于html - 绝对跨度以下的居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47003732/

相关文章:

css - 如何使用 Twitter Bootstrap 3 将长单词包装在容器内?

javascript - 如何水平自动滚动轮播(jquery)

javascript - 在 div 中定位表格

css - Bootstrap 网站右侧的空白区域

javascript - Web 应用程序中的数字本地化

javascript - 无限滚动的 <tbody> 元素?

javascript - Less jsmodifyVars 不适用于字符串路径

jQuery - 当一张卡片悬停或有焦点时,模糊另一张

jquery - 使用 Jquery 在翻转时更改 DIV 背景

javascript - 如何使用jquery调整图像大小并防止无图像