1:目标:标题前方希望有一个小提示,目标效果图如下
2:思考1:使用两个元素标签实现,第一个标签实现提示背景,第二个标签显示文字;
思考2:使用伪类元素before实现;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div class="content"> <div class="title">
<div class="icon"></div> <div class="title-name">标题一</div> </div>
<div class="title-before"> <div class="title-name-before">用伪类before实现</div> </div> </div> </body> <style> .content{ padding: 20px; } .title{ display: flex; justify-content: flex-start; } .title .icon{ width: 6px; border-radius: 3px; background-color: cornflowerblue; margin-right: 15px; } .title-name{ color: #333333; font-size: 16px; font-weight: bold; } .title-before{ margin-top: 20px; } /*::before实现*/ .title-name-before{ color: #333333; font-size: 16px; font-weight: bold; } .title-name-before::before{ content: ','; display: inline-block; width: 6px; color: cornflowerblue; border-radius: 3px; background-color: cornflowerblue; margin-right: 15px; } </style> </html>
3:使用伪类注意事项:
1:伪类元素“content” 必须有值才会显示,
2:伪类元素宽会根据“content”内容变化,自定义宽度需要设置display:inline-block;
3:color的值需要和background-color值一致;