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>

效果如下:

相关推荐
XinZong15 分钟前
【AI社交】基于OpenClaw自研轻量化AI社交平台实战
前端
Le_ee40 分钟前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
爱上好庆祝1 小时前
学习js的第七天(wed APIs的开始)
前端·javascript·css·学习·html·css3
KaMeidebaby2 小时前
卡梅德生物技术快报|冻干工艺开发:注射用心肌肽全流程参数优化与工程化方案
前端·其他·百度·新浪微博
Moment2 小时前
面试官:如果产品经理给你多个需求,怎么让AI去完成❓❓❓
前端·后端·面试
每天吃饭的羊2 小时前
JSONP
前端
gogoing2 小时前
ESLint 配置字段说明
前端·javascript
gogoing3 小时前
CSS 属性值计算过程(Computed Value)
前端·css
gogoing3 小时前
webpack 的性能优化
前端·javascript
桃花键神3 小时前
Bright Data Web Scraping指南 2026: 使用 MCP + Dify 自动采集海外社交媒体数据
大数据·前端·人工智能