文本对齐问题,之前提过类似的但是没有考虑到文本换行情况?

图片
要求是测试和下面的数字垂直居中,亿元不要参与对齐,一直跟在数字的后面,并且垂直居中于数字一行,并且要考虑到数字过多然后换行的情况。

阅读 2.2k
1 个回答

很简单的哇,把 “亿元” 两个字使用绝对定位挂在数字后面就行了,或者数字设置一个负的 margin-right

以下是示例代码:
图片.png

<!-- 使用绝对定位 -->
<body>
  <div class="demo">
    <div class="title">测试</div>
    <div class="number">
      216.11
      <span class="unit">亿元</span>
    </div>
  </div>
</body>
<style>
  .demo {
    line-height: 35px;
    text-align: center;
    color: #303030;
  }
  .demo .title {
    font-size: 13px;
  }
  .demo .number {
    font-size: 20px;
    font-weight: bold;
    position: relative;
  }
  .demo .number .unit {
    font-size: 12px;
    font-weight: normal;
    position: absolute;
  }
</style>
<!-- 使用 margin-right: -36px -->
<body>
  <div class="demo">
    <div class="title">测试</div>
    <div class="number">
      216.11
      <span class="unit">亿元</span>
    </div>
  </div>
</body>
<style>
  .demo {
    line-height: 35px;
    text-align: center;
    color: #303030;
  }
  .demo .title {
    font-size: 13px;
  }
  .demo .number {
    font-size: 20px;
    font-weight: bold;
    margin-right: -35px; /* "亿元"两个字的宽度 */
  }
  .demo .number .unit {
    font-size: 12px;
    font-weight: normal;
  }
</style>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题