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>

效果如下:

相关推荐
FRing039 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
Bczheng19 小时前
组件化服务端渲染模式SSR
前端
whitelbwwww10 小时前
CUDA C++实战--中级
开发语言·前端·c++
张紫娃11 小时前
Chrome插件-Stylus(屏蔽博客广告)
前端·chrome·stylus
Yannik11 小时前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana11 小时前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端
inksci11 小时前
用飞帆做了个小网页
前端·javascript·css·html
去伪存真11 小时前
被 pnpm 打包坑惨后,我彻底盘明白了硬链接与软链接
前端·electron
zhensherlock12 小时前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
tryCbest17 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js