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>

效果如下:

相关推荐
正小安22 分钟前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   2 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常2 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
赛男丨木子丿小喵2 小时前
visual studio2022添加新项中没有html和css
css·html·visual studio
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
Jiaberrr3 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui