将文本居中置于绝对跨度之下

时间:2017-10-29 18:28:02

标签: html css

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



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>
&#13;
&#13;
&#13;

9 个答案:

答案 0 :(得分:3)

使用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>

答案 1 :(得分:1)

只需将文字插入第一个范围即可:

<div class="div1">
    <span class="number">
        1,000
        <span class="units">miles</span>
    </span>
</div>

并添加此CSS:

.units{
    display:block;
    text-align: center;
    font-size: 14px;
}

答案 2 :(得分:0)

这样做的一种方法是使用flexbox,如下所示:

.div1{
  display:flex;
  flex-flow:column;
}

答案 3 :(得分:0)

更改span.units中的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 { font-family: Verdana; font-size: 36px; font-weight: bold; position: absolute; top: 70%; left: 50%; transform: translateX(-50%) translateY(-50%); }值。

&#13;
&#13;
<div class="div1">
   <span class="number">1,000</span><br>
      <span class="units">miles</span>
   <br>

</div>
&#13;
header_record_filetype = cs.Struct(
    'record_type' / cs.Int8ub,
    'file_type' / cs.PascalString(cs.Int16ub),
    'unknown' / cs.Int8ub
)

header_record_user = cs.Struct(
    'record_type' / cs.Int8ub,
    'user' / cs.PascalString(cs.Int16ub)
)
&#13;
&#13;
&#13;

答案 4 :(得分:0)

用另一个div包裹它们(数字+单位)并将绝对位置应用于该div。

&#13;
&#13;
    .div1 {
      height:200px;
      width:200px;
      background-color:orange;
      position: relative;
    }
    .center {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
      text-align: center;
    }
    .number {
      font-family: Verdana;
      font-size: 36px;
      font-weight: bold;
    }
&#13;
    <div class="div1">
      <div class="center">
       <span class="number">1,000</span>
       <br/>
       <span class="units">miles</span>
      </div>
    </div>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

最好的方法是使用Flexbox,但如果您不想这样做,可以为{{1}添加text-align: centerwidth: 100%top: 62% } element。

&#13;
&#13;
.units
&#13;
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;
  position: absolute;
  top: 62%;
  left: 0;
  text-align: center;
  width: 100%;
}
&#13;
&#13;
&#13;

由于实验,我将<div class="div1"> <span class="number">1,000</span> <br/> <span class="units">miles</span> </div>设置为top。如果您认为值和单位之间的空间太小或太高,则可以使用其他值。

编辑:

使用Flexbox(正如我所说,它更好,因为你不会使用类似62%的硬编码值):

&#13;
&#13;
62%
&#13;
div.div1 {
  height: 200px;
  width: 200px;
  background-color: orange;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

span.number {
  font-family: Verdana;
  font-size: 36px;
  font-weight: bold;
}
&#13;
&#13;
&#13;

我还删除了<div class="div1"> <span class="number">1,000</span> <span class="units">miles</span> </div>代码。这是不必要的。仅在<br>标记内使用<br>

答案 6 :(得分:0)

CSS Flexbox 来救援! (而且,我们也可以摆脱一大堆不需要的CSS)

div.div1 {
    height:200px;
    width:200px;
    background-color:orange;
    display:flex;            /* Make the container a flex-container */
    justify-content:center;  /* Horizontally align children */
    align-items:center;      /* Vertically align children */
    flex-direction:column;   /* Change from row to column based layout */
}

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

答案 7 :(得分:0)

下面嵌入的代码段演示了以下内容:

  1. 标准增强型解决方案
  2. 替代绝对定位解决方案
  3. Flex-Box解决方案
  4. 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;
      font-family: Verdana;
      font-weight: bold;
      position: absolute;
      bottom: 25%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
    
    
    /* Additional */
    
    .container {
      height: 200px;
      width: 200px;
      background-color: orange;
      position: relative;
    }
    
    .container_number {
      font-family: Verdana;
      font-size: 36px;
      font-weight: bold;
    }
    
    .container_units {
      margin: 0 auto;
    }
    
    .container_inner {
      position: absolute;
      top: 0;
      bottom: 0;
      text-align: center;
      margin: auto;
      left: 0;
      right: 0;
      height: 30%;
    }
    
    .flex_container {
      height: 200px;
      width: 200px;
      background-color: orange;
      display: flex;
      flex-direction: column;
      justify-content: center;
      text-align: center;
      align-items: center;
    }
    
    .flex_container_number {
      font-family: Verdana;
      font-size: 36px;
      font-weight: bold;
    }
    
    .flex_container_units {
      margin: 0 auto;
    }
    <!-- Standard Augmented Solution -->
    <div class="div1">
      <span class="number">1,000</span>
      <br/>
      <span class="units">miles</span>
    </div>
    
    <hr>
    <!-- Alternative Absolute Positioning Solution -->
    <div class="container">
      <div class="container_inner">
        <span class="container_number">1,000</span>
        <br/>
        <span class="container_units">miles</span>
      </div>
    </div>
    
    <hr>
    <!-- Flex-Box Solution -->
    <div class="flex_container">
      <span class="flex_container_number">1,000</span>
      <span class="flex_container_units">miles</span>
    </div>

    JSFiddle演示: https://jsfiddle.net/vqgj2h7h/5/

答案 8 :(得分:0)

试试这个,Flexbox目前不支持某些浏览器。

&#13;
&#13;
div.div1 {
    height: 200px;
    width: 200px;
    background-color: orange;
    position: relative;
    text-align:center;
}

span.number {
    font-family: Verdana;
    padding-top: 33%;
    display: block;
    font-weight: 600;
    font-size: 36px;
}

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