css给文字设置背景色

html 复制代码
<!DOCTYPE html>
<html>

<head>
  <style>
    .father {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      padding: 5px;
    }

    .title {
      font-size: 12px;
      color: #405562;
      line-height: 20px;
      font-weight: 400;
      /* 配合父级的align-items: flex-start;子元素长度才不会是其父级的宽度 */
      position: relative;
    }

    .title span {
      /* 设置position为非static值,z-index才生效 */
      position: relative;
      z-index: 99;
    }

    .title div {
      width: 100%;
      /* 长方形的宽度 */
      height: 8px;
      /* 长方形的高度 */
      background: #F0F0F0;
      border-radius: 4px;
      position: absolute;
      left: 0px;
      top: 10px;
      z-index: 0;
    }

    .num {
      font-size: 15px;
      color: #333333;
      line-height: 20px;
      font-weight: 700;
    }
  </style>
</head>

<body>

  <div class="father">
    <div class="title">
      <span>文本文本文本文本文本文本</span>
      <div></div>
    </div>
    <span class="num">100</span>
  </div>

</body>

</html>

效果如下:

相关推荐
5***790019 分钟前
Vue项目性能优化
前端·javascript·vue.js
天若有情6731 小时前
【c++】手撸C++ Promise:从零实现通用异步回调组件,支持链式调用+异常安全
开发语言·前端·javascript·c++·promise
抱琴_1 小时前
【Vue3】大屏性能优化黑科技:Vue 3 中实现请求合并,让你的大屏飞起来!
前端·vue.js
不会玩电脑的Xin.1 小时前
HTML + CSS
前端·css·html
hadage2332 小时前
--- JavaScript 的一些常用语法总结 ---
java·前端·javascript
彭于晏爱编程2 小时前
🍭🍭🍭升级 AntD 6:做第一个吃螃蟹的人
前端
掘金一周2 小时前
大部分人都错了!这才是chrome插件多脚本通信的正确姿势 | 掘金一周 11.27
前端·人工智能·后端
_瑶瑶_2 小时前
浅记一下ElementPlus中的虚拟化表格(el-table-v2)的简单使用
前端·javascript
Drift_Dream2 小时前
ResizeObserver:轻松监听元素尺寸变化
前端